//! Templates as the shipping templates route sees them (`apps/desktop/src/templates`): the //! live library, its sidebar ordering and the writes behind create, edit, favorite, default, //! duplicate and delete. use std::{cmp::Ordering, sync::Arc}; use desktop_runtime::{Reply, Result, RuntimeHandle}; use serde_json::{Value, json}; use uuid::Uuid; use super::{ mutations::{failure, statement, transaction}, resource_list::Glyph, }; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Section { pub title: Arc, pub description: Arc, } #[derive(Debug)] pub struct StoredTemplate { pub sections: Vec
, pub targets: Vec>, pub warnings: Vec>, pub raw_sections: Arc, pub raw_targets: Arc, } impl StoredTemplate { pub fn decode(raw_sections: Arc, raw_targets: Arc) -> Self { let sections = decode_sections(&raw_sections); let targets = decode_targets(&raw_targets); let mut warnings = Vec::new(); if let Err(error) = §ions { warnings.push(error.clone()); } if let Err(error) = &targets { warnings.push(error.clone()); } Self { sections: sections.unwrap_or_default(), targets: targets.unwrap_or_default(), warnings, raw_sections, raw_targets, } } } fn decode_sections(raw: &str) -> std::result::Result, Arc> { let value = super::mutations::decode_json_text(raw) .map_err(|error| Arc::::from(error.to_string()))?; let rows = value .as_array() .ok_or_else(|| Arc::from("Sections must be an array; stored data is preserved."))?; let mut sections = Vec::new(); for row in rows { if let Some(title) = row.as_str() { if !title.trim().is_empty() { sections.push(Section { title: title.trim().into(), description: "".into(), }); } } else { let title = row.get("title").and_then(Value::as_str).ok_or_else(|| { Arc::from("A stored section has an invalid title; stored data is preserved.") })?; let description = match row.get("description") { None => "", Some(Value::String(description)) => description, _ => return Err( "A stored section has an invalid description; stored data is preserved." .into(), ), }; sections.push(Section { title: title.trim().into(), description: description.into(), }); } } Ok(sections) } fn decode_targets(raw: &str) -> std::result::Result>, Arc> { if raw.is_empty() || raw == "null" { return Ok(Vec::new()); } let value: Value = serde_json::from_str(raw) .map_err(|error| Arc::from(format!("Invalid stored targets: {error}")))?; if let Some(target) = value.as_str() { return Ok(if target.trim().is_empty() { vec![] } else { vec![target.trim().into()] }); } let targets = value .as_array() .ok_or_else(|| Arc::from("Targets must be strings; stored data is preserved."))?; Ok(targets .iter() .filter_map(Value::as_str) .map(str::trim) .filter(|target| !target.is_empty()) .map(Into::into) .collect()) } /// Shipping stamps template rows with second precision. const NOW: &str = "strftime('%Y-%m-%dT%H:%M:%SZ','now')"; #[derive(Clone, Debug, PartialEq)] pub struct Template { pub id: Arc, pub title: Arc, pub description: Arc, pub pinned: bool, pub pin_order: Option, pub category: Arc, pub icon: Glyph, pub sections: Vec
, pub targets: Vec>, /// Stored sections or targets that could not be decoded; editing them is disabled so the /// original bytes survive. pub warnings: Vec>, } impl Template { pub fn display_title(&self) -> &str { match self.title.trim() { "" => "Untitled", title => title, } } } /// The templates in id order (shipping's live query) plus the two settings the route reads. #[derive(Clone, Debug, Default, PartialEq)] pub struct Library { pub templates: Vec