@@ -6,29 +6,35 @@ use std::{
66
77use bytes:: { BufMut , Bytes , BytesMut } ;
88use serde:: Deserialize ;
9- use serde_yaml:: { Deserializer , Value as YamlValue } ;
9+ use serde_yaml:: Deserializer ;
1010use thiserror:: Error ;
1111
1212use crate :: {
1313 common:: {
1414 data,
1515 data:: { MockDefinition , StaticMockDefinition } ,
1616 } ,
17- server:: {
18- persistence:: Error :: { Deserialization , FileRead } ,
19- state,
20- state:: { Error :: DataConversionError , StateManager } ,
21- } ,
17+ server:: { state, state:: StateManager } ,
2218} ;
2319
2420#[ derive( Error , Debug ) ]
2521pub enum Error {
26- #[ error( "cannot read from mock file: {0}" ) ]
27- FileRead ( String ) ,
28- #[ error( "cannot modify state: {0}" ) ]
22+ #[ error( "cannot read static mock directory '{}': {source}" , path. display( ) ) ]
23+ DirectoryRead {
24+ path : PathBuf ,
25+ #[ source]
26+ source : std:: io:: Error ,
27+ } ,
28+ #[ error( "cannot read static mock file '{}': {source}" , path. display( ) ) ]
29+ FileRead {
30+ path : PathBuf ,
31+ #[ source]
32+ source : std:: io:: Error ,
33+ } ,
34+ #[ error( "state operation failed: {0}" ) ]
2935 State ( #[ from] state:: Error ) ,
30- #[ error( "cannot deserialize YAML: {0}" ) ]
31- Deserialization ( String ) ,
36+ #[ error( "cannot process YAML: {0}" ) ]
37+ Yaml ( # [ from ] serde_yaml :: Error ) ,
3238 #[ error( "cannot convert data structures: {0}" ) ]
3339 DataConversion ( #[ from] data:: Error ) ,
3440}
4753fn read_static_mocks ( path : PathBuf ) -> Result < Vec < StaticMockDefinition > , Error > {
4854 let mut definitions: Vec < StaticMockDefinition > = Vec :: new ( ) ;
4955
50- let paths = read_dir ( path) . expect ( "cannot list files in directory" ) ;
56+ let paths = read_dir ( & path) . map_err ( |source| Error :: DirectoryRead {
57+ path : path. clone ( ) ,
58+ source,
59+ } ) ?;
5160 for file_path in paths {
52- let file_path = file_path. unwrap ( ) . path ( ) ;
61+ let file_path = file_path
62+ . map_err ( |source| Error :: DirectoryRead {
63+ path : path. clone ( ) ,
64+ source,
65+ } ) ?
66+ . path ( ) ;
5367 if let Some ( ext) = file_path. extension ( )
5468 && !"yaml" . eq ( ext)
5569 && !"yml" . eq ( ext)
@@ -59,7 +73,10 @@ fn read_static_mocks(path: PathBuf) -> Result<Vec<StaticMockDefinition>, Error>
5973
6074 tracing:: info!( "Loading static mock file from '{}'" , file_path. to_string_lossy( ) ) ;
6175
62- let content = read_to_string ( file_path) . map_err ( |err| FileRead ( err. to_string ( ) ) ) ?;
76+ let content = read_to_string ( & file_path) . map_err ( |source| Error :: FileRead {
77+ path : file_path,
78+ source,
79+ } ) ?;
6380
6481 definitions. extend ( deserialize_mock_defs_from_yaml ( & content) ?) ;
6582 }
@@ -71,12 +88,7 @@ pub fn deserialize_mock_defs_from_yaml(yaml_content: &str) -> Result<Vec<StaticM
7188 let mut definitions = Vec :: new ( ) ;
7289
7390 for document in Deserializer :: from_str ( yaml_content) {
74- let value = YamlValue :: deserialize ( document) . map_err ( |err| Deserialization ( err. to_string ( ) ) ) ?;
75-
76- let definition: StaticMockDefinition =
77- serde_yaml:: from_value ( value) . map_err ( |err| Deserialization ( err. to_string ( ) ) ) ?;
78-
79- definitions. push ( definition) ;
91+ definitions. push ( StaticMockDefinition :: deserialize ( document) ?) ;
8092 }
8193
8294 Ok ( definitions)
@@ -90,9 +102,8 @@ pub fn serialize_mock_defs_to_yaml(mocks: &[MockDefinition]) -> Result<Bytes, Er
90102 buffer. put_slice ( b"---\n " ) ;
91103 }
92104
93- let static_mock: StaticMockDefinition =
94- StaticMockDefinition :: try_from ( mock) . map_err ( |err| DataConversionError ( err. to_string ( ) ) ) ?;
95- let yaml = serde_yaml:: to_string ( & static_mock) . map_err ( |err| DataConversionError ( err. to_string ( ) ) ) ?;
105+ let static_mock = StaticMockDefinition :: try_from ( mock) ?;
106+ let yaml = serde_yaml:: to_string ( & static_mock) ?;
96107 buffer. put_slice ( yaml. as_bytes ( ) ) ;
97108 }
98109
0 commit comments