use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub enum KconfigState {
#[default]
NotFound,
NotSet,
Off,
Disabled,
On,
Module,
Enabled,
Text(String),
}
impl KconfigState {
pub fn check(&self, other: KconfigState) -> bool {
match self {
KconfigState::NotFound
| KconfigState::NotSet
| KconfigState::Off
| KconfigState::Disabled => {
other == KconfigState::NotFound
|| other == KconfigState::NotSet
|| other == KconfigState::Off
|| other == KconfigState::Disabled
}
KconfigState::On => other == KconfigState::On,
KconfigState::Module => other == KconfigState::Module,
KconfigState::Enabled => {
other == KconfigState::On
|| other == KconfigState::Module
|| other == KconfigState::Enabled
}
KconfigState::Text(t) => other == KconfigState::Text(t.clone()),
}
}
}
impl std::fmt::Display for KconfigState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let text: &str = match self {
KconfigState::NotFound => "NotFound",
KconfigState::NotSet => "NotSet",
KconfigState::Off => "Off",
KconfigState::Disabled => "Disabled (NotFound, NotSet, or Off)",
KconfigState::On => "On",
KconfigState::Module => "Module",
KconfigState::Enabled => "Enabled (On or Module)",
KconfigState::Text(t) => t,
};
write!(f, "{text}")
}
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct KconfigOption {
name: String,
state: KconfigState,
}
impl std::fmt::Display for KconfigOption {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.name, self.state)
}
}
impl KconfigOption {
pub fn new(name: &str, state: KconfigState) -> Self {
KconfigOption {
name: name.to_string(),
state,
}
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn state(&self) -> KconfigState {
self.state.clone()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn success_new() {
let test_array = [
("CONFIG_TEST_NOT_FOUND", KconfigState::NotFound),
("CONFIG_TEST_NOT_SET", KconfigState::NotSet),
("CONFIG_TEST_OFF", KconfigState::Off),
("CONFIG_TEST_DISABLED", KconfigState::Disabled),
("CONFIG_TEST_ON", KconfigState::On),
("CONFIG_TEST_MODULE", KconfigState::Module),
("CONFIG_TEST_ENABLED", KconfigState::Enabled),
("CONFIG_TEST_TEXT", KconfigState::Text("test".to_string())),
];
for (option, state) in test_array {
let kconfig_option = KconfigOption::new(option, state.clone());
assert_eq!(kconfig_option.name(), option);
assert_eq!(kconfig_option.state(), state);
insta::assert_snapshot!(kconfig_option);
}
}
}