use super::units::ParameterUnit;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParameterContext {
Internal,
Superuser,
User,
}
impl ParameterContext {
pub const fn name(self) -> &'static str {
match self {
Self::Internal => "internal",
Self::Superuser => "superuser",
Self::User => "user",
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct EnumOption {
pub name: &'static str,
pub value: u8,
pub hidden: bool,
}
impl EnumOption {
pub const fn listed(name: &'static str, value: u8) -> Self {
Self {
name,
value,
hidden: false,
}
}
pub const fn hidden(name: &'static str, value: u8) -> Self {
Self {
name,
value,
hidden: true,
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum ParameterKind {
Bool {
boot: bool,
},
Integer {
boot: i32,
min: i32,
max: i32,
unit: Option<ParameterUnit>,
},
Enum {
boot: u8,
options: &'static [EnumOption],
},
String {
boot: &'static str,
},
}
impl ParameterKind {
pub const fn type_name(&self) -> &'static str {
match self {
Self::Bool { .. } => "bool",
Self::Integer { .. } => "integer",
Self::Enum { .. } => "enum",
Self::String { .. } => "string",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ParameterFlags(u16);
impl ParameterFlags {
pub const NONE: Self = Self(0);
pub const LIST_INPUT: Self = Self(1 << 0);
pub const LIST_QUOTE: Self = Self(1 << 1);
pub const NO_SHOW_ALL: Self = Self(1 << 2);
pub const NO_RESET: Self = Self(1 << 3);
pub const NO_RESET_ALL: Self = Self(1 << 4);
pub const REPORT: Self = Self(1 << 5);
pub const IS_NAME: Self = Self(1 << 6);
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
}
#[derive(Clone, Copy, Debug)]
pub struct ParameterDefinition {
pub name: &'static str,
pub kind: ParameterKind,
pub context: ParameterContext,
pub category: &'static str,
pub short_desc: &'static str,
pub extra_desc: Option<&'static str>,
pub flags: ParameterFlags,
pub library: Option<&'static str>,
}
impl ParameterDefinition {
pub fn has_flag(&self, flag: ParameterFlags) -> bool {
self.flags.contains(flag)
}
pub fn boot_setting(&self) -> String {
match self.kind {
ParameterKind::Bool { boot } => if boot { "on" } else { "off" }.into(),
ParameterKind::Integer { boot, .. } => boot.to_string(),
ParameterKind::Enum { boot, options } => {
super::value::enum_display(options, boot).into()
}
ParameterKind::String { boot } => boot.into(),
}
}
pub fn unit(&self) -> Option<ParameterUnit> {
match self.kind {
ParameterKind::Integer { unit, .. } => unit,
_ => None,
}
}
}