11//! Copyright The KCL Authors. All rights reserved.
22
33extern crate serde_json;
4- extern crate serde_yaml ;
4+ extern crate serde_yaml_ng ;
55
66use crate :: * ;
77
@@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
1414/// - ignore_none: Whether to ignore the attribute whose value is `None` (defaults to false).
1515/// - sep: Which separator to use between YAML documents (defaults to "---").
1616///
17- /// TODO: We have not yet supported the following options because serde_yaml
17+ /// TODO: We have not yet supported the following options because serde_yaml_ng
1818/// does not support these capabilities yet.
1919/// Ref: https://github.com/dtolnay/serde-yaml/issues/337
2020/// - indent: Which kind of indentation to use when emitting (defaults to 2).
@@ -44,17 +44,17 @@ impl Default for YamlEncodeOptions {
4444
4545impl ValueRef {
4646 /// Decode a yaml single document string to a ValueRef.
47- /// Returns [serde_yaml ::Error] when decoding fails.
48- pub fn from_yaml ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml :: Error > {
47+ /// Returns [serde_yaml_ng ::Error] when decoding fails.
48+ pub fn from_yaml ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml_ng :: Error > {
4949 // We use JsonValue to implement the KCL universal serialization object.
50- let json_value: JsonValue = serde_yaml :: from_str ( s) ?;
50+ let json_value: JsonValue = serde_yaml_ng :: from_str ( s) ?;
5151 Ok ( Self :: from_json ( ctx, serde_json:: to_string ( & json_value) . unwrap ( ) . as_ref ( ) ) . unwrap ( ) )
5252 }
5353
5454 /// Decode yaml stream string that contains `---` to a ValueRef.
55- /// Returns [serde_yaml ::Error] when decoding fails.
56- pub fn from_yaml_stream ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml :: Error > {
57- let documents = serde_yaml :: Deserializer :: from_str ( s) ;
55+ /// Returns [serde_yaml_ng ::Error] when decoding fails.
56+ pub fn from_yaml_stream ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml_ng :: Error > {
57+ let documents = serde_yaml_ng :: Deserializer :: from_str ( s) ;
5858 let mut result = ValueRef :: list_value ( None ) ;
5959 for document in documents {
6060 let json_value: JsonValue = JsonValue :: deserialize ( document) ?;
@@ -71,9 +71,9 @@ impl ValueRef {
7171 }
7272
7373 /// Decode yaml stream string that contains `---` to a ValueRef.
74- /// Returns [serde_yaml ::Error] when decoding fails.
75- pub fn list_from_yaml_stream ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml :: Error > {
76- let documents = serde_yaml :: Deserializer :: from_str ( s) ;
74+ /// Returns [serde_yaml_ng ::Error] when decoding fails.
75+ pub fn list_from_yaml_stream ( ctx : & mut Context , s : & str ) -> Result < Self , serde_yaml_ng :: Error > {
76+ let documents = serde_yaml_ng :: Deserializer :: from_str ( s) ;
7777 let mut result = ValueRef :: list_value ( None ) ;
7878 for document in documents {
7979 let json_value: JsonValue = JsonValue :: deserialize ( document) ?;
@@ -84,17 +84,17 @@ impl ValueRef {
8484
8585 pub fn to_yaml ( & self ) -> Vec < u8 > {
8686 let json = self . to_json_string ( ) ;
87- let yaml_value: serde_yaml :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
88- match serde_yaml :: to_string ( & yaml_value) {
87+ let yaml_value: serde_yaml_ng :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
88+ match serde_yaml_ng :: to_string ( & yaml_value) {
8989 Ok ( s) => s. into_bytes ( ) ,
9090 _ => Vec :: new ( ) ,
9191 }
9292 }
9393
9494 pub fn to_yaml_string ( & self ) -> String {
9595 let json = self . to_json_string ( ) ;
96- let yaml_value: serde_yaml :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
97- match serde_yaml :: to_string ( & yaml_value) {
96+ let yaml_value: serde_yaml_ng :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
97+ match serde_yaml_ng :: to_string ( & yaml_value) {
9898 Ok ( s) => {
9999 let s = s. strip_prefix ( "---\n " ) . unwrap_or_else ( || s. as_ref ( ) ) ;
100100 s. to_string ( )
@@ -113,8 +113,8 @@ impl ValueRef {
113113 ignore_none : opts. ignore_none ,
114114 } ;
115115 let json = self . to_json_string_with_options ( & json_opts) ;
116- let yaml_value: serde_yaml :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
117- match serde_yaml :: to_string ( & yaml_value) {
116+ let yaml_value: serde_yaml_ng :: Value = serde_json:: from_str ( json. as_ref ( ) ) . unwrap ( ) ;
117+ match serde_yaml_ng :: to_string ( & yaml_value) {
118118 Ok ( s) => {
119119 let s = s. strip_prefix ( "---\n " ) . unwrap_or_else ( || s. as_ref ( ) ) ;
120120 s. to_string ( )
@@ -129,8 +129,8 @@ mod test_value_yaml {
129129 use crate :: * ;
130130
131131 #[ test]
132- fn test_serde_yaml_on_str ( ) {
133- let on_str = serde_yaml :: to_string ( "on" ) . unwrap ( ) ;
132+ fn test_serde_yaml_ng_on_str ( ) {
133+ let on_str = serde_yaml_ng :: to_string ( "on" ) . unwrap ( ) ;
134134 assert_eq ! ( on_str, "'on'\n " ) ;
135135 }
136136
0 commit comments