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/start.rs | 72 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 23 deletions(-) (limited to 'src/routes/start.rs') 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, -- cgit v1.2.3