From 5f3f41704036a7490c89c688c06738db8e993cf1 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Wed, 30 Sep 2020 10:59:35 -0600 Subject: [PATCH] Refactor out requests --- src/mqtt/{request.rs => request/mod.rs} | 55 +++---------------------- src/mqtt/request/run_section.rs | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 50 deletions(-) rename src/mqtt/{request.rs => request/mod.rs} (75%) create mode 100644 src/mqtt/request/run_section.rs diff --git a/src/mqtt/request.rs b/src/mqtt/request/mod.rs similarity index 75% rename from src/mqtt/request.rs rename to src/mqtt/request/mod.rs index 2aaccb3..929572c 100644 --- a/src/mqtt/request.rs +++ b/src/mqtt/request/mod.rs @@ -1,12 +1,11 @@ -use crate::{ - model::{SectionId, Sections}, - section_runner::SectionRunner, -}; -use eyre::WrapErr; +use crate::{model::Sections, section_runner::SectionRunner}; + use futures_util::FutureExt; use num_derive::FromPrimitive; use serde::{Deserialize, Serialize}; -use std::{fmt, future::Future, pin::Pin, time::Duration}; +use std::{fmt, future::Future, pin::Pin}; + +mod run_section; pub struct RequestContext { pub sections: Sections, @@ -164,50 +163,6 @@ trait IRequest { fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture; } -mod run_section { - use super::*; - use crate::section_runner::SectionRunHandle; - - #[derive(Debug, Deserialize, Serialize)] - #[serde(rename_all = "camelCase")] - pub struct RequestData { - pub section_id: SectionId, - #[serde(with = "crate::serde::duration")] - pub duration: Duration, - } - - #[derive(Debug, Deserialize, Serialize)] - #[serde(rename_all = "camelCase")] - pub struct ResponseData { - pub message: String, - pub run_id: SectionRunHandle, - } - - impl IRequest for RequestData { - fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture { - let mut section_runner = ctx.section_runner.clone(); - let section = ctx.sections.get(&self.section_id).cloned(); - let duration = self.duration; - Box::pin(async move { - let section = section.ok_or_else(|| { - RequestError::with_name(ErrorCode::NotFound, "section not found", "section") - })?; - let handle = section_runner - .queue_run(section.clone(), duration) - .await - .wrap_err("could not queue run")?; - let res = ResponseData { - message: format!("running section '{}' for {:?}", §ion.name, duration), - run_id: handle, - }; - let res_value = - serde_json::to_value(res).wrap_err("could not serialize response")?; - Ok(res_value) - }) - } - } -} - #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase", tag = "type")] pub enum Request { diff --git a/src/mqtt/request/run_section.rs b/src/mqtt/request/run_section.rs new file mode 100644 index 0000000..2ee2c8f --- /dev/null +++ b/src/mqtt/request/run_section.rs @@ -0,0 +1,43 @@ +use super::*; +use crate::{model::SectionId, section_runner::SectionRunHandle}; +use eyre::WrapErr; +use serde::{Deserialize, Serialize}; +use std::time::Duration; + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RequestData { + pub section_id: SectionId, + #[serde(with = "crate::serde::duration")] + pub duration: Duration, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ResponseData { + pub message: String, + pub run_id: SectionRunHandle, +} + +impl IRequest for RequestData { + fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture { + let mut section_runner = ctx.section_runner.clone(); + let section = ctx.sections.get(&self.section_id).cloned(); + let duration = self.duration; + Box::pin(async move { + let section = section.ok_or_else(|| { + RequestError::with_name(ErrorCode::NotFound, "section not found", "section") + })?; + let handle = section_runner + .queue_run(section.clone(), duration) + .await + .wrap_err("could not queue run")?; + let res = ResponseData { + message: format!("running section '{}' for {:?}", §ion.name, duration), + run_id: handle, + }; + let res_value = serde_json::to_value(res).wrap_err("could not serialize response")?; + Ok(res_value) + }) + } +}