22//
33// SPDX-License-Identifier: MPL-2.0
44
5- use kdl:: { KdlEntry , KdlNode } ;
5+ use kdl:: { KdlEntry , KdlNode , NodeKey } ;
66
7- use crate :: { Error , InvalidType , KdlType , MissingProperty } ;
7+ use crate :: { Error , FromKdlType , InvalidType , KdlType , MissingEntry , MissingProperty , StorageUnit } ;
88
99// Get a property from a node
1010pub ( crate ) fn get_kdl_property < ' a > ( node : & ' a KdlNode , name : & ' static str ) -> Result < & ' a KdlEntry , Error > {
@@ -17,6 +17,19 @@ pub(crate) fn get_kdl_property<'a>(node: &'a KdlNode, name: &'static str) -> Res
1717 Ok ( entry)
1818}
1919
20+ pub ( crate ) fn get_kdl_entry < ' a , T > ( node : & ' a KdlNode , id : & ' a T ) -> Result < & ' a KdlEntry , Error >
21+ where
22+ T : Into < NodeKey > + ToString + Clone ,
23+ {
24+ let entry = node. entry ( id. clone ( ) ) . ok_or_else ( || MissingEntry {
25+ at : node. span ( ) ,
26+ id : id. to_string ( ) ,
27+ advice : None ,
28+ } ) ?;
29+
30+ Ok ( entry)
31+ }
32+
2033// Get a string property from a value
2134pub ( crate ) fn kdl_value_to_string ( entry : & kdl:: KdlEntry ) -> Result < String , Error > {
2235 let value = entry. value ( ) . as_string ( ) . ok_or ( InvalidType {
@@ -27,6 +40,23 @@ pub(crate) fn kdl_value_to_string(entry: &kdl::KdlEntry) -> Result<String, Error
2740 Ok ( value. to_owned ( ) )
2841}
2942
43+ // Get an integer property from a value
44+ pub ( crate ) fn kdl_value_to_integer ( entry : & kdl:: KdlEntry ) -> Result < i128 , Error > {
45+ let value = entry. value ( ) . as_integer ( ) . ok_or ( InvalidType {
46+ at : entry. span ( ) ,
47+ expected_type : KdlType :: Integer ,
48+ } ) ?;
49+
50+ Ok ( value)
51+ }
52+
53+ // Convert a KDL value to a storage size
54+ pub ( crate ) fn kdl_value_to_storage_size ( entry : & kdl:: KdlEntry ) -> Result < u64 , Error > {
55+ let value = kdl_value_to_integer ( entry) ?;
56+ let units = StorageUnit :: from_kdl_type ( entry) ?;
57+ Ok ( value as u64 * units as u64 )
58+ }
59+
3060// Get a string property from a node
3161pub ( crate ) fn get_property_str ( node : & KdlNode , name : & ' static str ) -> Result < String , Error > {
3262 let value = get_kdl_property ( node, name) . and_then ( kdl_value_to_string) ?;
0 commit comments