diff --git a/src/lib.rs b/src/lib.rs index f525719..c070259 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,10 @@ pub mod zaphkiel { + pub mod cpu_info; pub mod db; pub mod gamelog_join_leave; pub mod group_access_type; pub mod join_leave_event; pub mod macros; - // pub mod utils; - // pub mod cache; - pub mod cpu_info; pub mod vertex; pub mod world_instance; pub mod world_regions; diff --git a/src/zaphkiel/cache.rs b/src/zaphkiel/cache.rs deleted file mode 100644 index f879792..0000000 --- a/src/zaphkiel/cache.rs +++ /dev/null @@ -1,209 +0,0 @@ -use std::collections::HashMap; -use std::fmt::Debug; -use std::hash::{Hash, Hasher}; -use std::iter::FromIterator; -use std::ops::Index; -use std::sync::Arc; - -use serde::ser::SerializeMap; -use serde::{Serialize, Serializer}; - -pub type ArcStrSet = ArcStrMap; - -#[derive(Debug, Default)] -pub struct ArcStrMap { - map: Arc>, -} - -impl Hash for ArcStrMap -where - HashMap: Hash, - K: Hash, - V: Hash, -{ - fn hash(&self, state: &mut H) { - let map = &*self.map.clone(); - map.hash(state); - } -} - -impl Clone for ArcStrMap -where - K: Hash + PartialEq + Eq, -{ - fn clone(&self) -> Self { - let map = self.map.clone(); - Self { map } - } -} - -impl From<[(K, V); N]> for ArcStrMap -where - K: Eq + Hash + Into>, -{ - fn from(value: [(K, V); N]) -> Self { - let map = HashMap::from(value).into(); - Self { map } - } -} - -impl FromIterator<(K, V)> for ArcStrMap -where - K: Eq + Hash + Into>, -{ - fn from_iter>(iter: T) -> Self { - let map = HashMap::from_iter(iter).into(); - Self { map } - } -} - -impl Index<&K> for ArcStrMap -where - K: Eq + Hash + Into>, - Arc: std::borrow::Borrow, -{ - type Output = V; - - fn index(&self, index: &K) -> &Self::Output { - &self.map[index] - } -} - -impl ArcStrMap { - #[must_use] - pub fn new_empty_arc_str() -> Self { - let map = HashMap::new().into(); - Self { map } - } - - #[must_use] - pub fn get_map(&self) -> &HashMap { - &self.map - } -} - -impl ArcStrMap -where - K: Eq + Hash + Into> + Clone, - V: ToOwned + Clone, - HashMap: FromIterator<(::Owned, ::Owned)>, -{ - #[inline] - #[must_use] - #[allow(clippy::should_implement_trait)] - pub fn into_iter( - self, - ) -> impl IntoIterator::Owned, ::Owned)> { - let it = self - .map - .iter() - .map(|(k, v)| (k.to_owned(), v.to_owned())) - .collect::>(); - it.into_iter() - } -} - -impl ArcStrMap { - #[inline] - pub fn iter(&self) -> impl Iterator { - self.map.iter() - } -} - -impl ArcStrSet -where - K: Eq + PartialEq + Hash, -{ - #[inline] - pub fn insert_set(&mut self, query: impl Into) -> bool { - Arc::get_mut(&mut self.map).is_some_and(|it| it.insert(query.into(), ()).is_some()) - } - - #[inline] - #[must_use] - pub fn keys_set(&self) -> std::collections::hash_map::Keys<'_, K, ()> { - self.map.keys() - } -} - -impl ArcStrMap -where - K: Eq + PartialEq + Hash, -{ - #[inline] - #[must_use] - pub fn get(&self, query: &K) -> Option<&V> { - self.map.get(query) - } - - #[inline] - pub fn insert(&mut self, query: impl Into, value: V) -> Option { - Arc::get_mut(&mut self.map)?.insert(query.into(), value) - } - - #[inline] - #[must_use] - pub fn values(&self) -> std::collections::hash_map::Values<'_, K, V> { - self.map.values() - } - - #[inline] - #[must_use] - pub fn keys(&self) -> std::collections::hash_map::Keys<'_, K, V> { - self.map.keys() - } -} - -impl Serialize for ArcStrMap -where - K: Serialize + AsRef, - V: Serialize, - Arc: for<'a> From<&'a K>, - K: std::fmt::Debug, - V: std::fmt::Debug, -{ - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let map = self.iter().map(|(k, v)| (k.as_ref(), v)); - - let mut s = serializer.serialize_map(Some(self.len()))?; - - println!("map: {:?}", self); - - for (k, v) in map { - s.serialize_entry(k, v)?; - } - s.end() - } -} - -impl ArcStrMap { - #[must_use] - pub fn new() -> Self { - let map = Arc::new(HashMap::default()); - Self { map } - } - - #[must_use] - #[inline] - pub fn len(&self) -> usize { - self.map.len() - } - - #[inline] - #[must_use] - pub fn is_empty(&self) -> bool { - self.map.is_empty() - } -} - -// impl FromIterator<(KItem, VItem)> for ArcStrMap -// where -// KItem: Into, -// { -// fn from_iter>(iter: T) -> Self { -// todo!() -// } -// } diff --git a/src/zaphkiel/db.rs b/src/zaphkiel/db.rs index 2831f64..d05c2c7 100644 --- a/src/zaphkiel/db.rs +++ b/src/zaphkiel/db.rs @@ -4,6 +4,7 @@ use sqlx::{Sqlite, SqlitePool}; use crate::zaphkiel::cpu_info::CPU_THREADS; +#[allow(clippy::missing_panics_doc)] pub async fn establish_connection() -> SqlitePool { sqlx::pool::PoolOptions::::new() .acquire_timeout(Duration::from_secs(60 * 60)) diff --git a/src/zaphkiel/gamelog_join_leave.rs b/src/zaphkiel/gamelog_join_leave.rs index 13c500d..52f078e 100644 --- a/src/zaphkiel/gamelog_join_leave.rs +++ b/src/zaphkiel/gamelog_join_leave.rs @@ -19,6 +19,7 @@ use crate::zaphkiel::join_leave_event::JoinLeaveEvent; use crate::zaphkiel::world_instance::WorldInstance; #[derive(Debug, sqlx::FromRow, Clone)] +#[allow(clippy::module_name_repetitions)] // I want to keep the name ~kat pub struct GamelogJoinLeaveRow { pub id: i64, pub created_at: String, @@ -40,6 +41,12 @@ pub struct GamelogJoinLeave { pub time: Option, } +impl Default for GamelogJoinLeave { + fn default() -> Self { + Self::new() + } +} + impl GamelogJoinLeave { #[must_use] pub fn new() -> Self { @@ -58,6 +65,7 @@ impl GamelogJoinLeave { } } +#[allow(clippy::fallible_impl_from)] // we want it to fail when it's wrong impl From for GamelogJoinLeave { fn from(row: GamelogJoinLeaveRow) -> Self { let mut ret = Self::new(); @@ -65,18 +73,14 @@ impl From for GamelogJoinLeave { ret.created_at = row.created_at.parse().unwrap(); ret.event = row.r#type.parse().unwrap(); ret.display_name = row.display_name.into(); - ret.location = if let Ok(location) = row.location.parse() { - Some(location) - } else { - None - }; + ret.location = row.location.parse().ok(); ret.user_id = match row.user_id { x if x.is_empty() => None, _ => Some(row.user_id.into()), }; ret.time = match row.time { ..=0 => None, - _ => Some(row.time as u64), + _ => Some(row.time.try_into().unwrap()), }; ret diff --git a/src/zaphkiel/group_access_type.rs b/src/zaphkiel/group_access_type.rs index 7eb64fb..829ec4a 100644 --- a/src/zaphkiel/group_access_type.rs +++ b/src/zaphkiel/group_access_type.rs @@ -14,14 +14,15 @@ impl From<&str> for GroupAccessType { } } +#[allow(clippy::fallible_impl_from)] // we WANT it to fail if it's wrong impl From for GroupAccessType { fn from(value: String) -> Self { let value = value.to_lowercase(); match value.as_str() { - "public" => GroupAccessType::Public, - "plus" => GroupAccessType::Plus, - "members" => GroupAccessType::Members, - _ => panic!("Unknown group access type: {}", value), + "public" => Self::Public, + "plus" => Self::Plus, + "members" => Self::Members, + _ => panic!("Unknown group access type: {value}"), } } } diff --git a/src/zaphkiel/join_leave_event.rs b/src/zaphkiel/join_leave_event.rs index 807d92e..0b1be89 100644 --- a/src/zaphkiel/join_leave_event.rs +++ b/src/zaphkiel/join_leave_event.rs @@ -15,23 +15,16 @@ impl From<&str> for JoinLeaveEvent { } } +#[allow(clippy::fallible_impl_from)] // we want it to fail if it's wrong impl From for JoinLeaveEvent { fn from(value: String) -> Self { let value = value.to_lowercase(); match value.as_str() { - "join" => JoinLeaveEvent::Join, - "leave" => JoinLeaveEvent::Leave, + "join" | "joins" | "joined" | "onplayerjoined" => Self::Join, - "joins" => JoinLeaveEvent::Join, - "leaves" => JoinLeaveEvent::Leave, + "leave" | "leaves" | "left" | "onplayerleft" => Self::Leave, - "joined" => JoinLeaveEvent::Join, - "left" => JoinLeaveEvent::Leave, - - "onplayerjoined" => JoinLeaveEvent::Join, - "onplayerleft" => JoinLeaveEvent::Leave, - - _ => panic!("Unknown join/leave event: {}", value), + _ => panic!("Unknown join/leave event: {value}"), } } } diff --git a/src/zaphkiel/utils.rs b/src/zaphkiel/utils.rs deleted file mode 100644 index d6b12ea..0000000 --- a/src/zaphkiel/utils.rs +++ /dev/null @@ -1,17 +0,0 @@ -use indicatif::{ProgressBar, ProgressStyle}; - -pub fn get_pb(len: u64, msg: &'static str) -> ProgressBar { - let pb = ProgressBar::new(len); - - let pb_style = ProgressStyle::default_bar() - .template( - "{spinner:.green} [{elapsed}] {msg} [{wide_bar:.cyan/blue}] ({pos}/{len}|{percent}%) ({per_sec}|{eta})", - ) - .unwrap() - .progress_chars("#>-"); - pb.set_style(pb_style); - pb.set_message(msg); - pb.tick(); - - pb -} diff --git a/src/zaphkiel/vertex.rs b/src/zaphkiel/vertex.rs index d25e358..edbc987 100644 --- a/src/zaphkiel/vertex.rs +++ b/src/zaphkiel/vertex.rs @@ -14,8 +14,8 @@ impl Hash for Vertex { } impl Vertex { - pub fn new(name: impl ToString) -> Vertex { - Vertex { + pub fn new(name: &impl ToString) -> Self { + Self { user_id: name.to_string(), ..Default::default() } @@ -23,7 +23,7 @@ impl Vertex { } impl Vertex { - pub fn add(&mut self, other: Vertex) { + pub fn add(&mut self, other: Self) { let old = self.everyone_else.get(&other).unwrap_or(&0); self.everyone_else.insert(other, old + 1); } diff --git a/src/zaphkiel/world_instance.rs b/src/zaphkiel/world_instance.rs index 8407f04..96b5f0b 100644 --- a/src/zaphkiel/world_instance.rs +++ b/src/zaphkiel/world_instance.rs @@ -19,10 +19,12 @@ pub struct WorldInstance { impl WorldInstance { /// Create a new `WorldInstance` with default values as specified in the `Default` trait. #[allow(dead_code)] + #[must_use] pub fn new() -> Self { Self::default() } + #[must_use] pub fn get_prefix(&self) -> String { format!("{}:{}", self.world_id, self.instance_id) } @@ -39,6 +41,7 @@ impl WorldInstance { /// - `InvalidOptionalField`: The optional field is invalid. /// - `Other`: Other errors. #[derive(Debug, Clone, sqlx::Type, Default, PartialEq, Eq)] +#[allow(clippy::module_name_repetitions)] // I want it like that ~kat pub enum WorldInstanceParseError { Empty, InvalidFormat, @@ -92,7 +95,7 @@ impl FromStr for WorldInstance { "friends" => ret.friends = Some(value), "group" => ret.group = Some(value), "groupAccessType" => ret.group_access_type = Some(value.into()), - _ => panic!("Unknown key: {}, {part}", key), + _ => panic!("Unknown key: {key}, {part}"), } } @@ -100,12 +103,14 @@ impl FromStr for WorldInstance { } } +#[allow(clippy::fallible_impl_from)] // I want it like that ~kat impl From<&str> for WorldInstance { fn from(s: &str) -> Self { Self::from_str(s).unwrap() } } +#[allow(clippy::fallible_impl_from)] // I want it like that ~kat impl From for WorldInstance { fn from(s: String) -> Self { Self::from_str(&s).unwrap() @@ -122,7 +127,13 @@ mod tests { #[test] fn test_parse_world_instance() { let world_instance_str = "world_id:instance_id~region(EU)"; - let expected_world_instance = WorldInstance { + let expected_world_instance = world_instance_data(); + let actual_world_instance = WorldInstance::from_str(world_instance_str).unwrap(); + assert_eq!(actual_world_instance, expected_world_instance); + } + + fn world_instance_data() -> WorldInstance { + WorldInstance { world_id: "world_id".to_string(), instance_id: "instance_id".to_string(), nonce: None, @@ -132,9 +143,7 @@ mod tests { friends: None, group: None, group_access_type: None, - }; - let actual_world_instance = WorldInstance::from_str(world_instance_str).unwrap(); - assert_eq!(actual_world_instance, expected_world_instance); + } } #[test] @@ -171,7 +180,7 @@ mod tests { } #[test] - #[should_panic] + #[should_panic(expected = "unknown key in world instance string")] fn test_parse_world_instance_unknown_key() { let world_instance_str = "world_id:instance_id~unknown_key(value)"; let actual_result = WorldInstance::from_str(world_instance_str); @@ -189,36 +198,16 @@ mod tests { #[test] fn test_from_str_for_world_instance_from_string() { - let world_instance_str = "world_id:instance_id~region(US)"; - let expected_world_instance = WorldInstance { - world_id: "world_id".to_string(), - instance_id: "instance_id".to_string(), - nonce: None, - hidden: None, - private: None, - region: Some(Regions::US), - friends: None, - group: None, - group_access_type: None, - }; + let world_instance_str = "world_id:instance_id~region(EU)"; + let expected_world_instance = world_instance_data(); let actual_world_instance = WorldInstance::from(world_instance_str.to_string()); assert_eq!(actual_world_instance, expected_world_instance); } #[test] fn test_from_str_for_world_instance_from_str() { - let world_instance_str = "world_id:instance_id~region(US)"; - let expected_world_instance = WorldInstance { - world_id: "world_id".to_string(), - instance_id: "instance_id".to_string(), - nonce: None, - hidden: None, - private: None, - region: Some(Regions::US), - friends: None, - group: None, - group_access_type: None, - }; + let world_instance_str = "world_id:instance_id~region(EU)"; + let expected_world_instance = world_instance_data(); let actual_world_instance = WorldInstance::from(world_instance_str); assert_eq!(actual_world_instance, expected_world_instance); } diff --git a/src/zaphkiel/world_regions.rs b/src/zaphkiel/world_regions.rs index 574bb00..a1f5217 100644 --- a/src/zaphkiel/world_regions.rs +++ b/src/zaphkiel/world_regions.rs @@ -18,31 +18,18 @@ impl From<&str> for Regions { } } +#[allow(clippy::fallible_impl_from)] // ~kat impl From for Regions { fn from(value: String) -> Self { let value = value.to_lowercase(); match value.as_str() { - "uswest" => Regions::USWest, - "us" => Regions::US, - "useast" => Regions::USEast, - "europe" => Regions::Europe, - "japan" => Regions::Japan, + "uswest" | "usw" | "us w" | "us_w" | "uw" => Self::USWest, + "us" => Self::US, + "useast" | "use" | "us e" | "us_e" | "ue" => Self::USEast, + "europe" | "eu" => Self::Europe, + "japan" | "jp" => Self::Japan, - "usw" => Regions::USWest, - "use" => Regions::USEast, - "eu" => Regions::Europe, - "jp" => Regions::Japan, - - "us w" => Regions::USWest, - "us e" => Regions::USEast, - - "us_w" => Regions::USWest, - "us_e" => Regions::USEast, - - "uw" => Regions::USWest, - "ue" => Regions::USEast, - - _ => panic!("Unknown region: {}", value), + _ => panic!("Unknown region: {value}"), } } } @@ -50,7 +37,7 @@ impl From for Regions { impl FromStr for Regions { type Err = std::string::ParseError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { Ok(Self::from(s)) } }