implement user skeleton struct

master
Edward Shen 3 years ago
commit dd15b606b8
Signed by: edward
GPG Key ID: F350507060ED6C90

3
.gitignore vendored

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

@ -0,0 +1,9 @@
[package]
name = "libmatrix-client"
version = "0.1.0"
authors = ["Edward Shen <code@eddie.sh>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -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!")
}
}

@ -0,0 +1,6 @@
#![forbid(unsafe_code)]
mod api;
mod client;
mod room;
mod user;

@ -0,0 +1 @@
pub struct Room {}

@ -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<String>,
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<String>) -> Result<Self, UserInitError> {
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<String, ApiError> {
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()
})
)
}
}
Loading…
Cancel
Save