pub mod patch;
pub use patch::{Patch, PatchError};
use std::collections::BTreeMap;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pub const SPEC_VERSION: u32 = 1;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "A complete application description."))]
pub struct HostSpec {
pub version: u32,
pub window: WindowSpec,
pub theme: ThemeSpec,
pub commands: Vec<CommandSpec>,
pub menus: Vec<MenuSpec>,
pub explorer: Vec<TreeSpec>,
pub layout: LayoutSpec,
pub panels: PanelsSpec,
pub status: StatusSpec,
pub widgets: Vec<WidgetSpec>,
pub dialogs: Vec<DialogSpec>,
pub toasts: Vec<ToastSpec>,
pub metadata: BTreeMap<String, String>,
}
impl Default for HostSpec {
fn default() -> Self {
Self {
version: SPEC_VERSION,
window: WindowSpec::default(),
theme: ThemeSpec::default(),
commands: Vec::new(),
menus: Vec::new(),
explorer: Vec::new(),
layout: LayoutSpec::default(),
panels: PanelsSpec::default(),
status: StatusSpec::default(),
widgets: vec![WidgetSpec::Label {
text: "Hello from crisol-host".to_owned(),
size: 13.0,
tone: "default".to_owned(),
strong: false,
}],
dialogs: Vec::new(),
toasts: Vec::new(),
metadata: BTreeMap::new(),
}
}
}
impl HostSpec {
#[cfg(feature = "schema")]
pub fn schema() -> serde_json::Value {
serde_json::to_value(schemars::schema_for!(HostSpec)).unwrap_or(serde_json::Value::Null)
}
pub fn from_json(json: &str) -> Result<Self, String> {
let spec: Self = serde_json::from_str(json).map_err(|error| error.to_string())?;
if spec.version > SPEC_VERSION {
return Err(format!("unsupported spec version {}", spec.version));
}
Ok(spec)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "Window title and size."))]
pub struct WindowSpec {
pub title: String,
pub width: f32,
pub height: f32,
pub resizable: bool,
pub custom_title_bar: bool,
}
impl Default for WindowSpec {
fn default() -> Self {
Self {
title: "Workbench".to_owned(),
width: 980.0,
height: 720.0,
resizable: true,
custom_title_bar: true,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "Theme preferences."))]
pub struct ThemeSpec {
pub id: String,
pub follow_system: bool,
pub reduced_motion: bool,
}
impl Default for ThemeSpec {
fn default() -> Self {
Self {
id: "github-dark".to_owned(),
follow_system: false,
reduced_motion: false,
}
}
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One command."))]
pub struct CommandSpec {
pub id: String,
pub title: String,
pub category: String,
pub shortcut: Option<String>,
pub when: Option<String>,
pub enabled: bool,
pub checked: bool,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One top-level menu."))]
pub struct MenuSpec {
pub title: String,
pub items: Vec<MenuItemSpec>,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One menu row."))]
pub struct MenuItemSpec {
pub label: String,
pub command: Option<String>,
pub separator: bool,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One explorer node."))]
pub struct TreeSpec {
pub key: String,
pub label: String,
pub icon: Option<String>,
pub expanded: bool,
pub children: Vec<TreeSpec>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "Editor groups and tabs."))]
pub struct LayoutSpec {
pub groups: Vec<GroupSpec>,
pub active: usize,
pub direction: String,
}
impl Default for LayoutSpec {
fn default() -> Self {
Self {
groups: vec![GroupSpec::default()],
active: 0,
direction: "horizontal".to_owned(),
}
}
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One editor group."))]
pub struct GroupSpec {
pub id: String,
pub tabs: Vec<TabSpec>,
pub active: usize,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "One editor tab."))]
pub struct TabSpec {
pub id: String,
pub title: String,
pub pinned: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "Visible panels."))]
pub struct PanelsSpec {
pub sidebar: bool,
pub inspector: bool,
pub bottom: bool,
pub status: bool,
}
impl Default for PanelsSpec {
fn default() -> Self {
Self {
sidebar: true,
inspector: false,
bottom: false,
status: true,
}
}
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "Status bar items."))]
pub struct StatusSpec {
pub left: Vec<String>,
pub right: Vec<String>,
}
mod widget;
pub use widget::WidgetSpec;
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "A plot series in a spec."))]
pub struct SeriesSpec {
pub name: String,
pub style: String,
pub points: Vec<[f64; 2]>,
pub dataset: Option<String>,
pub x: Option<String>,
pub y: Option<String>,
pub color: Option<String>,
pub width: Option<f32>,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "A dialog declaration."))]
pub struct DialogSpec {
pub id: String,
pub title: String,
pub message: String,
pub confirm: String,
pub cancel: String,
pub open: bool,
}
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
#[cfg_attr(feature = "schema", schemars(description = "A toast declaration."))]
pub struct ToastSpec {
pub kind: String,
pub title: String,
pub description: Option<String>,
}