mod generated;
pub use generated::names;
use crate::formats::TmuxText;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OptionKind {
Flag,
Number,
Choice,
Text,
Colour,
Key,
Command,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OptionScope {
Server,
Session,
Window,
Pane,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OptionSchema {
name: &'static str,
kind: OptionKind,
scope: OptionScope,
}
impl OptionSchema {
pub(crate) const fn new(name: &'static str, kind: OptionKind, scope: OptionScope) -> Self {
Self { name, kind, scope }
}
#[must_use]
pub const fn name(&self) -> &'static str {
self.name
}
#[must_use]
pub const fn kind(&self) -> OptionKind {
self.kind
}
#[must_use]
pub const fn scope(&self) -> OptionScope {
self.scope
}
}
#[must_use]
pub fn option_schema(name: &str) -> Option<&'static OptionSchema> {
let name = name.split_once('[').map_or(name, |(base, _)| base);
generated::OPTION_SCHEMA
.binary_search_by(|entry| entry.name.cmp(name))
.ok()
.map(|index| &generated::OPTION_SCHEMA[index])
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OptionValue {
Flag(bool),
Number(i64),
Text(TmuxText),
}
impl OptionValue {
pub(crate) fn decode(name: &str, value: TmuxText) -> Self {
match option_schema(name).map(OptionSchema::kind) {
Some(OptionKind::Flag) => value
.as_flag()
.map_or_else(|| Self::Text(value.clone()), Self::Flag),
Some(OptionKind::Number) => value
.parse::<i64>()
.map_or_else(|| Self::Text(value.clone()), Self::Number),
_ => Self::Text(value),
}
}
}