|
| 1 | +use crate::reader::{MetaType, Reader}; |
| 2 | +use serde::Serialize; |
| 3 | +use tucana::shared::{DefinitionDataType, FlowType, RuntimeFunctionDefinition}; |
| 4 | + |
| 5 | +#[derive(Serialize, Clone, Debug)] |
| 6 | +pub struct DefinitionError { |
| 7 | + pub definition: String, |
| 8 | + pub definition_type: MetaType, |
| 9 | + pub error: String, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug)] |
| 13 | +pub struct Parser { |
| 14 | + pub features: Vec<Feature>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Serialize, Clone, Debug)] |
| 18 | +pub struct Feature { |
| 19 | + pub name: String, |
| 20 | + pub data_types: Vec<DefinitionDataType>, |
| 21 | + pub flow_types: Vec<FlowType>, |
| 22 | + pub runtime_functions: Vec<RuntimeFunctionDefinition>, |
| 23 | + pub errors: Vec<DefinitionError>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Feature { |
| 27 | + fn new(name: String) -> Self { |
| 28 | + Feature { |
| 29 | + name, |
| 30 | + data_types: Vec::new(), |
| 31 | + flow_types: Vec::new(), |
| 32 | + runtime_functions: Vec::new(), |
| 33 | + errors: Vec::new(), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl Parser { |
| 39 | + |
| 40 | + pub fn from_path(path: &str) -> Option<Self> { |
| 41 | + let reader = match Reader::from_path(path) { |
| 42 | + Some(reader) => reader, |
| 43 | + None => return None, |
| 44 | + }; |
| 45 | + |
| 46 | + Some(Self::from_reader(reader)) |
| 47 | + } |
| 48 | + |
| 49 | + pub fn from_reader(reader: Reader) -> Self { |
| 50 | + let mut features: Vec<Feature> = vec![]; |
| 51 | + |
| 52 | + for meta in &reader.meta { |
| 53 | + let feature = features.iter_mut().find(|f| f.name == meta.name); |
| 54 | + |
| 55 | + if let Some(existing) = feature { |
| 56 | + Parser::append_meta(existing, meta); |
| 57 | + } else { |
| 58 | + let mut new_feature = Feature::new(meta.name.clone()); |
| 59 | + Parser::append_meta(&mut new_feature, meta); |
| 60 | + features.push(new_feature); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Parser { features } |
| 65 | + } |
| 66 | + |
| 67 | + fn append_meta(feature: &mut Feature, meta: &crate::reader::Meta) { |
| 68 | + for definition in &meta.data { |
| 69 | + match meta.r#type { |
| 70 | + MetaType::DataType => { |
| 71 | + match serde_json::from_str::<DefinitionDataType>(definition) { |
| 72 | + Ok(data_type) => feature.data_types.push(data_type), |
| 73 | + Err(err) => feature.errors.push(DefinitionError { |
| 74 | + definition: definition.to_string(), |
| 75 | + definition_type: MetaType::DataType, |
| 76 | + error: err.to_string() |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + MetaType::FlowType => match serde_json::from_str::<FlowType>(definition) { |
| 81 | + Ok(flow_type) => feature.flow_types.push(flow_type), |
| 82 | + Err(err) => feature.errors.push(DefinitionError { |
| 83 | + definition: definition.to_string(), |
| 84 | + definition_type: MetaType::FlowType, |
| 85 | + error: err.to_string() |
| 86 | + }) |
| 87 | + }, |
| 88 | + MetaType::RuntimeFunction => { |
| 89 | + match serde_json::from_str::<RuntimeFunctionDefinition>(definition) { |
| 90 | + Ok(func) => feature.runtime_functions.push(func), |
| 91 | + Err(err) => feature.errors.push(DefinitionError { |
| 92 | + definition: definition.to_string(), |
| 93 | + definition_type: MetaType::RuntimeFunction, |
| 94 | + error: err.to_string() |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments