This commit is contained in:
2023-05-27 22:36:30 +02:00
parent c11780ba30
commit 563c8d9914

View File

@@ -2,21 +2,21 @@ use std::cmp::min;
use crate::bots::approved_bot::modules::download::StartDownloadData; use crate::bots::approved_bot::modules::download::StartDownloadData;
use super::types::{Author, AuthorBook, Book, SearchBook, Sequence, Translator, TranslatorBook, BookAuthor, BookGenre, AsBook}; use super::types::{
AsBook, Author, AuthorBook, Book, BookAuthor, BookGenre, SearchBook, Sequence, Translator,
TranslatorBook,
};
const NO_LIMIT: u32 = 4096; const NO_LIMIT: u32 = 4096;
pub trait Format { pub trait Format {
fn format(&self, max_size: u32) -> String; fn format(&self, max_size: u32) -> String;
} }
pub trait FormatInline { pub trait FormatInline {
fn format_inline(&self) -> String; fn format_inline(&self) -> String;
} }
impl FormatInline for BookAuthor { impl FormatInline for BookAuthor {
fn format_inline(&self) -> String { fn format_inline(&self) -> String {
let BookAuthor { let BookAuthor {
@@ -30,7 +30,6 @@ impl FormatInline for BookAuthor {
} }
} }
impl FormatInline for Translator { impl FormatInline for Translator {
fn format_inline(&self) -> String { fn format_inline(&self) -> String {
let Translator { let Translator {
@@ -45,12 +44,10 @@ impl FormatInline for Translator {
} }
} }
fn format_authors(authors: Vec<BookAuthor>, count: usize) -> String { fn format_authors(authors: Vec<BookAuthor>, count: usize) -> String {
match !authors.is_empty() { match !authors.is_empty() {
true => { true => {
let formated_authors = authors let formated_authors = authors.clone()[..min(count, authors.len())]
.clone()[..min(count, authors.len())]
.into_iter() .into_iter()
.map(|author| author.format_inline()) .map(|author| author.format_inline())
.collect::<Vec<String>>() .collect::<Vec<String>>()
@@ -61,12 +58,10 @@ fn format_authors(authors: Vec<BookAuthor>, count: usize) -> String {
} }
} }
fn format_translators(translators: Vec<Translator>, count: usize) -> String { fn format_translators(translators: Vec<Translator>, count: usize) -> String {
match !translators.is_empty() { match !translators.is_empty() {
true => { true => {
let formated_translators = translators let formated_translators = translators.clone()[..min(count, translators.len())]
.clone()[..min(count, translators.len())]
.into_iter() .into_iter()
.map(|translator| translator.format_inline()) .map(|translator| translator.format_inline())
.collect::<Vec<String>>() .collect::<Vec<String>>()
@@ -77,12 +72,10 @@ fn format_translators(translators: Vec<Translator>, count: usize) -> String {
} }
} }
fn format_sequences(sequences: Vec<Sequence>, count: usize) -> String { fn format_sequences(sequences: Vec<Sequence>, count: usize) -> String {
match !sequences.is_empty() { match !sequences.is_empty() {
true => { true => {
let formated_sequences: String = sequences let formated_sequences: String = sequences.clone()[..min(count, sequences.len())]
.clone()[..min(count, sequences.len())]
.into_iter() .into_iter()
.map(|sequence| sequence.format(NO_LIMIT)) .map(|sequence| sequence.format(NO_LIMIT))
.collect::<Vec<String>>() .collect::<Vec<String>>()
@@ -93,12 +86,10 @@ fn format_sequences(sequences: Vec<Sequence>, count: usize) -> String {
} }
} }
fn format_genres(genres: Vec<BookGenre>, count: usize) -> String { fn format_genres(genres: Vec<BookGenre>, count: usize) -> String {
match !genres.is_empty() { match !genres.is_empty() {
true => { true => {
let formated_genres: String = genres let formated_genres: String = genres.clone()[..min(count, genres.len())]
.clone()[..min(count, genres.len())]
.into_iter() .into_iter()
.map(|genre| genre.format()) .map(|genre| genre.format())
.collect::<Vec<String>>() .collect::<Vec<String>>()
@@ -109,7 +100,6 @@ fn format_genres(genres: Vec<BookGenre>, count: usize) -> String {
} }
} }
impl Format for Author { impl Format for Author {
fn format(&self, _max_size: u32) -> String { fn format(&self, _max_size: u32) -> String {
let Author { let Author {
@@ -131,7 +121,6 @@ impl Format for Author {
} }
} }
impl Format for Sequence { impl Format for Sequence {
fn format(&self, _max_size: u32) -> String { fn format(&self, _max_size: u32) -> String {
let Sequence { id, name, .. } = self; let Sequence { id, name, .. } = self;
@@ -143,7 +132,6 @@ impl Format for Sequence {
} }
} }
impl Format for Translator { impl Format for Translator {
fn format(&self, _max_size: u32) -> String { fn format(&self, _max_size: u32) -> String {
let Translator { let Translator {
@@ -165,6 +153,23 @@ impl Format for Translator {
} }
} }
struct FormatVectorsResult {
authors: String,
translators: String,
sequences: String,
genres: String,
}
impl Book {
fn format_vectors(&self, max_size: u32) -> FormatVectorsResult {
FormatVectorsResult {
authors: format_authors(self.authors.clone(), self.authors.len()),
translators: format_translators(self.translators.clone(), self.translators.len()),
sequences: format_sequences(self.sequences.clone(), self.sequences.len()),
genres: format_genres(self.genres.clone(), self.genres.len()),
}
}
}
impl Format for Book { impl Format for Book {
fn format(&self, max_size: u32) -> String { fn format(&self, max_size: u32) -> String {
@@ -189,30 +194,27 @@ impl Format for Book {
let download_command = (StartDownloadData { id: self.id }).to_string(); let download_command = (StartDownloadData { id: self.id }).to_string();
let download_links = format!("Скачать:\n📥{download_command}"); let download_links = format!("Скачать:\n📥{download_command}");
let authors = format_authors(self.authors.clone(), self.authors.len()); let required_data_len: u32 = format!("{book_title}{pages_count}{annotations}{download_links}").len().try_into().unwrap();
let translators = format_translators(self.translators.clone(), self.translators.len()); let FormatVectorsResult { authors, translators, sequences, genres } = self.format_vectors(
let sequences = format_sequences(self.sequences.clone(), self.sequences.len()); max_size - required_data_len
let genres = format_genres(self.genres.clone(), self.genres.len()); );
format!("{book_title}{pages_count}{annotations}{authors}{translators}{sequences}{genres}{download_links}") format!("{book_title}{pages_count}{annotations}{authors}{translators}{sequences}{genres}{download_links}")
} }
} }
impl Format for SearchBook { impl Format for SearchBook {
fn format(&self, max_size: u32) -> String { fn format(&self, max_size: u32) -> String {
self.clone().as_book().format(max_size) self.clone().as_book().format(max_size)
} }
} }
impl Format for AuthorBook { impl Format for AuthorBook {
fn format(&self, max_size: u32) -> String { fn format(&self, max_size: u32) -> String {
self.clone().as_book().format(max_size) self.clone().as_book().format(max_size)
} }
} }
impl Format for TranslatorBook { impl Format for TranslatorBook {
fn format(&self, max_size: u32) -> String { fn format(&self, max_size: u32) -> String {
self.clone().as_book().format(max_size) self.clone().as_book().format(max_size)