json0_rs/
lib.rs

1use std::rc::Rc;
2
3use error::Result;
4use json::{Appliable, Routable};
5use operation::{Operation, OperationFactory};
6use path::Path;
7use serde_json::Value;
8use sub_type::{SubTypeFunctions, SubTypeFunctionsHolder};
9use transformer::Transformer;
10
11mod common;
12pub mod error;
13mod json;
14pub mod operation;
15pub mod path;
16mod sub_type;
17mod transformer;
18
19#[cfg(test)]
20#[macro_use]
21extern crate assert_matches;
22
23pub struct Json0 {
24    functions: Rc<SubTypeFunctionsHolder>,
25    transformer: Transformer,
26    operation_faction: OperationFactory,
27}
28
29impl Json0 {
30    pub fn new() -> Json0 {
31        let functions = Rc::new(SubTypeFunctionsHolder::new());
32        let transformer = Transformer::new(functions.clone());
33        let operation_faction = OperationFactory::new(functions.clone());
34
35        Json0 {
36            functions,
37            transformer,
38            operation_faction,
39        }
40    }
41
42    pub fn register_subtype(
43        &self,
44        sub_type: String,
45        o: Box<dyn SubTypeFunctions>,
46    ) -> Result<Option<Box<dyn SubTypeFunctions>>> {
47        self.functions.register_subtype(sub_type, o)
48    }
49
50    pub fn unregister_subtype(&self, sub_type: &String) -> Option<Box<dyn SubTypeFunctions>> {
51        self.functions.unregister_subtype(sub_type)
52    }
53
54    pub fn clear_registered_subtype(&self) {
55        self.functions.clear();
56    }
57
58    pub fn operation_factory(&self) -> &OperationFactory {
59        &self.operation_faction
60    }
61
62    pub fn apply(&self, value: &mut Value, operations: Vec<Operation>) -> Result<()> {
63        for operation in operations {
64            for op in operation.into_iter() {
65                value.apply(op.path.clone(), op.operator)?;
66            }
67        }
68        Ok(())
69    }
70
71    pub fn get_by_path<'a>(&self, value: &'a mut Value, paths: &Path) -> Result<Option<&'a Value>> {
72        value.route_get(paths)
73    }
74
75    pub fn transform(
76        &self,
77        operation: &Operation,
78        base_operation: &Operation,
79    ) -> Result<(Operation, Operation)> {
80        self.transformer.transform(operation, base_operation)
81    }
82}
83
84impl Default for Json0 {
85    fn default() -> Self {
86        Self::new()
87    }
88}