use crate::MindMapError;
use std::collections::BTreeMap;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Theme {
pub name: String,
pub palette: Vec<String>,
variables: BTreeMap<String, String>,
}
impl Theme {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
..Self::default()
}
}
pub fn set_variable(
&mut self,
name: impl Into<String>,
value: impl Into<String>,
) -> Result<(), MindMapError> {
let name = name.into();
let value = value.into();
if !name.starts_with("--dme-")
|| name.len() <= 6
|| !name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
|| value.trim().is_empty()
|| value.contains([';', '{', '}'])
{
return Err(MindMapError::InvalidOperation(
"Expected a --dme-* variable and one CSS value".into(),
));
}
self.variables.insert(name, value);
Ok(())
}
pub fn variables(&self) -> &BTreeMap<String, String> {
&self.variables
}
pub(crate) fn css(&self) -> String {
self.variables
.iter()
.map(|(name, value)| format!("{name}:{value};"))
.collect()
}
}