Skip to main content

sn0int_common/metadata/
stealth.rs

1use crate::errors::*;
2use clap::ValueEnum;
3use serde::{Deserialize, Serialize};
4use std::str::FromStr;
5
6#[derive(Debug, Eq, PartialEq, PartialOrd, Clone, ValueEnum, Serialize, Deserialize)]
7pub enum Stealth {
8    Loud,
9    Normal,
10    Passive,
11    Offline,
12}
13
14impl Stealth {
15    #[inline]
16    pub fn variants() -> &'static [&'static str] {
17        &["loud", "normal", "passive", "offline"]
18    }
19
20    #[inline(always)]
21    fn as_num(&self) -> u8 {
22        match self {
23            Stealth::Loud => 3,
24            Stealth::Normal => 2,
25            Stealth::Passive => 1,
26            Stealth::Offline => 0,
27        }
28    }
29
30    #[inline]
31    pub fn equal_or_better(&self, other: &Self) -> bool {
32        self.as_num() <= other.as_num()
33    }
34}
35
36impl FromStr for Stealth {
37    type Err = Error;
38
39    fn from_str(s: &str) -> Result<Stealth> {
40        match s {
41            "loud" => Ok(Stealth::Loud),
42            // This is also the default level if none is provided
43            "normal" => Ok(Stealth::Normal),
44            "passive" => Ok(Stealth::Passive),
45            "offline" => Ok(Stealth::Offline),
46            x => bail!("Unknown stealth: {:?}", x),
47        }
48    }
49}