Skip to main content

lefthk_core/config/command/
kill.rs

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