1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
//! Provides a high level way of lazily dereferencing JSON Pointer in [serde](serde) [`Value`](serde_json::Value). //! //! It can operate in-memory or on files (`JSON` or `YAML`). //! //! To deserialize an object of type `T` or a pointer to //! local or distant document referencing an object of type `T`, //! we use the type [SparseSelector](crate::SparseSelector). //! //! The root document is wrapped in a [SparseRoot](crate::SparseRoot). //! This allow a coordination of the state and the cached values. //! //! Let's take the following `JSON` document : //! ```json //! { //! "hello": "world", //! "obj": { //! "key1": { //! "$ref": "#/hello" //! }, //! "key2": "universe" //! } //! } //! ``` //! //! Now, let's parse it using the [SparseSelector](crate::SparseSelector) and the [SparseRoot](crate::SparseRoot) : //! //! ```rust //! extern crate sppparse; //! //! use serde::{Deserialize, Serialize}; //! use sppparse::{Sparsable, SparsePointer, SparseRoot, SparseSelector}; //! use std::collections::HashMap; //! use std::path::PathBuf; //! //! #[derive(Debug, Deserialize, Serialize, Sparsable)] //! struct ObjectExampleParsed { //! hello: String, //! obj: HashMap<String, SparseSelector<String>>, //! } //! //! fn main() { //! let doc: SparseRoot<ObjectExampleParsed> = SparseRoot::new_from_file(PathBuf::from(concat!( //! env!("CARGO_MANIFEST_DIR"), //! "/", //! "./examples/read_multi_files.json" //! ))) //! .unwrap(); //! println!("Full object {:#?}", doc.root_get().unwrap()); //! println!( //! "A single ref {:#?}", //! doc.root_get().unwrap().obj.get("key1").unwrap().get() //! ); //! println!( //! "A single ref {:#?}", //! doc.root_get().unwrap().obj.get("key2").unwrap().get() //! ); //! } //! ``` //! ## In-memory //! //! Let's take the following `JSON` example document: //! //! ```json //! { //! "hello": "world", //! "obj": { //! "key1": //! { //! "$ref": "#/hello" //! } //! } //! } //! ``` //! //! We can just pass [Value](serde_json::Value) or objects that implements [Serialize](serde::Serialize) to the [SparseRoot](crate::SparseRoot) //! //! ```rust //! extern crate sppparse; //! //! use serde::{Deserialize, Serialize}; //! use serde_json::json; //! use sppparse::{Sparsable, SparsePointer, SparseRoot, SparseSelector}; //! use std::collections::HashMap; //! use std::path::PathBuf; //! //! #[derive(Debug, Deserialize, Serialize, Sparsable)] //! struct ObjectExampleParsed { //! hello: String, //! obj: HashMap<String, SparseSelector<String>>, //! } //! //! fn main() { //! let json_value = json!({ //! "hello": "world", //! "obj": { //! "key1": { //! "$ref": "#/hello" //! } //! } //! }); //! let parsed_obj: SparseRoot<ObjectExampleParsed> = //! SparseRoot::new_from_value(json_value, PathBuf::from("hello.json"), vec![]).unwrap(); //! //! println!( //! "{}", //! parsed_obj //! .root_get() //! .unwrap() //! .obj //! .get("key1") //! .unwrap() //! .get() //! .expect("the dereferenced pointer") //! ); //! } //! ``` //! ## File backed //! //! If we take the same object as the in-memory example, but reading from a file, //! the rust code would like the following : //! //! ```rust //! extern crate sppparse; //! //! use serde::{Deserialize, Serialize}; //! use sppparse::{Sparsable, SparsePointer, SparseRoot, SparseSelector}; //! use std::collections::HashMap; //! use std::path::PathBuf; //! //! #[derive(Debug, Deserialize, Serialize, Sparsable)] //! struct ObjectExampleParsed { //! hello: String, //! obj: HashMap<String, SparseSelector<String>>, //! } //! //! fn main() { //! let val: SparseRoot<ObjectExampleParsed> = SparseRoot::new_from_file(PathBuf::from(concat!( //! env!("CARGO_MANIFEST_DIR"), //! "/", //! "./examples/read_single_file.json" //! ))) //! .unwrap(); //! //! println!( //! "{}", //! val.root_get() //! .unwrap() //! .obj //! .get("key1") //! .unwrap() //! .get() //! .expect("the dereferenced pointer") //! ); //! } //! ``` //! //! ## Updates //! //! Using [Sparse](crate), it's also possible to modify the parsed value and then save them to disk. //! //! See the following example : //! //! ```rust //! extern crate sppparse; //! use serde::{Deserialize, Serialize}; //! use sppparse::{Sparsable, SparsePointer, SparseRoot, SparseSelector}; //! use std::collections::HashMap; //! use std::path::PathBuf; //! //! #[derive(Debug, Deserialize, Serialize, Sparsable)] //! struct ObjectExampleParsed { //! hello: String, //! obj: HashMap<String, SparseSelector<String>>, //! } //! //! fn main() { //! let mut val: SparseRoot<ObjectExampleParsed> = SparseRoot::new_from_file(PathBuf::from(concat!( //! env!("CARGO_MANIFEST_DIR"), //! "/", //! "./examples/read_single_file.json" //! ))) //! .unwrap(); //! //! println!( //! "Before : {}", //! val.root_get() //! .unwrap() //! .obj //! .get("key1") //! .unwrap() //! .get() //! .expect("the dereferenced pointer") //! ); //! { //! let state = val.state().clone(); //! let mut root_mut = val.root_get_mut().unwrap(); //! //! let key1 = root_mut.obj.get_mut("key1").unwrap(); //! let mut key1_deref = key1.get_mut(state).unwrap(); //! //! *key1_deref = "universe".to_string(); //! key1_deref.sparse_save().unwrap(); //! val.sparse_updt().unwrap(); //! } //! println!( //! "After : {}", //! val.root_get() //! .unwrap() //! .obj //! .get("key1") //! .unwrap() //! .get() //! .expect("the dereferenced pointer") //! ); //! // To persist those modification to disk use : //! // //! // val.save_to_disk(None).unwrap() //! } //! ``` #![warn(clippy::all)] mod sparsable; mod sparse_errors; mod sparse_metadata; mod sparse_pointed_value; mod sparse_pointer; mod sparse_ref; mod sparse_ref_raw; mod sparse_ref_raw_inline; mod sparse_root; mod sparse_selector; mod sparse_state; mod sparse_value; mod sparse_value_mut; /// The max stack frames [Sparse](crate) will go before returning a [cyclic](crate::SparseError::CyclicRef). /// /// For each [SparseSelector](crate::SparseSelector) in your objects, you should count 3 stack frames. /// /// i.e. If you have a document with a depth of 30 references. The maximum depth of the recursive function will be /// at most 90. pub const MAX_SPARSE_DEPTH: u32 = 100; #[cfg(test)] pub(crate) mod tests; pub use crate::sparse_errors::SparseError; pub use crate::sparse_state::{SparseFileFormat, SparseState, SparseStateFile}; use getset::{CopyGetters, Getters, MutGetters}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::Value; pub use sparsable::Sparsable as SparsableTrait; pub use sparse_metadata::SparseMetadata; pub use sparse_pointed_value::SparsePointedValue; pub use sparse_pointer::{SparsePointer, SparsePointerRaw}; pub use sparse_ref::SparseRef; pub use sparse_ref_raw::SparseRefRaw; pub use sparse_ref_raw_inline::SparseRefRawInline; pub use sparse_root::SparseRoot; pub use sparse_selector::SparseSelector; pub use sparse_value::SparseValue; pub use sparse_value_mut::SparseValueMut; pub use sppparse_derive::Sparsable; use std::cell::RefCell; use std::collections::HashMap; use std::convert::From; use std::path::PathBuf; use std::rc::Rc;