Skip to main content

lefthk_core/config/command/
reload.rs

1use ron::ser::PrettyConfig;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    config::command::utils::denormalize_function::DenormalizeCommandFunction,
6    errors::Error,
7    worker::{self, Worker},
8};
9
10use super::{Command, NormalizedCommand};
11
12inventory::submit! {DenormalizeCommandFunction::new::<Reload>()}
13
14#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
15pub struct Reload;
16
17impl Reload {
18    #[must_use]
19    pub fn new() -> Self {
20        Self
21    }
22}
23
24impl Command for Reload {
25    fn normalize(&self) -> NormalizedCommand {
26        let serialized_string =
27            ron::ser::to_string_pretty(self, PrettyConfig::new().struct_names(true)).unwrap();
28        NormalizedCommand(serialized_string)
29    }
30
31    fn denormalize(generalized: &NormalizedCommand) -> Option<Box<Self>> {
32        ron::from_str(&generalized.0).ok()
33    }
34
35    fn execute(&self, worker: &mut Worker) -> Error {
36        worker.status = worker::Status::Reload;
37        Ok(())
38    }
39
40    fn get_name(&self) -> &'static str {
41        "Reload"
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use crate::config::Command;
48
49    use super::Reload;
50
51    #[test]
52    fn normalize_process() {
53        let command = Reload::new();
54
55        let normalized = command.normalize();
56        let denormalized = Reload::denormalize(&normalized).unwrap();
57
58        assert_eq!(
59            Box::new(command.clone()),
60            denormalized,
61            "{command:?}, {denormalized:?}",
62        );
63    }
64}