diff options
author | Rina Fujino <18257209+rina23q@users.noreply.github.com> | 2022-05-05 16:31:10 +0200 |
---|---|---|
committer | Rina Fujino <18257209+rina23q@users.noreply.github.com> | 2022-05-05 16:31:10 +0200 |
commit | 29473a8b9912be60bfdad22684d97301c2e80960 (patch) | |
tree | 5ca975cae51a0f77db707970fb9d0a1c3f1c512b /crates/core | |
parent | 16ddae45cea0c605aeb4e100cff9a855d12e839c (diff) |
Move the operation status message trait to the smartrest crate
Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
Diffstat (limited to 'crates/core')
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_serializer.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/crates/core/c8y_smartrest/src/smartrest_serializer.rs b/crates/core/c8y_smartrest/src/smartrest_serializer.rs index 5eabba04..503a345b 100644 --- a/crates/core/c8y_smartrest/src/smartrest_serializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_serializer.rs @@ -1,6 +1,8 @@ use crate::error::SmartRestSerializerError; +use crate::topic::C8yTopic; use agent_interface::{OperationStatus, SoftwareUpdateResponse}; use csv::{QuoteStyle, WriterBuilder}; +use mqtt_channel::Message; use serde::{Deserialize, Serialize, Serializer}; pub type SmartRest = String; @@ -209,6 +211,33 @@ fn serialize_smartrest<S: Serialize>(record: S) -> Result<String, SmartRestSeria Ok(csv) } +/// Helper to generate a SmartREST operation status message +pub trait TryIntoOperationStatusMessage { + fn executing() -> Result<Message, SmartRestSerializerError> { + let status = Self::status_executing()?; + Ok(Self::create_message(status)) + } + + fn successful(parameter: Option<String>) -> Result<Message, SmartRestSerializerError> { + let status = Self::status_successful(parameter)?; + Ok(Self::create_message(status)) + } + + fn failed(failure_reason: String) -> Result<Message, SmartRestSerializerError> { + let status = Self::status_failed(failure_reason)?; + Ok(Self::create_message(status)) + } + + fn create_message(payload: SmartRest) -> Message { + let topic = C8yTopic::SmartRestResponse.to_topic().unwrap(); // never fail + Message::new(&topic, payload) + } + + fn status_executing() -> Result<SmartRest, SmartRestSerializerError>; + fn status_successful(parameter: Option<String>) -> Result<SmartRest, SmartRestSerializerError>; + fn status_failed(failure_reason: String) -> Result<SmartRest, SmartRestSerializerError>; +} + #[cfg(test)] mod tests { use super::*; |