diff --git a/src/api/client_server/mod.rs b/src/api/client_server/mod.rs index dfe7033..49836a5 100644 --- a/src/api/client_server/mod.rs +++ b/src/api/client_server/mod.rs @@ -1,2 +1,2 @@ pub mod auth; -pub mod versions; \ No newline at end of file +pub mod versions; diff --git a/src/api/mod.rs b/src/api/mod.rs index 3abd782..8876ca2 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1 +1 @@ -pub mod client_server; \ No newline at end of file +pub mod client_server; diff --git a/src/main.rs b/src/main.rs index 8c82746..88101c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,19 +6,19 @@ use axum::{ body::Body, handler::Handler, http::{Request, StatusCode}, - Router, Extension, + Extension, Router, }; use tower_http::{ cors::CorsLayer, - trace::{DefaultOnRequest, TraceLayer, DefaultOnResponse}, + trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer}, }; use tracing::Level; mod api; -mod responses; -mod requests; -mod types; mod models; +mod requests; +mod responses; +mod types; struct Config { db_path: String, @@ -27,7 +27,10 @@ struct Config { impl Default for Config { fn default() -> Self { - Self { db_path: "sqlite://db.sqlite3".into(), homeserver_name: "fuckwit.dev".into() } + Self { + db_path: "sqlite://db.sqlite3".into(), + homeserver_name: "fuckwit.dev".into(), + } } } @@ -41,7 +44,9 @@ async fn main() { // init config let config = Arc::new(Config::default()); - let pool = sqlx::SqlitePool::connect("sqlite://db.sqlite3").await.unwrap(); + let pool = sqlx::SqlitePool::connect("sqlite://db.sqlite3") + .await + .unwrap(); let cors = CorsLayer::new() .allow_origin(tower_http::cors::Any) diff --git a/src/models/devices.rs b/src/models/devices.rs index ff12d2e..facfdfc 100644 --- a/src/models/devices.rs +++ b/src/models/devices.rs @@ -1,23 +1,34 @@ use sqlx::SqlitePool; -use super::{users::User, sessions::Session}; +use super::{sessions::Session, users::User}; pub struct Device { id: i64, user_id: i64, device_id: String, - display_name: String + display_name: String, } impl Device { - pub async fn create(conn: &SqlitePool, user: &User, device_id: &str, display_name: &str) -> anyhow::Result { + pub async fn create( + conn: &SqlitePool, + user: &User, + device_id: &str, + display_name: &str, + ) -> anyhow::Result { let user_id = user.id(); Ok(sqlx::query_as!(Self, "insert into devices(user_id, device_id, display_name) values(?, ?, ?) returning id, user_id, device_id, display_name", user_id, device_id, display_name).fetch_one(conn).await?) } pub async fn by_user(conn: &SqlitePool, user: &User) -> anyhow::Result { let user_id = user.id(); - Ok(sqlx::query_as!(Self, "select id, user_id, device_id, display_name from devices where user_id = ?", user_id).fetch_one(conn).await?) + Ok(sqlx::query_as!( + Self, + "select id, user_id, device_id, display_name from devices where user_id = ?", + user_id + ) + .fetch_one(conn) + .await?) } pub async fn create_session(&self, conn: &SqlitePool) -> anyhow::Result { diff --git a/src/models/mod.rs b/src/models/mod.rs index a28301e..113c92a 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,3 @@ -pub mod users; pub mod devices; pub mod sessions; +pub mod users; diff --git a/src/models/sessions.rs b/src/models/sessions.rs index cfed214..1e4c733 100644 --- a/src/models/sessions.rs +++ b/src/models/sessions.rs @@ -11,7 +11,14 @@ pub struct Session { impl Session { pub async fn create(conn: &SqlitePool, device: &Device, value: &str) -> anyhow::Result { let device_id = device.id(); - Ok(sqlx::query_as!(Self, "insert into sessions(device_id, value) values(?, ?) returning id, device_id, value", device_id, value).fetch_one(conn).await?) + Ok(sqlx::query_as!( + Self, + "insert into sessions(device_id, value) values(?, ?) returning id, device_id, value", + device_id, + value + ) + .fetch_one(conn) + .await?) } /// Get the session's id. diff --git a/src/models/users.rs b/src/models/users.rs index 851d486..4e56a07 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -11,15 +11,31 @@ pub struct User { impl User { pub async fn exists(conn: &SqlitePool, user_id: &UserId) -> anyhow::Result { - Ok(sqlx::query!("select user_id from users where user_id = ?", user_id).fetch_optional(conn).await?.is_some()) + Ok( + sqlx::query!("select user_id from users where user_id = ?", user_id) + .fetch_optional(conn) + .await? + .is_some(), + ) } - pub async fn create(conn: &SqlitePool, user_id: &UserId, display_name: &str, password: &str) -> anyhow::Result { + pub async fn create( + conn: &SqlitePool, + user_id: &UserId, + display_name: &str, + password: &str, + ) -> anyhow::Result { Ok(sqlx::query_as!(Self, "insert into users(user_id, display_name, password) values (?, ?, ?) returning id, user_id, display_name, password", user_id, display_name, password).fetch_one(conn).await?) } pub async fn by_user_id(conn: &SqlitePool, user_id: &UserId) -> anyhow::Result { - Ok(sqlx::query_as!(Self, "select id, user_id, display_name, password from users where user_id = ?", user_id).fetch_one(conn).await?) + Ok(sqlx::query_as!( + Self, + "select id, user_id, display_name, password from users where user_id = ?", + user_id + ) + .fetch_one(conn) + .await?) } /// Get the user's id. diff --git a/src/requests/mod.rs b/src/requests/mod.rs index 9b90583..038a1c7 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -1 +1 @@ -pub mod registration; \ No newline at end of file +pub mod registration; diff --git a/src/requests/registration.rs b/src/requests/registration.rs index 1d7532e..f101e56 100644 --- a/src/requests/registration.rs +++ b/src/requests/registration.rs @@ -1,4 +1,4 @@ -use crate::types::{flow::Flow, identifier::Identifier, authentication_data::AuthenticationData}; +use crate::types::{authentication_data::AuthenticationData, flow::Flow, identifier::Identifier}; #[derive(Debug, serde::Deserialize)] pub struct RegistrationRequest { diff --git a/src/responses/flow.rs b/src/responses/flow.rs index f15957d..39493ae 100644 --- a/src/responses/flow.rs +++ b/src/responses/flow.rs @@ -3,7 +3,7 @@ use crate::types::flow::Flow; #[derive(Debug, Clone, serde::Serialize)] struct FlowWrapper { #[serde(rename = "type")] - _type: Flow + _type: Flow, } #[derive(Debug, Clone, serde::Serialize)] @@ -14,7 +14,9 @@ pub struct Flows { impl Flows { pub fn new() -> Self { Self { - flows: vec![FlowWrapper{ _type: Flow::Password }], + flows: vec![FlowWrapper { + _type: Flow::Password, + }], } } } diff --git a/src/responses/username_available.rs b/src/responses/username_available.rs index b5b9875..6ca7805 100644 --- a/src/responses/username_available.rs +++ b/src/responses/username_available.rs @@ -7,4 +7,4 @@ impl UsernameAvailable { pub fn new(available: bool) -> Self { Self { available } } -} \ No newline at end of file +} diff --git a/src/types/authentication_data.rs b/src/types/authentication_data.rs index a140ab7..ea29dde 100644 --- a/src/types/authentication_data.rs +++ b/src/types/authentication_data.rs @@ -3,7 +3,7 @@ use super::{flow::Flow, identifier::Identifier}; #[derive(Debug, Clone, serde::Deserialize)] #[serde(untagged)] pub enum AuthenticationData { - Password(AuthenticationPassword) + Password(AuthenticationPassword), } #[derive(Debug, Clone, serde::Deserialize)] @@ -12,7 +12,7 @@ pub struct AuthenticationPassword { _type: Flow, identifier: Identifier, password: String, - user: Option + user: Option, } impl AuthenticationPassword { @@ -33,4 +33,4 @@ impl AuthenticationPassword { pub fn user(&self) -> Option<&String> { self.user.as_ref() } -} \ No newline at end of file +} diff --git a/src/types/flow.rs b/src/types/flow.rs index 1098d6a..f935a43 100644 --- a/src/types/flow.rs +++ b/src/types/flow.rs @@ -41,4 +41,4 @@ impl<'de> serde::Deserialize<'de> for Flow { deserializer.deserialize_str(FlowVisitor {}) } -} \ No newline at end of file +} diff --git a/src/types/identifier.rs b/src/types/identifier.rs index 852ce13..f278b11 100644 --- a/src/types/identifier.rs +++ b/src/types/identifier.rs @@ -3,14 +3,14 @@ use super::identifier_type::IdentifierType; #[derive(Debug, Clone, serde::Deserialize)] #[serde(untagged)] pub enum Identifier { - User(IdentifierUser) + User(IdentifierUser), } #[derive(Debug, Clone, serde::Deserialize)] pub struct IdentifierUser { #[serde(rename = "type")] _type: IdentifierType, - user: Option + user: Option, } impl IdentifierUser { @@ -19,4 +19,4 @@ impl IdentifierUser { pub fn user(&self) -> Option<&String> { self.user.as_ref() } -} \ No newline at end of file +} diff --git a/src/types/identifier_type.rs b/src/types/identifier_type.rs index 114e6c8..20d79e2 100644 --- a/src/types/identifier_type.rs +++ b/src/types/identifier_type.rs @@ -41,4 +41,4 @@ impl<'de> serde::Deserialize<'de> for IdentifierType { deserializer.deserialize_str(IdentifierVisitor {}) } -} \ No newline at end of file +} diff --git a/src/types/matrix_user_id.rs b/src/types/matrix_user_id.rs index c225ec1..007315a 100644 --- a/src/types/matrix_user_id.rs +++ b/src/types/matrix_user_id.rs @@ -14,7 +14,9 @@ impl UserId { } fn is_valid(&self) -> anyhow::Result<()> { - (self.0.len() <= 255).then(|| ()).ok_or(UserIdError::TooLong(self.0.len()))?; + (self.0.len() <= 255) + .then(|| ()) + .ok_or(UserIdError::TooLong(self.0.len()))?; Ok(()) } @@ -31,5 +33,5 @@ pub enum UserIdError { #[error("UserId too long {0} (expected < 255)")] TooLong(usize), #[error("Invalid UserId given")] - Invalid -} \ No newline at end of file + Invalid, +} diff --git a/src/types/mod.rs b/src/types/mod.rs index 69d3d83..383d3cb 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -2,5 +2,5 @@ pub mod authentication_data; pub mod flow; pub mod identifier; pub mod identifier_type; +pub mod matrix_user_id; pub mod user_interactive_authorization; -pub mod matrix_user_id; \ No newline at end of file diff --git a/src/types/user_interactive_authorization.rs b/src/types/user_interactive_authorization.rs index 810c6f9..0f31776 100644 --- a/src/types/user_interactive_authorization.rs +++ b/src/types/user_interactive_authorization.rs @@ -5,16 +5,18 @@ pub struct UserInteractiveAuthorizationInfo { flows: Vec, completed: Vec, session: Option, - auth_error: Option + auth_error: Option, } impl UserInteractiveAuthorizationInfo { pub fn new() -> Self { Self { - flows: vec![FlowStage { stages: vec![Flow::Password]}], + flows: vec![FlowStage { + stages: vec![Flow::Password], + }], completed: vec![], session: None, - auth_error: None + auth_error: None, } } } @@ -22,4 +24,4 @@ impl UserInteractiveAuthorizationInfo { #[derive(Debug, serde::Serialize)] struct FlowStage { stages: Vec, -} \ No newline at end of file +}