fmt: run cargo fmt

This commit is contained in:
Patrick Michl 2022-04-24 22:05:39 +02:00
parent d17c3a202a
commit 9687490c8e
18 changed files with 84 additions and 39 deletions

View File

@ -6,19 +6,19 @@ use axum::{
body::Body, body::Body,
handler::Handler, handler::Handler,
http::{Request, StatusCode}, http::{Request, StatusCode},
Router, Extension, Extension, Router,
}; };
use tower_http::{ use tower_http::{
cors::CorsLayer, cors::CorsLayer,
trace::{DefaultOnRequest, TraceLayer, DefaultOnResponse}, trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer},
}; };
use tracing::Level; use tracing::Level;
mod api; mod api;
mod responses;
mod requests;
mod types;
mod models; mod models;
mod requests;
mod responses;
mod types;
struct Config { struct Config {
db_path: String, db_path: String,
@ -27,7 +27,10 @@ struct Config {
impl Default for Config { impl Default for Config {
fn default() -> Self { 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 // init config
let config = Arc::new(Config::default()); 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() let cors = CorsLayer::new()
.allow_origin(tower_http::cors::Any) .allow_origin(tower_http::cors::Any)

View File

@ -1,23 +1,34 @@
use sqlx::SqlitePool; use sqlx::SqlitePool;
use super::{users::User, sessions::Session}; use super::{sessions::Session, users::User};
pub struct Device { pub struct Device {
id: i64, id: i64,
user_id: i64, user_id: i64,
device_id: String, device_id: String,
display_name: String display_name: String,
} }
impl Device { impl Device {
pub async fn create(conn: &SqlitePool, user: &User, device_id: &str, display_name: &str) -> anyhow::Result<Self> { pub async fn create(
conn: &SqlitePool,
user: &User,
device_id: &str,
display_name: &str,
) -> anyhow::Result<Self> {
let user_id = user.id(); 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?) 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<Self> { pub async fn by_user(conn: &SqlitePool, user: &User) -> anyhow::Result<Self> {
let user_id = user.id(); 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<Session> { pub async fn create_session(&self, conn: &SqlitePool) -> anyhow::Result<Session> {

View File

@ -1,3 +1,3 @@
pub mod users;
pub mod devices; pub mod devices;
pub mod sessions; pub mod sessions;
pub mod users;

View File

@ -11,7 +11,14 @@ pub struct Session {
impl Session { impl Session {
pub async fn create(conn: &SqlitePool, device: &Device, value: &str) -> anyhow::Result<Self> { pub async fn create(conn: &SqlitePool, device: &Device, value: &str) -> anyhow::Result<Self> {
let device_id = device.id(); 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. /// Get the session's id.

View File

@ -11,15 +11,31 @@ pub struct User {
impl User { impl User {
pub async fn exists(conn: &SqlitePool, user_id: &UserId) -> anyhow::Result<bool> { pub async fn exists(conn: &SqlitePool, user_id: &UserId) -> anyhow::Result<bool> {
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<Self> { pub async fn create(
conn: &SqlitePool,
user_id: &UserId,
display_name: &str,
password: &str,
) -> anyhow::Result<Self> {
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?) 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<Self> { pub async fn by_user_id(conn: &SqlitePool, user_id: &UserId) -> anyhow::Result<Self> {
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. /// Get the user's id.

View File

@ -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)] #[derive(Debug, serde::Deserialize)]
pub struct RegistrationRequest { pub struct RegistrationRequest {

View File

@ -3,7 +3,7 @@ use crate::types::flow::Flow;
#[derive(Debug, Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
struct FlowWrapper { struct FlowWrapper {
#[serde(rename = "type")] #[serde(rename = "type")]
_type: Flow _type: Flow,
} }
#[derive(Debug, Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
@ -14,7 +14,9 @@ pub struct Flows {
impl Flows { impl Flows {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
flows: vec![FlowWrapper{ _type: Flow::Password }], flows: vec![FlowWrapper {
_type: Flow::Password,
}],
} }
} }
} }

View File

@ -3,7 +3,7 @@ use super::{flow::Flow, identifier::Identifier};
#[derive(Debug, Clone, serde::Deserialize)] #[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum AuthenticationData { pub enum AuthenticationData {
Password(AuthenticationPassword) Password(AuthenticationPassword),
} }
#[derive(Debug, Clone, serde::Deserialize)] #[derive(Debug, Clone, serde::Deserialize)]
@ -12,7 +12,7 @@ pub struct AuthenticationPassword {
_type: Flow, _type: Flow,
identifier: Identifier, identifier: Identifier,
password: String, password: String,
user: Option<String> user: Option<String>,
} }
impl AuthenticationPassword { impl AuthenticationPassword {

View File

@ -3,14 +3,14 @@ use super::identifier_type::IdentifierType;
#[derive(Debug, Clone, serde::Deserialize)] #[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum Identifier { pub enum Identifier {
User(IdentifierUser) User(IdentifierUser),
} }
#[derive(Debug, Clone, serde::Deserialize)] #[derive(Debug, Clone, serde::Deserialize)]
pub struct IdentifierUser { pub struct IdentifierUser {
#[serde(rename = "type")] #[serde(rename = "type")]
_type: IdentifierType, _type: IdentifierType,
user: Option<String> user: Option<String>,
} }
impl IdentifierUser { impl IdentifierUser {

View File

@ -14,7 +14,9 @@ impl UserId {
} }
fn is_valid(&self) -> anyhow::Result<()> { 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(()) Ok(())
} }
@ -31,5 +33,5 @@ pub enum UserIdError {
#[error("UserId too long {0} (expected < 255)")] #[error("UserId too long {0} (expected < 255)")]
TooLong(usize), TooLong(usize),
#[error("Invalid UserId given")] #[error("Invalid UserId given")]
Invalid Invalid,
} }

View File

@ -2,5 +2,5 @@ pub mod authentication_data;
pub mod flow; pub mod flow;
pub mod identifier; pub mod identifier;
pub mod identifier_type; pub mod identifier_type;
pub mod user_interactive_authorization;
pub mod matrix_user_id; pub mod matrix_user_id;
pub mod user_interactive_authorization;

View File

@ -5,16 +5,18 @@ pub struct UserInteractiveAuthorizationInfo {
flows: Vec<FlowStage>, flows: Vec<FlowStage>,
completed: Vec<Flow>, completed: Vec<Flow>,
session: Option<String>, session: Option<String>,
auth_error: Option<String> auth_error: Option<String>,
} }
impl UserInteractiveAuthorizationInfo { impl UserInteractiveAuthorizationInfo {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
flows: vec![FlowStage { stages: vec![Flow::Password]}], flows: vec![FlowStage {
stages: vec![Flow::Password],
}],
completed: vec![], completed: vec![],
session: None, session: None,
auth_error: None auth_error: None,
} }
} }
} }