#[derive(Debug, Clone, schemars::JsonSchema)]
pub struct JsonUiConfig {
pub tailwind_cdn: bool,
pub stylesheet_urls: Vec<String>,
pub custom_head: Option<String>,
pub body_class: String,
}
impl Default for JsonUiConfig {
fn default() -> Self {
Self {
tailwind_cdn: false,
stylesheet_urls: vec![format!(
"/_ferro/ferro-base.css?v={}",
env!("CARGO_PKG_VERSION")
)],
custom_head: None,
body_class: "bg-background text-text font-sans".to_string(),
}
}
}
impl JsonUiConfig {
pub fn new() -> Self {
Self::default()
}
pub fn tailwind_cdn(mut self, enabled: bool) -> Self {
self.tailwind_cdn = enabled;
self
}
pub fn stylesheet_urls(mut self, urls: Vec<String>) -> Self {
self.stylesheet_urls = urls;
self
}
pub fn custom_head(mut self, head: impl Into<String>) -> Self {
self.custom_head = Some(head.into());
self
}
pub fn body_class(mut self, class: impl Into<String>) -> Self {
self.body_class = class.into();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_has_tailwind_cdn_false_and_default_stylesheet_urls() {
let c = JsonUiConfig::default();
assert!(!c.tailwind_cdn, "tailwind_cdn default must be false");
assert_eq!(c.stylesheet_urls.len(), 1);
assert!(
c.stylesheet_urls[0].starts_with("/_ferro/ferro-base.css?v="),
"default stylesheet_urls must be versioned /_ferro/ferro-base.css?v=...; got {:?}",
c.stylesheet_urls
);
}
#[test]
fn stylesheet_urls_builder_replaces_entire_list() {
let c =
JsonUiConfig::new().stylesheet_urls(vec!["/a.css".to_string(), "/b.css".to_string()]);
assert_eq!(c.stylesheet_urls, vec!["/a.css", "/b.css"]);
assert!(
!c.stylesheet_urls
.contains(&"/_ferro/ferro-base.css".to_string()),
"builder must replace the default, not append"
);
}
#[test]
fn stylesheet_urls_builder_accepts_empty_vec() {
let c = JsonUiConfig::new().stylesheet_urls(vec![]);
assert!(c.stylesheet_urls.is_empty());
}
#[test]
fn json_schema_derives_with_new_field() {
let _schema = schemars::schema_for!(JsonUiConfig);
}
}