From e4832b4cf36ba0eaed298ee458498eddd7176590 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 11 Feb 2024 21:17:58 +0100 Subject: fix clippy --- src/routes/device.rs | 26 +++++++++---------- src/routes/start.rs | 72 +++++++++++++++++++++++++++++++++++----------------- src/routes/status.rs | 2 +- 3 files changed, 63 insertions(+), 37 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index a3308d4..b80cb85 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -7,12 +7,12 @@ use serde_json::{json, Value}; use tracing::{debug, info}; use crate::auth::auth; use crate::db::Device; -use crate::error::WebolError; +use crate::error::Error; -pub async fn get_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { +pub async fn get(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("add device {}", payload.id); let secret = headers.get("authorization"); - if auth(secret).map_err(WebolError::Auth)? { + if auth(secret).map_err(Error::Auth)? { let device = sqlx::query_as!( Device, r#" @@ -21,13 +21,13 @@ pub async fn get_device(State(state): State>, headers: Head WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(WebolError::DB)?; + ).fetch_one(&state.db).await.map_err(Error::DB)?; debug!("got device {:?}", device); Ok(Json(json!(device))) } else { - Err(WebolError::Generic) + Err(Error::Generic) } } @@ -36,10 +36,10 @@ pub struct GetDevicePayload { id: String, } -pub async fn put_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { +pub async fn put(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); let secret = headers.get("authorization"); - if auth(secret).map_err(WebolError::Auth)? { + if auth(secret).map_err(Error::Auth)? { sqlx::query!( r#" INSERT INTO devices (id, mac, broadcast_addr, ip) @@ -49,11 +49,11 @@ pub async fn put_device(State(state): State>, headers: Head payload.mac, payload.broadcast_addr, payload.ip - ).execute(&state.db).await.map_err(WebolError::DB)?; + ).execute(&state.db).await.map_err(Error::DB)?; Ok(Json(json!(PutDeviceResponse { success: true }))) } else { - Err(WebolError::Generic) + Err(Error::Generic) } } @@ -70,10 +70,10 @@ pub struct PutDeviceResponse { success: bool } -pub async fn post_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { +pub async fn post(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); let secret = headers.get("authorization"); - if auth(secret).map_err(WebolError::Auth)? { + if auth(secret).map_err(Error::Auth)? { let device = sqlx::query_as!( Device, r#" @@ -85,11 +85,11 @@ pub async fn post_device(State(state): State>, headers: Hea payload.broadcast_addr, payload.ip, payload.id - ).fetch_one(&state.db).await.map_err(WebolError::DB)?; + ).fetch_one(&state.db).await.map_err(Error::DB)?; Ok(Json(json!(device))) } else { - Err(WebolError::Generic) + Err(Error::Generic) } } diff --git a/src/routes/start.rs b/src/routes/start.rs index a206cbd..4264588 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -1,23 +1,27 @@ -use axum::Json; +use crate::auth::auth; +use crate::config::SETTINGS; +use crate::db::Device; +use crate::error::Error; +use crate::services::ping::Value as PingValue; +use crate::wol::{create_buffer, send_packet}; +use axum::extract::State; use axum::http::HeaderMap; +use axum::Json; use serde::{Deserialize, Serialize}; -use std::sync::Arc; -use axum::extract::State; use serde_json::{json, Value}; +use std::sync::Arc; use tracing::{debug, info}; use uuid::Uuid; -use crate::auth::auth; -use crate::config::SETTINGS; -use crate::wol::{create_buffer, send_packet}; -use crate::db::Device; -use crate::error::WebolError; -use crate::services::ping::PingValue; #[axum_macros::debug_handler] -pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { +pub async fn start( + State(state): State>, + headers: HeaderMap, + Json(payload): Json, +) -> Result, Error> { info!("POST request"); let secret = headers.get("authorization"); - let authorized = auth(secret).map_err(WebolError::Auth)?; + let authorized = auth(secret).map_err(Error::Auth)?; if authorized { let device = sqlx::query_as!( Device, @@ -27,7 +31,10 @@ pub async fn start(State(state): State>, headers: HeaderMap WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await.map_err(WebolError::DB)?; + ) + .fetch_one(&state.db) + .await + .map_err(Error::DB)?; info!("starting {}", device.id); @@ -36,9 +43,9 @@ pub async fn start(State(state): State>, headers: HeaderMap .unwrap_or("0.0.0.0:1111".to_string()); let _ = send_packet( - &bind_addr.parse().map_err(WebolError::IpParse)?, - &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, - create_buffer(&device.mac)? + &bind_addr.parse().map_err(Error::IpParse)?, + &device.broadcast_addr.parse().map_err(Error::IpParse)?, + &create_buffer(&device.mac)?, )?; let dev_id = device.id.clone(); let uuid = if payload.ping.is_some_and(|ping| ping) { @@ -49,7 +56,7 @@ pub async fn start(State(state): State>, headers: HeaderMap uuid = Some(key); break; } - }; + } let uuid_gen = match uuid { Some(u) => u, None => Uuid::new_v4().to_string(), @@ -58,26 +65,45 @@ pub async fn start(State(state): State>, headers: HeaderMap tokio::spawn(async move { debug!("init ping service"); - state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); + state.ping_map.insert( + uuid_gen.clone(), + PingValue { + ip: device.ip.clone(), + online: false, + }, + ); - crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await; + crate::services::ping::spawn( + state.ping_send.clone(), + device, + uuid_gen.clone(), + &state.ping_map, + &state.db, + ) + .await; }); Some(uuid_genc) - } else { None }; - Ok(Json(json!(StartResponse { id: dev_id, boot: true, uuid }))) + } else { + None + }; + Ok(Json(json!(Response { + id: dev_id, + boot: true, + uuid + }))) } else { - Err(WebolError::Generic) + Err(Error::Generic) } } #[derive(Deserialize)] -pub struct StartPayload { +pub struct Payload { id: String, ping: Option, } #[derive(Serialize)] -struct StartResponse { +struct Response { id: String, boot: bool, uuid: Option, diff --git a/src/routes/status.rs b/src/routes/status.rs index 45f3e51..31ef996 100644 --- a/src/routes/status.rs +++ b/src/routes/status.rs @@ -7,4 +7,4 @@ use crate::services::ping::status_websocket; #[axum_macros::debug_handler] pub async fn status(State(state): State>, ws: WebSocketUpgrade) -> Response { ws.on_upgrade(move |socket| status_websocket(socket, state)) -} \ No newline at end of file +} -- cgit v1.2.3