char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1#! [ doc = include_str! ( "../README.md" )] 2 3mod document; 4mod format; 5pub mod syntax; 6 7pub use document::{ Document , PathSegment }; 8pub use syntax::{ Error , ErrorKind , Span }; 9 10# [ derive ( Debug , Clone , Copy , PartialEq , Eq )] 11# [ non_exhaustive ] 12pub struct Options { 13pub max_depth : usize , 14} 15 16impl Options { 17pub const DEFAULT_MAX_DEPTH : usize =128 ; 18 19pub fn new () ->Self { 20Self :: default () 21} 22 23pub fn with_max_depth ( mut self , max_depth : usize ) ->Self { 24self . max_depth = max_depth; 25self 26} 27} 28 29impl Default for Options { 30fn default () ->Self { 31Self { 32max_depth : Self :: DEFAULT_MAX_DEPTH , 33} 34} 35} 36 37pub fn parse ( source : & str ) ->Result < serde_json:: Value , Error > { 38parse_with ( source, & Options :: default ()) 39} 40 41pub fn parse_with ( source : & str , options : & Options ) ->Result < serde_json:: Value , Error > { 42 syntax:: parse_with ( source, options) ?. to_value () 43} 44 45pub fn stringify ( value : & serde_json:: Value ) ->Result < String , Error > { 46stringify_with ( value, & Options :: default ()) 47} 48 49pub fn stringify_with ( value : & serde_json:: Value , options : & Options ) ->Result < String , Error > { 50 format:: ( value, true , options) 51} 52 53impl syntax:: Value { 54pub fn to_value ( & self ) ->Result < serde_json:: Value , Error > { 55use serde_json:: Value ; 56use syntax:: ValueKind ; 57 58Ok ( match & self . kind { 59ValueKind :: Null =>Value :: Null , 60ValueKind :: Bool ( value) =>Value :: Bool ( * value), 61// Rebuilt from primitives so `serde_json/arbitrary_precision`, which the 62// bundler enables, neither keeps the source spelling nor admits out-of-range numbers. 63ValueKind :: Number ( value) =>Value :: Number ( 64 value 65. parse () 66. ok () 67. and_then ( |number : serde_json:: Number |{ 68 number 69. as_u64 () 70. map ( Into :: into) 71. or_else ( || number. as_i64 (). map ( Into :: into)) 72. or_else ( || serde_json:: Number :: from_f64 ( number. as_f64 () ?)) 73}) 74. ok_or_else ( ||Error { 75kind : ErrorKind :: Number , 76span : self . span . clone (), 77message : "Invalid or out-of-range JSON number" . into (), 78previous : None , 79}) ?, 80), 81ValueKind :: String ( value) =>Value :: String ( value. clone ()), 82ValueKind :: Array ( values) =>Value :: Array ( 83 values 84. iter () 85. map ( Self :: to_value) 86. collect ::< Result < _ , _ >>() ?, 87), 88ValueKind :: Object ( members) =>Value :: Object ( 89 members 90. iter () 91. map ( |member|Ok (( member. key . clone (), member. value . to_value () ?))) 92. collect ::< Result < _ , Error >>() ?, 93), 94}) 95} 96}