11import glaml . {
2- type DocNode , DocNodeBool , DocNodeFloat , DocNodeInt , DocNodeMap , DocNodeNil ,
3- DocNodeSeq , DocNodeStr ,
2+ type Node , NodeBool , NodeFloat , NodeInt , NodeMap , NodeNil , NodeSeq , NodeStr ,
43}
54import gleam/float
65import gleam/int
76import gleam/list
87import gleam/string
98import yodel/errors . {
10- type ConfigError , InvalidSyntax , Location , ParseError , SyntaxError ,
9+ type ConfigError , InvalidStructure , InvalidSyntax , Location , ParseError ,
10+ SyntaxError ,
1111}
1212import yodel/input . { type Input , Content , File }
1313import yodel/options . { type Format , Auto , Json , Yaml }
1414import yodel/path . { type Path }
1515import yodel/properties . { type Properties }
1616
1717const known_extensions = [
18- # ( "json" , [ "json" , "jsn" , "json5" , "jsonc" ] ) , # ( "yaml" , [ "yaml" , "yml" ] ) ,
18+ # ( "json" , [ "json" , "jsn" , "json5" , "jsonc" ] ) ,
19+ # ( "yaml" , [ "yaml" , "yml" ] ) ,
1920]
2021
2122pub fn detect ( input : Input ) -> Format {
@@ -76,34 +77,41 @@ fn detect_yaml(content: String) -> Bool {
7677
7778pub fn parse ( from string : String ) -> Result ( Properties , ConfigError ) {
7879 case glaml . parse_string ( string ) {
79- Ok ( doc ) -> glaml . doc_node ( doc ) |> parse_properties ( path . new ( ) ) |> Ok
80+ Ok ( docs ) ->
81+ docs
82+ |> list . map ( glaml . document_root )
83+ |> list . fold ( properties . new ( ) , fn ( acc , node ) {
84+ parse_properties ( node , path . new ( ) )
85+ |> properties . merge ( acc )
86+ } )
87+ |> Ok
8088 Error ( err ) -> Error ( map_glaml_error ( err ) )
8189 }
8290}
8391
84- fn parse_properties ( node : DocNode , path : Path ) -> Properties {
92+ fn parse_properties ( node : Node , path : Path ) -> Properties {
8593 case node {
86- DocNodeStr ( value ) -> properties . string ( path , value )
87- DocNodeInt ( value ) -> properties . int ( path , value )
88- DocNodeFloat ( value ) -> properties . float ( path , value )
89- DocNodeBool ( value ) -> properties . bool ( path , value )
90- DocNodeNil -> properties . null ( path )
94+ NodeStr ( value ) -> properties . string ( path , value )
95+ NodeInt ( value ) -> properties . int ( path , value )
96+ NodeFloat ( value ) -> properties . float ( path , value )
97+ NodeBool ( value ) -> properties . bool ( path , value )
98+ NodeNil -> properties . null ( path )
9199
92- DocNodeMap ( pairs ) -> parse_map ( pairs , path )
93- DocNodeSeq ( items ) -> parse_seq ( items , path )
100+ NodeMap ( pairs ) -> parse_map ( pairs , path )
101+ NodeSeq ( items ) -> parse_seq ( items , path )
94102 }
95103}
96104
97- fn extract_key ( node : DocNode ) -> String {
105+ fn extract_key ( node : Node ) -> String {
98106 case node {
99- DocNodeStr ( value ) -> value
100- DocNodeInt ( value ) -> int . to_string ( value )
101- DocNodeFloat ( value ) -> float . to_string ( value )
107+ NodeStr ( value ) -> value
108+ NodeInt ( value ) -> int . to_string ( value )
109+ NodeFloat ( value ) -> float . to_string ( value )
102110 _ -> string . inspect ( node )
103111 }
104112}
105113
106- fn parse_map ( pairs : List ( # ( DocNode , DocNode ) ) , path : Path ) -> Properties {
114+ fn parse_map ( pairs : List ( # ( Node , Node ) ) , path : Path ) -> Properties {
107115 list . fold ( pairs , properties . new ( ) , fn ( acc , pair ) {
108116 let key = extract_key ( pair . 0 )
109117 let path = path |> path . add_segment ( key )
@@ -112,21 +120,26 @@ fn parse_map(pairs: List(#(DocNode, DocNode)), path: Path) -> Properties {
112120 } )
113121}
114122
115- fn parse_seq ( items : List ( DocNode ) , path : Path ) -> Properties {
123+ fn parse_seq ( items : List ( Node ) , path : Path ) -> Properties {
116124 list . index_fold ( items , properties . new ( ) , fn ( acc , item , index ) {
117125 let path = path |> path . add_index ( index )
118126 let props = parse_properties ( item , path )
119127 properties . merge ( acc , props )
120128 } )
121129}
122130
123- fn map_glaml_error ( error : glaml . DocError ) -> ConfigError {
124- let glaml . DocError ( msg , # ( line , col ) ) = error
125- ParseError (
126- InvalidSyntax ( SyntaxError (
127- format : "Json/Yaml" ,
128- location : Location ( line , col ) ,
129- message : msg ,
130- ) ) ,
131- )
131+ fn map_glaml_error ( error : glaml . YamlError ) -> ConfigError {
132+ case error {
133+ glaml . UnexpectedParsingError ->
134+ ParseError ( InvalidStructure ( "Unexpected parsing error" ) )
135+ glaml . ParsingError ( msg , loc ) -> {
136+ ParseError (
137+ InvalidSyntax ( SyntaxError (
138+ format : "Json/Yaml" ,
139+ location : Location ( loc . line , loc . column ) ,
140+ message : msg ,
141+ ) ) ,
142+ )
143+ }
144+ }
132145}
0 commit comments