@@ -36,7 +36,6 @@ use std::{
3636 fmt,
3737 path:: { Path , PathBuf } ,
3838} ;
39- use structable:: OutputConfig ;
4039use thiserror:: Error ;
4140use tracing:: error;
4241
@@ -88,14 +87,43 @@ pub enum ConfigFileBuilderError {
8887 path : PathBuf ,
8988 } ,
9089}
90+ ///
91+ /// Output configuration
92+ ///
93+ /// This structure is controlling how the table table is being built for a structure.
94+ #[ derive( Clone , Debug , Default , Deserialize ) ]
95+ pub struct ViewConfig {
96+ /// Limit fields (their titles) to be returned
97+ #[ serde( default ) ]
98+ pub fields : Vec < FieldConfig > ,
99+ }
100+
101+ /// Field output configuration
102+ #[ derive( Clone , Debug , Deserialize , Eq , Ord , PartialOrd , PartialEq ) ]
103+ #[ serde( untagged) ]
104+ pub enum FieldConfig {
105+ /// Simple - only attribute name as string
106+ Simple ( String ) ,
107+ /// Extended - object with individual properties of the output column
108+ Extended {
109+ /// Attribute name
110+ name : String ,
111+ /// Min length of the column
112+ #[ serde( default ) ]
113+ min_len : Option < usize > ,
114+ /// Max length of the column
115+ #[ serde( default ) ]
116+ max_len : Option < usize > ,
117+ } ,
118+ }
91119
92120/// OpenStackClient configuration
93121#[ derive( Clone , Debug , Default , Deserialize ) ]
94122pub struct Config {
95123 /// Map of views with the key being the resource key `<SERVICE_TYPE>.<RESOURCE>[/<SUBRESOURCE>]`)
96124 /// and the value being an `[OutputConfig]`
97125 #[ serde( default ) ]
98- pub views : HashMap < String , OutputConfig > ,
126+ pub views : HashMap < String , ViewConfig > ,
99127}
100128
101129/// A builder to create a [`ConfigFile`] by specifying which files to load.
@@ -224,3 +252,36 @@ impl fmt::Display for Config {
224252 write ! ( f, "" )
225253 }
226254}
255+
256+ #[ cfg( test) ]
257+ mod tests {
258+ use super :: * ;
259+ use std:: io:: Write ;
260+ use tempfile:: Builder ;
261+
262+ #[ test]
263+ fn test_parse_config ( ) {
264+ let mut config_file = Builder :: new ( ) . suffix ( ".yaml" ) . tempfile ( ) . unwrap ( ) ;
265+
266+ const CONFIG_DATA : & str = r#"
267+ views:
268+ foo:
269+ fields: ["a", "b", "c"]
270+ bar:
271+ fields:
272+ - "a"
273+ - name: "b"
274+ min_len: 1
275+ - "c"
276+ "# ;
277+
278+ write ! ( config_file, "{}" , CONFIG_DATA ) . unwrap ( ) ;
279+
280+ let _cfg = ConfigFileBuilder {
281+ sources : Vec :: new ( ) ,
282+ }
283+ . add_source ( config_file. path ( ) )
284+ . unwrap ( )
285+ . build ( ) ;
286+ }
287+ }
0 commit comments