reactive_graph_utils_schema_generator/
json_schema.rs

1use anyhow::Result;
2use anyhow::anyhow;
3use reactive_graph_instance_system_json_schema::schema_entity_instances;
4use reactive_graph_instance_system_json_schema::schema_flow_instances;
5use reactive_graph_instance_system_json_schema::schema_relation_instances;
6use reactive_graph_type_system_json_schema::schema_components;
7use reactive_graph_type_system_json_schema::schema_entity_types;
8use reactive_graph_type_system_json_schema::schema_flow_types;
9use reactive_graph_type_system_json_schema::schema_relation_types;
10use serde_json::to_string_pretty;
11use std::fs::write;
12use std::path::Path;
13use std::path::PathBuf;
14use std::process::exit;
15use workspace_root::get_workspace_root;
16
17#[derive(Debug, Clone)]
18pub enum JsonSchemaTypes {
19    Component,
20    EntityType,
21    RelationType,
22    FlowType,
23    EntityInstance,
24    RelationInstance,
25    FlowInstance,
26}
27
28impl JsonSchemaTypes {
29    pub fn schema_path(&self) -> PathBuf {
30        match self {
31            JsonSchemaTypes::Component => Path::new("schema/json/component.schema.json"),
32            JsonSchemaTypes::EntityType => Path::new("schema/json/entity-type.schema.json"),
33            JsonSchemaTypes::RelationType => Path::new("schema/json/relation-type.schema.json"),
34            JsonSchemaTypes::FlowType => Path::new("schema/json/flow-type.schema.json"),
35            JsonSchemaTypes::EntityInstance => Path::new("schema/json/entity-instance.schema.json"),
36            JsonSchemaTypes::RelationInstance => Path::new("schema/json/relation-instance.schema.json"),
37            JsonSchemaTypes::FlowInstance => Path::new("schema/json/flow-instance.schema.json"),
38        }
39        .to_owned()
40    }
41}
42
43pub fn generate_json_schema(schema_type: &JsonSchemaTypes) -> String {
44    let json_schema = match schema_type {
45        JsonSchemaTypes::Component => schema_components(),
46        JsonSchemaTypes::EntityType => schema_entity_types(),
47        JsonSchemaTypes::RelationType => schema_relation_types(),
48        JsonSchemaTypes::FlowType => schema_flow_types(),
49        JsonSchemaTypes::EntityInstance => schema_entity_instances(),
50        JsonSchemaTypes::RelationInstance => schema_relation_instances(),
51        JsonSchemaTypes::FlowInstance => schema_flow_instances(),
52    };
53    match to_string_pretty(&json_schema.clone().to_value()) {
54        Ok(json_schema) => json_schema,
55        Err(_) => json_schema.to_value().to_string(),
56    }
57}
58
59pub fn write_json_schema(schema_type: JsonSchemaTypes) -> Result<()> {
60    let schema_path = get_workspace_root().join(schema_type.schema_path());
61    if !schema_path.exists() {
62        eprintln!("Schema path doesn't exist: {:?}", schema_path.display());
63        exit(1);
64    }
65    write(schema_path.clone(), generate_json_schema(&schema_type))
66        .map_err(|_| anyhow!("Failed to write JSON schema {:?} to {:?}", schema_type, schema_path))?;
67    Ok(())
68}
69
70#[cfg(test)]
71mod tests {
72    use super::JsonSchemaTypes;
73    use super::generate_json_schema;
74
75    #[test]
76    fn test_generate_json_schemas() {
77        assert!(generate_json_schema(&JsonSchemaTypes::Component).len() > 0);
78        assert!(generate_json_schema(&JsonSchemaTypes::EntityType).len() > 0);
79        assert!(generate_json_schema(&JsonSchemaTypes::RelationType).len() > 0);
80        assert!(generate_json_schema(&JsonSchemaTypes::FlowType).len() > 0);
81        assert!(generate_json_schema(&JsonSchemaTypes::EntityInstance).len() > 0);
82        assert!(generate_json_schema(&JsonSchemaTypes::RelationInstance).len() > 0);
83        assert!(generate_json_schema(&JsonSchemaTypes::FlowInstance).len() > 0);
84    }
85}