use serde::Serialize;
use crate::model;
pub const MONO: &[u8] = include_bytes!("../fonts/IBMPlexMono-Regular.ttf");
pub const FORMATS: [&str; 3] = ["svg", "pdf", "png"];
pub const DEFAULT_FORMATS: [&str; 2] = ["svg", "pdf"];
pub use crate::png::DPI;
pub type Files = Vec<(String, Vec<u8>)>;
pub const OPERATIONS: [&str; 10] = [
"analyse",
"plan",
"edit",
"render",
"review",
"spec",
"parts",
"pack",
"spec_schema",
"item_options",
];
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Error {
pub code: &'static str,
pub message: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub choices: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(skip)]
pub(crate) also: Vec<String>,
}
impl Error {
pub fn invalid(message: impl Into<String>) -> Error {
Error {
code: "invalid",
message: message.into(),
choices: Vec::new(),
path: None,
also: Vec::new(),
}
}
pub fn failed(message: impl Into<String>) -> Error {
Error {
code: "failed",
message: message.into(),
choices: Vec::new(),
path: None,
also: Vec::new(),
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)?;
if !self.choices.is_empty() {
write!(f, " Choose one of: {}.", self.choices.join(", "))?;
}
Ok(())
}
}
impl From<model::Unanalysed> for Error {
fn from(e: model::Unanalysed) -> Error {
if e.failed {
Error::failed(e.message)
} else {
Error::invalid(e.message)
}
}
}
impl From<model::Invalid> for Error {
fn from(e: model::Invalid) -> Error {
Error {
code: "invalid",
message: e.message,
choices: e.choices,
path: e.path,
also: e.also,
}
}
}
pub fn spec_ops(before: &serde_json::Value, after: &serde_json::Value) -> Vec<crate::items::Op> {
let mut ops = Vec::new();
spec_diff(before, after, "", &mut ops);
ops
}
fn spec_diff(
before: &serde_json::Value,
after: &serde_json::Value,
path: &str,
out: &mut Vec<crate::items::Op>,
) {
use serde_json::Value;
if before == after {
return;
}
match (before, after) {
(_, Value::Null) => out.push(crate::items::Op::remove(path)),
(b, Value::Object(a)) if !b.is_object() && (a.is_empty() || !b.is_null()) => {
out.push(crate::items::Op::set(path, after.clone()))
}
(b, Value::Object(a)) => {
let empty = serde_json::Map::new();
let b = b.as_object().unwrap_or(&empty);
let mut keys: Vec<&String> = b.keys().chain(a.keys()).collect();
keys.sort();
keys.dedup();
for k in keys {
if k == "schema" && path.is_empty() {
continue;
}
let child = match path {
"" => crate::items::key(k),
_ => format!("{path}/{}", crate::items::key(k)),
};
spec_diff(
b.get(k).unwrap_or(&Value::Null),
a.get(k).unwrap_or(&Value::Null),
&child,
out,
);
}
}
_ => out.push(crate::items::Op::set(path, after.clone())),
}
}
#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use native::selected_part;
#[cfg(not(target_arch = "wasm32"))]
pub use native::*;
#[cfg(target_arch = "wasm32")]
pub fn plan_analysed(analysis: &model::Analysis, spec: &model::Spec) -> Result<model::Plan, Error> {
Ok(model::plan_recovered(analysis, spec)?)
}