mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Optimize
This commit is contained in:
@@ -108,7 +108,7 @@ impl AnnotationFormat for BookAnnotation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_normal_text(&self) -> bool {
|
fn is_normal_text(&self) -> bool {
|
||||||
self.text.replace('\n', "").replace(' ', "").len() != 0
|
!self.text.replace(['\n', ' '], "").is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,11 +116,11 @@ impl GetPaginationCallbackData for AnnotationCallbackData {
|
|||||||
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
||||||
match self {
|
match self {
|
||||||
AnnotationCallbackData::Book { id, .. } => AnnotationCallbackData::Book {
|
AnnotationCallbackData::Book { id, .. } => AnnotationCallbackData::Book {
|
||||||
id: id.clone(),
|
id: *id,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
},
|
},
|
||||||
AnnotationCallbackData::Author { id, .. } => AnnotationCallbackData::Author {
|
AnnotationCallbackData::Author { id, .. } => AnnotationCallbackData::Author {
|
||||||
id: id.clone(),
|
id: *id,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ impl AnnotationFormat for AuthorAnnotation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_normal_text(&self) -> bool {
|
fn is_normal_text(&self) -> bool {
|
||||||
self.text.replace("\n", "").replace(' ', "").len() != 0
|
!self.text.replace(['\n', ' '], "").is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,15 +96,15 @@ impl GetPaginationCallbackData for BookCallbackData {
|
|||||||
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
||||||
match self {
|
match self {
|
||||||
BookCallbackData::Author { id, .. } => BookCallbackData::Author {
|
BookCallbackData::Author { id, .. } => BookCallbackData::Author {
|
||||||
id: id.clone(),
|
id: *id,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
},
|
},
|
||||||
BookCallbackData::Translator { id, .. } => BookCallbackData::Translator {
|
BookCallbackData::Translator { id, .. } => BookCallbackData::Translator {
|
||||||
id: id.clone(),
|
id: *id,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
},
|
},
|
||||||
BookCallbackData::Sequence { id, .. } => BookCallbackData::Sequence {
|
BookCallbackData::Sequence { id, .. } => BookCallbackData::Sequence {
|
||||||
id: id.clone(),
|
id: *id,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ impl std::str::FromStr for RandomCallbackData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(strum::ParseError::VariantNotFound);
|
Err(strum::ParseError::VariantNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ where
|
|||||||
Err(int_error) => return Err(Box::new(int_error)),
|
Err(int_error) => return Err(Box::new(int_error)),
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(err);
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,7 +210,6 @@ async fn get_genre_metas_handler(cq: CallbackQuery, bot: CacheMe<Throttle<Bot>>)
|
|||||||
RandomCallbackData::Genres {
|
RandomCallbackData::Genres {
|
||||||
index: index as u32
|
index: index as u32
|
||||||
}
|
}
|
||||||
.to_string()
|
|
||||||
)),
|
)),
|
||||||
text: genre_meta,
|
text: genre_meta,
|
||||||
}]
|
}]
|
||||||
@@ -277,7 +276,7 @@ async fn get_genres_by_meta_handler(
|
|||||||
vec![InlineKeyboardButton {
|
vec![InlineKeyboardButton {
|
||||||
kind: teloxide::types::InlineKeyboardButtonKind::CallbackData(format!(
|
kind: teloxide::types::InlineKeyboardButtonKind::CallbackData(format!(
|
||||||
"{}_{}",
|
"{}_{}",
|
||||||
RandomCallbackData::RandomBookByGenre { id: genre.id }.to_string(),
|
RandomCallbackData::RandomBookByGenre { id: genre.id },
|
||||||
genre.id
|
genre.id
|
||||||
)),
|
)),
|
||||||
text: genre.description,
|
text: genre.description,
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ fn get_query(cq: CallbackQuery) -> Option<String> {
|
|||||||
.map(|reply_to_message| {
|
.map(|reply_to_message| {
|
||||||
reply_to_message
|
reply_to_message
|
||||||
.text()
|
.text()
|
||||||
.map(|text| text.replace('/', "").replace('&', "").replace('?', ""))
|
.map(|text| text.replace(['/', '&', '?'], ""))
|
||||||
})
|
})
|
||||||
.unwrap_or(None)
|
.unwrap_or(None)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ async fn settings_callback_handler(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if allowed_langs_set.len() == 0 {
|
if allowed_langs_set.is_empty() {
|
||||||
return match bot
|
return match bot
|
||||||
.answer_callback_query(cq.id)
|
.answer_callback_query(cq.id)
|
||||||
.text("Должен быть активен, хотя бы один язык!")
|
.text("Должен быть активен, хотя бы один язык!")
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ impl GetPaginationCallbackData for UpdateLogCallbackData {
|
|||||||
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
fn get_pagination_callback_data(&self, target_page: u32) -> String {
|
||||||
let UpdateLogCallbackData { from, to, .. } = self;
|
let UpdateLogCallbackData { from, to, .. } = self;
|
||||||
UpdateLogCallbackData {
|
UpdateLogCallbackData {
|
||||||
from: from.clone(),
|
from: *from,
|
||||||
to: to.clone(),
|
to: *to,
|
||||||
page: target_page,
|
page: target_page,
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
|
|||||||
@@ -73,14 +73,14 @@ where
|
|||||||
&search_data,
|
&search_data,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if t_page + 1 <= total_pages.into() {
|
if t_page < total_pages.into() {
|
||||||
one_page_row.push(generic_get_pagination_button(
|
one_page_row.push(generic_get_pagination_button(
|
||||||
page + 1,
|
page + 1,
|
||||||
PaginationDelta::OnePlus,
|
PaginationDelta::OnePlus,
|
||||||
&search_data,
|
&search_data,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if one_page_row.len() != 0 {
|
if !one_page_row.is_empty() {
|
||||||
result.push(one_page_row);
|
result.push(one_page_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,14 +93,14 @@ where
|
|||||||
&search_data,
|
&search_data,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if t_page + 1 <= total_pages.into() {
|
if t_page < total_pages.into() {
|
||||||
five_page_row.push(generic_get_pagination_button(
|
five_page_row.push(generic_get_pagination_button(
|
||||||
page + 5,
|
page + 5,
|
||||||
PaginationDelta::FivePlus,
|
PaginationDelta::FivePlus,
|
||||||
&search_data,
|
&search_data,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if five_page_row.len() != 0 {
|
if !five_page_row.is_empty() {
|
||||||
result.push(five_page_row);
|
result.push(five_page_row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ pub fn split_text_to_chunks(text: &str, width: usize) -> Vec<String> {
|
|||||||
|
|
||||||
let chunks = textwrap::wrap(text, 512)
|
let chunks = textwrap::wrap(text, 512)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|text| text.replace('\r', "").len() != 0)
|
.filter(|text| !text.replace('\r', "").is_empty())
|
||||||
.map(|text| text.to_string());
|
.map(|text| text.to_string());
|
||||||
|
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ impl Format for Book {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let authors = match self.authors.len() != 0 {
|
let authors = match !self.authors.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_authors = self
|
let formated_authors = self
|
||||||
.authors
|
.authors
|
||||||
@@ -40,7 +40,7 @@ impl Format for Book {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let translators = match self.translators.len() != 0 {
|
let translators = match !self.translators.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_translators = self
|
let formated_translators = self
|
||||||
.translators
|
.translators
|
||||||
@@ -54,7 +54,7 @@ impl Format for Book {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sequences = match self.sequences.len() != 0 {
|
let sequences = match !self.sequences.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_sequences: String = self
|
let formated_sequences: String = self
|
||||||
.sequences
|
.sequences
|
||||||
@@ -68,7 +68,7 @@ impl Format for Book {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let genres = match self.genres.len() != 0 {
|
let genres = match !self.genres.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_genres: String = self
|
let formated_genres: String = self
|
||||||
.genres
|
.genres
|
||||||
@@ -145,11 +145,11 @@ impl Format for SearchBook {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let authors = if self.authors.len() != 0 {
|
let authors = if !self.authors.is_empty() {
|
||||||
let formated_authors = self
|
let formated_authors = self
|
||||||
.authors
|
.authors
|
||||||
.clone()[..min(5, self.authors.len())]
|
.clone()[..min(5, self.authors.len())]
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|author| author.format_author())
|
.map(|author| author.format_author())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
@@ -160,11 +160,11 @@ impl Format for SearchBook {
|
|||||||
"".to_string()
|
"".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let translators = if self.translators.len() != 0 {
|
let translators = if !self.translators.is_empty() {
|
||||||
let formated_translators = self
|
let formated_translators = self
|
||||||
.translators
|
.translators
|
||||||
.clone()[..min(5, self.translators.len())]
|
.clone()[..min(5, self.translators.len())]
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|translator| translator.format_translator())
|
.map(|translator| translator.format_translator())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
@@ -227,7 +227,7 @@ impl Format for AuthorBook {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let translators = match self.translators.len() != 0 {
|
let translators = match !self.translators.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_translators = self
|
let formated_translators = self
|
||||||
.translators
|
.translators
|
||||||
@@ -272,7 +272,7 @@ impl Format for TranslatorBook {
|
|||||||
false => "".to_string(),
|
false => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let authors = match self.authors.len() != 0 {
|
let authors = match !self.authors.is_empty() {
|
||||||
true => {
|
true => {
|
||||||
let formated_authors = self
|
let formated_authors = self
|
||||||
.authors
|
.authors
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub async fn message_handler(
|
|||||||
let result = register::register(from_user.id, text).await;
|
let result = register::register(from_user.id, text).await;
|
||||||
|
|
||||||
let message_text = match result {
|
let message_text = match result {
|
||||||
register::RegisterStatus::Success { ref username } => format_registered_message(&username),
|
register::RegisterStatus::Success { ref username } => format_registered_message(username),
|
||||||
register::RegisterStatus::WrongToken => strings::ERROR_MESSAGE.to_string(),
|
register::RegisterStatus::WrongToken => strings::ERROR_MESSAGE.to_string(),
|
||||||
register::RegisterStatus::RegisterFail => strings::ALREADY_REGISTERED.to_string(),
|
register::RegisterStatus::RegisterFail => strings::ALREADY_REGISTERED.to_string(),
|
||||||
};
|
};
|
||||||
@@ -30,7 +30,7 @@ pub async fn message_handler(
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_manager_handler() -> Handler<
|
pub fn get_manager_handler() -> Handler<
|
||||||
|
|||||||
@@ -68,5 +68,5 @@ pub async fn register(user_id: UserId, message_text: &str) -> RegisterStatus {
|
|||||||
return RegisterStatus::RegisterFail;
|
return RegisterStatus::RegisterFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
return RegisterStatus::Success { username: bot_username };
|
RegisterStatus::Success { username: bot_username }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
pub fn format_registered_message(username: &str) -> String {
|
pub fn format_registered_message(username: &str) -> String {
|
||||||
return format!("@{username} зарегистрирован и через несколько минут будет подключен!", username = username);
|
format!("@{username} зарегистрирован и через несколько минут будет подключен!", username = username)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ALREADY_REGISTERED: &str= "Ошибка! Возможно бот уже зарегистрирован!";
|
pub const ALREADY_REGISTERED: &str= "Ошибка! Возможно бот уже зарегистрирован!";
|
||||||
|
|||||||
@@ -157,8 +157,8 @@ impl BotsManager {
|
|||||||
|
|
||||||
async fn update_data(&mut self, bots_data: Vec<BotData>) {
|
async fn update_data(&mut self, bots_data: Vec<BotData>) {
|
||||||
for bot_data in bots_data.iter() {
|
for bot_data in bots_data.iter() {
|
||||||
if !self.bot_port_map.contains_key(&bot_data.id) {
|
if let std::collections::hash_map::Entry::Vacant(e) = self.bot_port_map.entry(bot_data.id) {
|
||||||
self.bot_port_map.insert(bot_data.id, self.next_port);
|
e.insert(self.next_port);
|
||||||
self.next_port += 1;
|
self.next_port += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user