From e77c8157ae5813ca43aac551129e0a028750dd03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 2 Nov 2021 23:49:00 +0100 Subject: [PATCH] cargo fmt, version bump --- CHANGELOG.md | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/group_handler/handle_mention.rs | 33 ++++++++++++++--------------- src/group_handler/mod.rs | 5 ++--- src/main.rs | 6 ++---- src/store/common_config.rs | 8 +++---- src/store/group_config.rs | 28 ++++++++++++++++++------ src/store/mod.rs | 14 ++++++------ src/tr.rs | 17 +++++++-------- 10 files changed, 67 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccce2ab..99b5022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.4.4 +- Fix some failing tests +- Lowercase the domain when normalizing an account + ## v0.4.3 - Fix hashtag not working in a mention diff --git a/Cargo.lock b/Cargo.lock index 109525c..14ad1dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,7 +328,7 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] name = "fedigroups" -version = "0.4.3" +version = "0.4.4" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 02eb503..21a0f72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fedigroups" -version = "0.4.3" +version = "0.4.4" authors = ["Ondřej Hruška "] edition = "2018" publish = false diff --git a/src/group_handler/handle_mention.rs b/src/group_handler/handle_mention.rs index 0f28630..3166924 100644 --- a/src/group_handler/handle_mention.rs +++ b/src/group_handler/handle_mention.rs @@ -2,25 +2,21 @@ use std::cmp::Ordering; use std::collections::HashSet; use std::time::Duration; -use elefren::{FediClient, SearchType, StatusBuilder}; use elefren::entities::account::Account; use elefren::entities::prelude::Status; use elefren::status_builder::Visibility; +use elefren::{FediClient, SearchType, StatusBuilder}; -use crate::command::{RE_NOBOT_TAG, StatusCommand}; +use crate::command::{StatusCommand, RE_NOBOT_TAG}; use crate::error::GroupError; use crate::group_handler::GroupHandle; -use crate::store::CommonConfig; use crate::store::group_config::GroupConfig; +use crate::store::CommonConfig; use crate::tr::TranslationTable; use crate::utils; -use crate::utils::{LogError, normalize_acct, VisExt}; +use crate::utils::{normalize_acct, LogError, VisExt}; -use crate::{ - grp_debug, - grp_warn, - grp_info -}; +use crate::{grp_debug, grp_info, grp_warn}; pub struct ProcessMention<'a> { status: Status, @@ -51,7 +47,7 @@ impl<'a> ProcessMention<'a> { self.client .search_v2(acct, !followed, Some(SearchType::Accounts), Some(1), followed), ) - .await + .await { Err(_) => { grp_warn!(self, "Account lookup timeout!"); @@ -98,9 +94,9 @@ impl<'a> ProcessMention<'a> { let mut to_add = String::new(); for m in members { to_add.push_str(&if admins.contains(&m) { - crate::tr!(self, "user_list_entry_admin", user=m) + crate::tr!(self, "user_list_entry_admin", user = m) } else { - crate::tr!(self, "user_list_entry", user=m) + crate::tr!(self, "user_list_entry", user = m) }); } self.add_reply(&to_add); @@ -149,8 +145,7 @@ impl<'a> ProcessMention<'a> { } async fn reblog_status(&self) { - self.client.reblog(&self.status.id) - .await.log_error("Failed to reblog status"); + self.client.reblog(&self.status.id).await.log_error("Failed to reblog status"); self.delay_after_post().await; } @@ -419,7 +414,7 @@ impl<'a> ProcessMention<'a> { async fn cmd_undo(&mut self) -> Result<(), GroupError> { if let (Some(ref parent_account_id), Some(ref parent_status_id)) = - (&self.status.in_reply_to_account_id, &self.status.in_reply_to_id) + (&self.status.in_reply_to_account_id, &self.status.in_reply_to_id) { if parent_account_id == &self.group_account.id { // This is a post sent by the group user, likely an announcement. @@ -675,7 +670,11 @@ impl<'a> ProcessMention<'a> { }; if self.config.is_member_only() { - self.add_reply(crate::tr!(self, "help_group_info_closed", membership = &membership_line)); + self.add_reply(crate::tr!( + self, + "help_group_info_closed", + membership = &membership_line + )); } else { self.add_reply(crate::tr!(self, "help_group_info_open", membership = &membership_line)); } @@ -709,7 +708,7 @@ impl<'a> ProcessMention<'a> { let mut to_add = String::new(); for t in tags { - to_add.push_str(&crate::tr!(self, "tag_list_entry", tag=t)); + to_add.push_str(&crate::tr!(self, "tag_list_entry", tag = t)); } self.add_reply(to_add); } diff --git a/src/group_handler/mod.rs b/src/group_handler/mod.rs index c0d0393..6a7b5f4 100644 --- a/src/group_handler/mod.rs +++ b/src/group_handler/mod.rs @@ -42,7 +42,7 @@ pub struct GroupInternal { impl Default for GroupInternal { fn default() -> Self { Self { - recently_seen_notif_statuses: VecDeque::new() + recently_seen_notif_statuses: VecDeque::new(), } } } @@ -552,8 +552,7 @@ impl GroupHandle { self.config.set_member(notif_acct, true).log_error("Fail add a member"); - crate::tr!(self, "mention_prefix", user = notif_acct) - + &crate::tr!(self, "welcome_public") + crate::tr!(self, "mention_prefix", user = notif_acct) + &crate::tr!(self, "welcome_public") }; let post = StatusBuilder::new() diff --git a/src/main.rs b/src/main.rs index 2f91ced..404602e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,10 +76,8 @@ async fn main() -> anyhow::Result<()> { let default_level = 3; - let level = ( - default_level as isize - + args.occurrences_of("verbose") as isize - - args.occurrences_of("quiet") as isize) + let level = (default_level as isize + args.occurrences_of("verbose") as isize + - args.occurrences_of("quiet") as isize) .clamp(0, LEVELS.len() as isize) as usize; env_logger::Builder::new() diff --git a/src/store/common_config.rs b/src/store/common_config.rs index a3cddf8..43dc824 100644 --- a/src/store/common_config.rs +++ b/src/store/common_config.rs @@ -1,6 +1,6 @@ -use std::collections::HashMap; use crate::store::DEFAULT_LOCALE_NAME; use crate::tr::TranslationTable; +use std::collections::HashMap; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default, deny_unknown_fields)] @@ -29,7 +29,7 @@ pub struct CommonConfig { /// This is a work-around for servers that stop sending notifs after a while. pub socket_retire_time_s: f64, #[serde(skip)] - pub tr : HashMap, + pub tr: HashMap, } impl Default for CommonConfig { @@ -52,10 +52,10 @@ impl Default for CommonConfig { } impl CommonConfig { - pub fn tr(&self, lang : &str) -> &TranslationTable { + pub fn tr(&self, lang: &str) -> &TranslationTable { match self.tr.get(lang) { Some(tr) => tr, - None => self.tr.get(DEFAULT_LOCALE_NAME).expect("default locale is not loaded") + None => self.tr.get(DEFAULT_LOCALE_NAME).expect("default locale is not loaded"), } } } diff --git a/src/store/group_config.rs b/src/store/group_config.rs index 15fe25c..ab0b0e2 100644 --- a/src/store/group_config.rs +++ b/src/store/group_config.rs @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; use elefren::AppData; use crate::error::GroupError; -use crate::store::{DEFAULT_LOCALE_NAME, CommonConfig}; +use crate::store::{CommonConfig, DEFAULT_LOCALE_NAME}; use crate::tr::TranslationTable; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -206,7 +206,7 @@ async fn load_locale_override_file(locale_path: impl AsRef) -> Result Result<(), GroupError> { + pub(crate) async fn initialize_by_appdata( + acct: String, + appdata: AppData, + group_dir: PathBuf, + ) -> Result<(), GroupError> { if !group_dir.is_dir() { debug!("Creating group directory"); tokio::fs::create_dir_all(&group_dir).await?; @@ -306,12 +310,17 @@ impl GroupConfig { /* state */ let state = load_or_create_state_file(state_path).await?; - let g = GroupConfig { config, control, state, _group_tr: TranslationTable::new() }; + let g = GroupConfig { + config, + control, + state, + _group_tr: TranslationTable::new(), + }; g.warn_of_bad_config(); Ok(()) } - pub(crate) async fn from_dir(group_dir: PathBuf, cc : &CommonConfig) -> Result { + pub(crate) async fn from_dir(group_dir: PathBuf, cc: &CommonConfig) -> Result { let config_path = group_dir.join("config.json"); let control_path = group_dir.join("control.json"); let state_path = group_dir.join("state.json"); @@ -338,7 +347,12 @@ impl GroupConfig { } } - let g = GroupConfig { config, control, state, _group_tr: tr }; + let g = GroupConfig { + config, + control, + state, + _group_tr: tr, + }; g.warn_of_bad_config(); Ok(g) } @@ -588,7 +602,7 @@ mod tests { config: Default::default(), control: Default::default(), state: Default::default(), - _group_tr: Default::default() + _group_tr: Default::default(), } } diff --git a/src/store/mod.rs b/src/store/mod.rs index 041606c..d050620 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -9,9 +9,9 @@ use crate::group_handler::{GroupHandle, GroupInternal}; pub mod common_config; pub mod group_config; +use crate::tr::TranslationTable; pub use common_config::CommonConfig; pub use group_config::GroupConfig; -use crate::tr::TranslationTable; #[derive(Debug, Default)] pub struct ConfigStore { @@ -32,8 +32,8 @@ pub struct StoreOptions { pub store_dir: String, } -const DEFAULT_LOCALE_NAME : &str = "en"; -const DEFAULT_LOCALE_JSON : &str = include_str!("../../locales/en.json"); +const DEFAULT_LOCALE_NAME: &str = "en"; +const DEFAULT_LOCALE_JSON: &str = include_str!("../../locales/en.json"); impl ConfigStore { /// Create a new instance of the store. @@ -214,7 +214,7 @@ impl ConfigStore { Ok(f) => { let locale_name = path.file_stem().unwrap_or_default().to_string_lossy(); self.load_locale(&locale_name, &String::from_utf8_lossy(&f), false); - }, + } Err(e) => { error!("Failed to read locale file {}: {}", path.display(), e); } @@ -234,10 +234,12 @@ impl ConfigStore { for (k, v) in def_tr.entries() { if !tr.translation_exists(k) { if self.config.validate_locales { - warn!("locale \"{}\" is missing \"{}\", default: {:?}", + warn!( + "locale \"{}\" is missing \"{}\", default: {:?}", locale_name, k, - def_tr.get_translation_raw(k).unwrap()); + def_tr.get_translation_raw(k).unwrap() + ); } tr.add_translation(k, v); } diff --git a/src/tr.rs b/src/tr.rs index 53355d1..3072544 100644 --- a/src/tr.rs +++ b/src/tr.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -#[derive(Debug,Clone,Serialize,Deserialize,Default)] +#[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct TranslationTable { #[serde(flatten)] entries: HashMap, @@ -15,24 +15,24 @@ impl TranslationTable { } /// Iterate all entries - pub fn entries(&self) -> impl Iterator { + pub fn entries(&self) -> impl Iterator { self.entries.iter() } - pub fn get_translation_raw(&self, key : &str) -> Option<&str> { + pub fn get_translation_raw(&self, key: &str) -> Option<&str> { self.entries.get(key).map(|s| s.as_str()) } /// Add or update a translation - pub fn add_translation(&mut self, key : impl ToString, subs : impl ToString) { + pub fn add_translation(&mut self, key: impl ToString, subs: impl ToString) { self.entries.insert(key.to_string(), subs.to_string()); } - pub fn translation_exists(&self, key : &str) -> bool { + pub fn translation_exists(&self, key: &str) -> bool { self.entries.contains_key(key) } - pub fn subs(&self, key : &str, substitutions: &[&str]) -> String { + pub fn subs(&self, key: &str, substitutions: &[&str]) -> String { match self.entries.get(key) { Some(s) => { // TODO optimize @@ -45,7 +45,7 @@ impl TranslationTable { } s } - None => key.to_owned() + None => key.to_owned(), } } } @@ -56,12 +56,11 @@ mod tests { #[test] fn deser_tr_table() { - let tr : TranslationTable = serde_json::from_str(r#"{"foo":"bar"}"#).unwrap(); + let tr: TranslationTable = serde_json::from_str(r#"{"foo":"bar"}"#).unwrap(); assert_eq!("bar", tr.subs("foo", &[])); assert_eq!("xxx", tr.subs("xxx", &[])); } - #[test] fn subs() { let mut tr = TranslationTable::new();