|
| 1 | +//! Renders [`build_modules`](crate::registry::build_modules)'s output as a |
| 2 | +//! directory tree of JSON files -- one directory per module, containing |
| 3 | +//! `module.json` plus one file per data type / function / runtime function -- |
| 4 | +//! using the same field names and camelCase wire format Aquila accepts, |
| 5 | +//! since `tucana::shared`'s `Serialize` impls already implement protobuf's |
| 6 | +//! canonical JSON mapping. Mirrors the layout the old `definitions/*.json` |
| 7 | +//! tree used, so this is primarily a debugging/parity-check tool: run it and |
| 8 | +//! diff the result against a known-good definitions dump. |
| 9 | +
|
| 10 | +use std::fs; |
| 11 | +use std::io; |
| 12 | +use std::path::Path; |
| 13 | + |
| 14 | +use serde::Serialize; |
| 15 | +use tucana::shared::{Module, Translation}; |
| 16 | + |
| 17 | +/// Writes one subdirectory per module under `root`, named by the module's |
| 18 | +/// identifier. |
| 19 | +pub fn write_all(modules: &[Module], root: &Path) -> io::Result<()> { |
| 20 | + for module in modules { |
| 21 | + write(module, &root.join(&module.identifier))?; |
| 22 | + } |
| 23 | + Ok(()) |
| 24 | +} |
| 25 | + |
| 26 | +fn write(module: &Module, dir: &Path) -> io::Result<()> { |
| 27 | + fs::create_dir_all(dir)?; |
| 28 | + write_json(&dir.join("module.json"), &Meta::from(module))?; |
| 29 | + |
| 30 | + write_each( |
| 31 | + &dir.join("data_types"), |
| 32 | + &module.definition_data_types, |
| 33 | + |dt| &dt.identifier, |
| 34 | + )?; |
| 35 | + write_each(&dir.join("functions"), &module.function_definitions, |f| { |
| 36 | + &f.runtime_name |
| 37 | + })?; |
| 38 | + write_each( |
| 39 | + &dir.join("runtime_functions"), |
| 40 | + &module.runtime_function_definitions, |
| 41 | + |f| &f.runtime_name, |
| 42 | + )?; |
| 43 | + |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Serialize)] |
| 48 | +struct Meta<'a> { |
| 49 | + identifier: &'a str, |
| 50 | + name: &'a [Translation], |
| 51 | + description: &'a [Translation], |
| 52 | + documentation: &'a str, |
| 53 | + author: &'a str, |
| 54 | + icon: &'a str, |
| 55 | + version: &'a str, |
| 56 | +} |
| 57 | + |
| 58 | +impl<'a> From<&'a Module> for Meta<'a> { |
| 59 | + fn from(module: &'a Module) -> Self { |
| 60 | + Self { |
| 61 | + identifier: &module.identifier, |
| 62 | + name: &module.name, |
| 63 | + description: &module.description, |
| 64 | + documentation: &module.documentation, |
| 65 | + author: &module.author, |
| 66 | + icon: &module.icon, |
| 67 | + version: &module.version, |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn write_each<T: Serialize>( |
| 73 | + dir: &Path, |
| 74 | + items: &[T], |
| 75 | + identifier: impl Fn(&T) -> &str, |
| 76 | +) -> io::Result<()> { |
| 77 | + if items.is_empty() { |
| 78 | + return Ok(()); |
| 79 | + } |
| 80 | + fs::create_dir_all(dir)?; |
| 81 | + for item in items { |
| 82 | + let file_name = identifier(item).replace("::", "_"); |
| 83 | + write_json(&dir.join(format!("{file_name}.json")), item)?; |
| 84 | + } |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +fn write_json<T: Serialize>(path: &Path, value: &T) -> io::Result<()> { |
| 89 | + let json = serde_json::to_string_pretty(value) |
| 90 | + .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 91 | + fs::write(path, json) |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use tucana::shared::{DefinitionDataType, Module, Translation}; |
| 97 | + |
| 98 | + use super::write_all; |
| 99 | + |
| 100 | + fn sample_modules() -> Vec<Module> { |
| 101 | + vec![Module { |
| 102 | + identifier: "example-module".into(), |
| 103 | + version: "0.1.0".into(), |
| 104 | + author: "code0-tech".into(), |
| 105 | + icon: "tabler:bolt".into(), |
| 106 | + documentation: "An example".into(), |
| 107 | + name: vec![Translation { |
| 108 | + code: "en-US".into(), |
| 109 | + content: "Example".into(), |
| 110 | + }], |
| 111 | + definition_data_types: vec![DefinitionDataType { |
| 112 | + identifier: "EMAIL".into(), |
| 113 | + r#type: "string".into(), |
| 114 | + version: "0.1.0".into(), |
| 115 | + ..Default::default() |
| 116 | + }], |
| 117 | + ..Default::default() |
| 118 | + }] |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn writes_one_directory_per_module_with_one_file_per_definition() { |
| 123 | + let dir = std::env::temp_dir().join(format!("taurus-export-test-{}", std::process::id())); |
| 124 | + write_all(&sample_modules(), &dir).expect("export succeeds"); |
| 125 | + |
| 126 | + let meta: serde_json::Value = serde_json::from_slice( |
| 127 | + &std::fs::read(dir.join("example-module").join("module.json")).unwrap(), |
| 128 | + ) |
| 129 | + .unwrap(); |
| 130 | + assert_eq!(meta["identifier"], "example-module"); |
| 131 | + |
| 132 | + let email: serde_json::Value = serde_json::from_slice( |
| 133 | + &std::fs::read( |
| 134 | + dir.join("example-module") |
| 135 | + .join("data_types") |
| 136 | + .join("EMAIL.json"), |
| 137 | + ) |
| 138 | + .unwrap(), |
| 139 | + ) |
| 140 | + .unwrap(); |
| 141 | + assert_eq!(email["identifier"], "EMAIL"); |
| 142 | + |
| 143 | + // No functions were registered, so that directory shouldn't exist. |
| 144 | + assert!(!dir.join("example-module").join("functions").exists()); |
| 145 | + |
| 146 | + std::fs::remove_dir_all(&dir).ok(); |
| 147 | + } |
| 148 | +} |
0 commit comments