commit dd15b606b85c7d4129a29fbbfc2e740dbf9128e3 Author: Edward Shen Date: Sun Aug 18 23:29:52 2019 -0400 implement user skeleton struct diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e48c706 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "libmatrix-client" +version = "0.1.0" +authors = ["Edward Shen "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..5a9b2cf --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,19 @@ +use std::fmt; + +#[derive(Debug, PartialEq)] +pub struct Client {} + +impl Client{ + pub fn new() -> Self { + Client {} + } +} + +#[derive(Default)] +pub struct ApiError {} + +impl fmt::Display for ApiError { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "an error occurred!") + } +} diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..579256c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +#![forbid(unsafe_code)] + +mod api; +mod client; +mod room; +mod user; diff --git a/src/room/mod.rs b/src/room/mod.rs new file mode 100644 index 0000000..8be9529 --- /dev/null +++ b/src/room/mod.rs @@ -0,0 +1 @@ +pub struct Room {} diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..8953b1d --- /dev/null +++ b/src/user.rs @@ -0,0 +1,120 @@ +use crate::api::{ApiError, Client}; +use crate::room::Room; +use std::fmt; + +#[derive(Debug, PartialEq, Eq)] +enum UserInitErrorReason { + InvalidUsername, + NoDomainProvided, +} + +#[derive(Debug, PartialEq)] +struct UserInitError { + message: String, + reason: UserInitErrorReason, +} + +impl UserInitError { + fn new(msg: &str, reason: UserInitErrorReason) -> Self { + UserInitError { + message: msg.to_string(), + reason, + } + } +} + +impl fmt::Display for UserInitError { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "{}", self.message) + } +} + +#[derive(Debug, PartialEq)] +struct User { + id: String, + display_name: Option, + client: Client, +} + +impl User { + /// Constructs a new User. This represents a user in a room. + /// + /// # Arguments + /// - `id` - The fully qualified user id, following the form @user:domain. Currently, this only + /// checks whether the id starts with `@` and contains a `:`. + /// - `display_name` - Some display name to use??? TODO: Figure out what this does + /// + /// Returns either a new User struct or an error containing the reason why it failed to create a + /// new User. + pub fn new(id: String, display_name: Option) -> Result { + if !id.starts_with("@") { + Err(UserInitError::new( + "User ID must start with a @", + UserInitErrorReason::InvalidUsername, + )) + } else if !id.contains(":") { + Err(UserInitError::new( + "User ID must contain a :", + UserInitErrorReason::NoDomainProvided, + )) + } else { + Ok(User { + id, + display_name, + client: Client::new(), + }) + } + } + + fn get_display_name(room: Room) -> String { + unimplemented!() + } + + fn set_display_name(&mut self, name: &str) -> Result<(), ApiError> { + unimplemented!() + } + + fn get_avatar_url() -> Result { + unimplemented!() + } + + fn set_avatar_url(url: &str) -> Result<(), ApiError> { + unimplemented!() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_returns_err_on_invalid_id() { + assert_eq!( + User::new(String::from("abc:edf"), None), + Err(UserInitError { + message: "User ID must start with a @".to_string(), + reason: UserInitErrorReason::InvalidUsername + }) + ); + + assert_eq!( + User::new(String::from("@abcedf"), None), + Err(UserInitError { + message: "User ID must contain a :".to_string(), + reason: UserInitErrorReason::NoDomainProvided + }) + ); + } + + #[test] + fn new_returns_struct_on_valid_input() { + assert_eq!( + User::new("@eddie:eddie.sh".to_string(), None), + Ok(User { + id: "@eddie:eddie.sh".to_string(), + display_name: None, + client: Client::new() + }) + ) + } +}