Skip to main content

crisol_core/spec/
mod.rs

1//! The versioned application specification and its JSON Schema.
2//!
3//! ```
4//! use crisol_core::spec;
5//! ```
6
7pub mod patch;
8pub use patch::{Patch, PatchError};
9
10use std::collections::BTreeMap;
11
12#[cfg(feature = "schema")]
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15
16/// The specification version understood by this host.
17/// # Example
18///
19/// ```
20/// use crisol_core::spec::SPEC_VERSION;
21/// ```
22pub const SPEC_VERSION: u32 = 1;
23
24/// A complete application description.
25#[derive(Clone, Debug, Serialize, Deserialize)]
26#[cfg_attr(feature = "schema", derive(JsonSchema))]
27#[serde(default)]
28#[non_exhaustive]
29/// # Example
30///
31/// ```
32/// use crisol_core::spec::HostSpec;
33/// ```
34#[cfg_attr(feature = "schema", schemars(description = "A complete application description."))]
35pub struct HostSpec {
36    /// Specification version. A host rejects a newer version.
37    pub version: u32,
38    /// Window title and size.
39    pub window: WindowSpec,
40    /// Theme and motion preferences.
41    pub theme: ThemeSpec,
42    /// Commands, their shortcuts, and contexts.
43    pub commands: Vec<CommandSpec>,
44    /// Top-level menus.
45    pub menus: Vec<MenuSpec>,
46    /// Explorer tree.
47    pub explorer: Vec<TreeSpec>,
48    /// Editor groups and tabs.
49    pub layout: LayoutSpec,
50    /// Which side and bottom panels are visible.
51    pub panels: PanelsSpec,
52    /// Status bar items.
53    pub status: StatusSpec,
54    /// The content widget tree.
55    pub widgets: Vec<WidgetSpec>,
56    /// Dialogs declared by the spec.
57    pub dialogs: Vec<DialogSpec>,
58    /// Toasts queued on startup.
59    pub toasts: Vec<ToastSpec>,
60    /// Extra application metadata.
61    pub metadata: BTreeMap<String, String>,
62}
63
64impl Default for HostSpec {
65    fn default() -> Self {
66        Self {
67            version: SPEC_VERSION,
68            window: WindowSpec::default(),
69            theme: ThemeSpec::default(),
70            commands: Vec::new(),
71            menus: Vec::new(),
72            explorer: Vec::new(),
73            layout: LayoutSpec::default(),
74            panels: PanelsSpec::default(),
75            status: StatusSpec::default(),
76            widgets: vec![WidgetSpec::Label {
77                text: "Hello from crisol-host".to_owned(),
78                size: 13.0,
79                tone: "default".to_owned(),
80                strong: false,
81            }],
82            dialogs: Vec::new(),
83            toasts: Vec::new(),
84            metadata: BTreeMap::new(),
85        }
86    }
87}
88
89impl HostSpec {
90    /// The JSON Schema generated from these Rust types.
91    #[cfg(feature = "schema")]
92    pub fn schema() -> serde_json::Value {
93        serde_json::to_value(schemars::schema_for!(HostSpec)).unwrap_or(serde_json::Value::Null)
94    }
95
96    /// Parse a spec, rejecting a newer version.
97    ///
98    /// # Errors
99    /// When the JSON is invalid or the version is newer than [`SPEC_VERSION`].
100    pub fn from_json(json: &str) -> Result<Self, String> {
101        let spec: Self = serde_json::from_str(json).map_err(|error| error.to_string())?;
102        if spec.version > SPEC_VERSION {
103            return Err(format!("unsupported spec version {}", spec.version));
104        }
105        Ok(spec)
106    }
107}
108
109/// Window title and size.
110#[derive(Clone, Debug, Serialize, Deserialize)]
111#[cfg_attr(feature = "schema", derive(JsonSchema))]
112#[serde(default)]
113#[non_exhaustive]
114/// # Example
115///
116/// ```
117/// use crisol_core::spec::WindowSpec;
118/// ```
119#[cfg_attr(feature = "schema", schemars(description = "Window title and size."))]
120pub struct WindowSpec {
121    /// Window title.
122    pub title: String,
123    /// Initial width in points.
124    pub width: f32,
125    /// Initial height in points.
126    pub height: f32,
127    /// Allow resizing.
128    pub resizable: bool,
129    /// Use the workbench's undecorated title bar.
130    pub custom_title_bar: bool,
131}
132
133impl Default for WindowSpec {
134    fn default() -> Self {
135        Self {
136            title: "Workbench".to_owned(),
137            width: 980.0,
138            height: 720.0,
139            resizable: true,
140            custom_title_bar: true,
141        }
142    }
143}
144
145/// Theme preferences.
146#[derive(Clone, Debug, Serialize, Deserialize)]
147#[cfg_attr(feature = "schema", derive(JsonSchema))]
148#[serde(default)]
149#[non_exhaustive]
150/// # Example
151///
152/// ```
153/// use crisol_core::spec::ThemeSpec;
154/// ```
155#[cfg_attr(feature = "schema", schemars(description = "Theme preferences."))]
156pub struct ThemeSpec {
157    /// Theme id, such as `github-dark` or `dracula`.
158    pub id: String,
159    /// Follow the desktop light/dark preference.
160    pub follow_system: bool,
161    /// Disable animations.
162    pub reduced_motion: bool,
163}
164
165impl Default for ThemeSpec {
166    fn default() -> Self {
167        Self {
168            id: "github-dark".to_owned(),
169            follow_system: false,
170            reduced_motion: false,
171        }
172    }
173}
174
175/// One command.
176#[cfg_attr(feature = "schema", derive(JsonSchema))]
177#[derive(Clone, Debug, Default, Serialize, Deserialize)]
178#[serde(default)]
179#[non_exhaustive]
180/// # Example
181///
182/// ```
183/// use crisol_core::spec::CommandSpec;
184/// ```
185#[cfg_attr(feature = "schema", schemars(description = "One command."))]
186pub struct CommandSpec {
187    /// Stable namespaced id.
188    pub id: String,
189    /// Visible title.
190    pub title: String,
191    /// Menu category.
192    pub category: String,
193    /// Default shortcut, such as `Ctrl+S`.
194    pub shortcut: Option<String>,
195    /// A `when` expression.
196    pub when: Option<String>,
197    /// Enabled state.
198    pub enabled: bool,
199    /// Checked state.
200    pub checked: bool,
201}
202
203/// One top-level menu.
204#[cfg_attr(feature = "schema", derive(JsonSchema))]
205#[derive(Clone, Debug, Default, Serialize, Deserialize)]
206#[serde(default)]
207#[non_exhaustive]
208/// # Example
209///
210/// ```
211/// use crisol_core::spec::MenuSpec;
212/// ```
213#[cfg_attr(feature = "schema", schemars(description = "One top-level menu."))]
214pub struct MenuSpec {
215    /// Menu title.
216    pub title: String,
217    /// Rows.
218    pub items: Vec<MenuItemSpec>,
219}
220
221/// One menu row.
222#[cfg_attr(feature = "schema", derive(JsonSchema))]
223#[derive(Clone, Debug, Default, Serialize, Deserialize)]
224#[serde(default)]
225#[non_exhaustive]
226/// # Example
227///
228/// ```
229/// use crisol_core::spec::MenuItemSpec;
230/// ```
231#[cfg_attr(feature = "schema", schemars(description = "One menu row."))]
232pub struct MenuItemSpec {
233    /// Row label.
234    pub label: String,
235    /// Command id, when the row runs a command.
236    pub command: Option<String>,
237    /// Draw a separator instead.
238    pub separator: bool,
239}
240
241/// One explorer node.
242#[cfg_attr(feature = "schema", derive(JsonSchema))]
243#[derive(Clone, Debug, Default, Serialize, Deserialize)]
244#[serde(default)]
245#[non_exhaustive]
246#[cfg_attr(feature = "schema", schemars(description = "One explorer node."))]
247/// # Example
248///
249/// ```
250/// use crisol_core::spec::TreeSpec;
251/// ```
252pub struct TreeSpec {
253    /// Stable key.
254    pub key: String,
255    /// Visible label.
256    pub label: String,
257    /// Icon name.
258    pub icon: Option<String>,
259    /// Expanded state.
260    pub expanded: bool,
261    /// Children.
262    pub children: Vec<TreeSpec>,
263}
264
265/// Editor groups and tabs.
266#[derive(Clone, Debug, Serialize, Deserialize)]
267#[cfg_attr(feature = "schema", derive(JsonSchema))]
268#[serde(default)]
269#[non_exhaustive]
270/// # Example
271///
272/// ```
273/// use crisol_core::spec::LayoutSpec;
274/// ```
275#[cfg_attr(feature = "schema", schemars(description = "Editor groups and tabs."))]
276pub struct LayoutSpec {
277    /// Groups left to right.
278    pub groups: Vec<GroupSpec>,
279    /// Active group index.
280    pub active: usize,
281    /// Draw groups horizontally or vertically.
282    pub direction: String,
283}
284
285impl Default for LayoutSpec {
286    fn default() -> Self {
287        Self {
288            groups: vec![GroupSpec::default()],
289            active: 0,
290            direction: "horizontal".to_owned(),
291        }
292    }
293}
294
295/// One editor group.
296#[cfg_attr(feature = "schema", derive(JsonSchema))]
297#[derive(Clone, Debug, Default, Serialize, Deserialize)]
298#[serde(default)]
299#[non_exhaustive]
300/// # Example
301///
302/// ```
303/// use crisol_core::spec::GroupSpec;
304/// ```
305#[cfg_attr(feature = "schema", schemars(description = "One editor group."))]
306pub struct GroupSpec {
307    /// Stable id.
308    pub id: String,
309    /// Tabs in order.
310    pub tabs: Vec<TabSpec>,
311    /// Active tab index.
312    pub active: usize,
313}
314
315/// One editor tab.
316#[cfg_attr(feature = "schema", derive(JsonSchema))]
317#[derive(Clone, Debug, Default, Serialize, Deserialize)]
318#[serde(default)]
319#[non_exhaustive]
320#[cfg_attr(feature = "schema", schemars(description = "One editor tab."))]
321/// # Example
322///
323/// ```
324/// use crisol_core::spec::TabSpec;
325/// ```
326pub struct TabSpec {
327    /// Stable id.
328    pub id: String,
329    /// Visible title.
330    pub title: String,
331    /// Pinned tab.
332    pub pinned: bool,
333}
334
335/// Visible panels.
336#[derive(Clone, Debug, Serialize, Deserialize)]
337#[cfg_attr(feature = "schema", derive(JsonSchema))]
338#[serde(default)]
339#[non_exhaustive]
340/// # Example
341///
342/// ```
343/// use crisol_core::spec::PanelsSpec;
344/// ```
345#[cfg_attr(feature = "schema", schemars(description = "Visible panels."))]
346pub struct PanelsSpec {
347    /// Explorer.
348    pub sidebar: bool,
349    /// Right inspector.
350    pub inspector: bool,
351    /// Bottom panel.
352    pub bottom: bool,
353    /// Status bar.
354    pub status: bool,
355}
356
357impl Default for PanelsSpec {
358    fn default() -> Self {
359        Self {
360            sidebar: true,
361            inspector: false,
362            bottom: false,
363            status: true,
364        }
365    }
366}
367
368/// Status bar items.
369#[cfg_attr(feature = "schema", derive(JsonSchema))]
370#[derive(Clone, Debug, Default, Serialize, Deserialize)]
371#[serde(default)]
372#[non_exhaustive]
373/// # Example
374///
375/// ```
376/// use crisol_core::spec::StatusSpec;
377/// ```
378#[cfg_attr(feature = "schema", schemars(description = "Status bar items."))]
379pub struct StatusSpec {
380    /// Left-aligned items.
381    pub left: Vec<String>,
382    /// Right-aligned items.
383    pub right: Vec<String>,
384}
385
386/// One content widget.
387mod widget;
388pub use widget::WidgetSpec;
389
390/// A plot series in a spec.
391#[cfg_attr(feature = "schema", derive(JsonSchema))]
392#[derive(Clone, Debug, Default, Serialize, Deserialize)]
393#[serde(default)]
394#[non_exhaustive]
395/// # Example
396///
397/// ```
398/// use crisol_core::spec::SeriesSpec;
399/// ```
400#[cfg_attr(feature = "schema", schemars(description = "A plot series in a spec."))]
401pub struct SeriesSpec {
402    /// Series name.
403    pub name: String,
404    /// `line` or `scatter`.
405    pub style: String,
406    /// Inline points.
407    pub points: Vec<[f64; 2]>,
408    /// Dataset name, when the points come from a dataset.
409    pub dataset: Option<String>,
410    /// X column for a dataset series.
411    pub x: Option<String>,
412    /// Y column for a dataset series.
413    pub y: Option<String>,
414    /// Color as `#rrggbb`.
415    pub color: Option<String>,
416    /// Line width or marker size.
417    pub width: Option<f32>,
418}
419
420/// A dialog declaration.
421#[cfg_attr(feature = "schema", derive(JsonSchema))]
422#[derive(Clone, Debug, Default, Serialize, Deserialize)]
423#[serde(default)]
424#[non_exhaustive]
425/// # Example
426///
427/// ```
428/// use crisol_core::spec::DialogSpec;
429/// ```
430#[cfg_attr(feature = "schema", schemars(description = "A dialog declaration."))]
431pub struct DialogSpec {
432    /// Stable id.
433    pub id: String,
434    /// Title.
435    pub title: String,
436    /// Message.
437    pub message: String,
438    /// Confirm label.
439    pub confirm: String,
440    /// Cancel label.
441    pub cancel: String,
442    /// Whether it starts open.
443    pub open: bool,
444}
445
446/// A toast declaration.
447#[cfg_attr(feature = "schema", derive(JsonSchema))]
448#[derive(Clone, Debug, Default, Serialize, Deserialize)]
449#[serde(default)]
450#[non_exhaustive]
451/// # Example
452///
453/// ```
454/// use crisol_core::spec::ToastSpec;
455/// ```
456#[cfg_attr(feature = "schema", schemars(description = "A toast declaration."))]
457pub struct ToastSpec {
458    /// `info`, `success`, `warning`, `error`, or `loading`.
459    pub kind: String,
460    /// Title.
461    pub title: String,
462    /// Description.
463    pub description: Option<String>,
464}