use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettingValues<T> {
pub current: T,
pub default: T,
pub effective: T,
}
impl<T> SettingValues<T> {
#[must_use]
pub const fn new(current: T, default: T, effective: T) -> Self {
Self {
current,
default,
effective,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SettingAvailability {
Available,
#[allow(dead_code)] Disabled {
reason: Cow<'static, str>,
},
}
impl SettingAvailability {
#[must_use]
pub fn is_available(&self) -> bool {
matches!(self, Self::Available)
}
#[must_use]
#[allow(dead_code)] pub fn disabled_reason(&self) -> Option<&str> {
match self {
Self::Available => None,
Self::Disabled { reason } => Some(reason.as_ref()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettingItemAction {
pub id: Cow<'static, str>,
pub label: Cow<'static, str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettingOption {
pub id: Cow<'static, str>,
pub label: Cow<'static, str>,
pub summary: Cow<'static, str>,
pub detail: Cow<'static, str>,
pub help: Cow<'static, str>,
pub values: SettingValues<Cow<'static, str>>,
pub availability: SettingAvailability,
pub tab: Cow<'static, str>,
pub action: Option<SettingItemAction>,
pub prefer_list_when_narrow: bool,
}
impl SettingOption {
#[must_use]
pub fn builder(
id: impl Into<Cow<'static, str>>,
label: impl Into<Cow<'static, str>>,
) -> SettingOptionBuilder {
SettingOptionBuilder {
id: id.into(),
label: label.into(),
summary: Cow::Borrowed(""),
detail: Cow::Borrowed(""),
help: Cow::Borrowed(""),
values: SettingValues::new(Cow::Borrowed(""), Cow::Borrowed(""), Cow::Borrowed("")),
availability: SettingAvailability::Available,
tab: Cow::Borrowed("all"),
action: None,
prefer_list_when_narrow: true,
}
}
}
#[derive(Debug, Clone)]
pub struct SettingOptionBuilder {
id: Cow<'static, str>,
label: Cow<'static, str>,
summary: Cow<'static, str>,
detail: Cow<'static, str>,
help: Cow<'static, str>,
values: SettingValues<Cow<'static, str>>,
availability: SettingAvailability,
tab: Cow<'static, str>,
action: Option<SettingItemAction>,
prefer_list_when_narrow: bool,
}
impl SettingOptionBuilder {
#[must_use]
pub fn summary(mut self, summary: impl Into<Cow<'static, str>>) -> Self {
self.summary = summary.into();
self
}
#[must_use]
pub fn detail(mut self, detail: impl Into<Cow<'static, str>>) -> Self {
self.detail = detail.into();
self
}
#[must_use]
pub fn help(mut self, help: impl Into<Cow<'static, str>>) -> Self {
self.help = help.into();
self
}
#[must_use]
pub fn values(mut self, values: SettingValues<Cow<'static, str>>) -> Self {
self.values = values;
self
}
#[must_use]
pub fn availability(mut self, availability: SettingAvailability) -> Self {
self.availability = availability;
self
}
#[must_use]
pub fn tab(mut self, tab: impl Into<Cow<'static, str>>) -> Self {
self.tab = tab.into();
self
}
#[must_use]
#[allow(dead_code)] pub fn action(mut self, action: SettingItemAction) -> Self {
self.action = Some(action);
self
}
#[must_use]
pub fn prefer_list_when_narrow(mut self, prefer: bool) -> Self {
self.prefer_list_when_narrow = prefer;
self
}
#[must_use]
pub fn build(self) -> SettingOption {
SettingOption {
id: self.id,
label: self.label,
summary: self.summary,
detail: self.detail,
help: self.help,
values: self.values,
availability: self.availability,
tab: self.tab,
action: self.action,
prefer_list_when_narrow: self.prefer_list_when_narrow,
}
}
}