use std::path::Path;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::results::ContentId;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DocFormat {
Json,
Toml,
}
impl DocFormat {
pub fn of(path: &Path) -> Option<DocFormat> {
match path.extension()?.to_str()? {
"json" => Some(DocFormat::Json),
"toml" => Some(DocFormat::Toml),
_ => None,
}
}
}
pub fn to_value(text: &str, format: DocFormat) -> Result<serde_json::Value, String> {
match format {
DocFormat::Json => serde_json::from_str(text).map_err(|e| e.to_string()),
DocFormat::Toml => {
let value: toml::Value = toml::from_str(text).map_err(|e| e.to_string())?;
serde_json::to_value(value).map_err(|e| e.to_string())
}
}
}
pub fn parse<T: DeserializeOwned>(text: &str, format: DocFormat) -> Result<T, String> {
serde_json::from_value(to_value(text, format)?).map_err(|e| e.to_string())
}
pub fn read<T: DeserializeOwned>(path: &Path) -> Result<T, String> {
let format = DocFormat::of(path).ok_or_else(|| format!("{}: a document is .json or .toml", path.display()))?;
let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
parse(&text, format).map_err(|e| format!("{}: {e}", path.display()))
}
pub fn render<T: Serialize>(value: &T, format: DocFormat) -> Result<String, String> {
match format {
DocFormat::Json => serde_json::to_string_pretty(value).map(|s| s + "\n").map_err(|e| e.to_string()),
DocFormat::Toml => toml::to_string_pretty(value).map_err(|e| e.to_string()),
}
}
pub fn write<T: Serialize>(path: &Path, value: &T) -> Result<(), String> {
let format = DocFormat::of(path).ok_or_else(|| format!("{}: a document is .json or .toml", path.display()))?;
std::fs::write(path, render(value, format)?).map_err(|e| format!("{}: {e}", path.display()))
}
pub fn content_id(text: &str, format: DocFormat) -> Result<ContentId, String> {
Ok(ContentId::of_json(&to_value(text, format)?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{OpSpec, Pipeline};
const PIPELINE_TOML: &str = r#"
version = 1
app = "lucem"
edges = [["image", "denoise.image"], ["denoise.image", "threshold.image"]]
[sources]
image = "image"
[nodes.denoise]
op = "lucem:denoise"
params = { sigma = 2.0 }
[nodes.threshold]
op = "lucem:threshold"
[outputs]
mask = "threshold.mask"
"#;
const PIPELINE_JSON: &str = r#"{"version": 1, "app": "lucem", "sources": {"image": "image"},
"nodes": {"denoise": {"op": "lucem:denoise", "params": {"sigma": 2.0}}, "threshold": {"op": "lucem:threshold"}},
"edges": [["image", "denoise.image"], ["denoise.image", "threshold.image"]],
"outputs": {"mask": "threshold.mask"}}"#;
#[test]
fn toml_and_json_read_into_the_same_document_with_the_same_id() {
let from_toml: Pipeline = parse(PIPELINE_TOML, DocFormat::Toml).unwrap();
let from_json: Pipeline = parse(PIPELINE_JSON, DocFormat::Json).unwrap();
assert_eq!(from_toml, from_json);
assert_eq!(
content_id(PIPELINE_TOML, DocFormat::Toml).unwrap(),
content_id(PIPELINE_JSON, DocFormat::Json).unwrap(),
"the format never changes an id"
);
}
#[test]
fn documents_write_and_read_back_in_either_format() {
let dir = std::env::temp_dir().join(format!("kitchen-contract-docs-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let p: Pipeline = parse(PIPELINE_JSON, DocFormat::Json).unwrap();
for name in ["p.json", "p.toml"] {
let path = dir.join(name);
write(&path, &p).unwrap();
assert_eq!(read::<Pipeline>(&path).unwrap(), p, "{name}");
}
let op: OpSpec = parse("id = \"x\"\ntitle = \"X\"\ninputs = []\noutputs = [{ name = \"o\", type = \"image\" }]\nsource = { from = \"builtin\" }\n", DocFormat::Toml).unwrap();
assert_eq!(op.version, 1);
assert!(read::<Pipeline>(&dir.join("p.yaml")).is_err(), "only JSON and TOML");
}
}