6
6
//! # Example
7
7
//!
8
8
//! ```no_run
9
- //! match oas3::from_path("path/to/openapi.yml") {
9
+ //! let yaml = std::fs::read_to_string("path/to/openapi.yml").unwrap();
10
+ //!
11
+ //! match oas3::from_yaml(yaml) {
10
12
//! Ok(spec) => println!("spec: {:?}", spec),
11
13
//! Err(err) => println!("error: {}", err)
12
14
//! }
17
19
#![ warn( missing_docs) ]
18
20
#![ cfg_attr( docsrs, feature( doc_auto_cfg) ) ]
19
21
20
- use std:: { fs:: File , io:: Read , path:: Path } ;
21
-
22
- mod error;
23
22
pub mod spec;
24
23
25
- pub use self :: { error :: Error , spec:: Spec } ;
24
+ pub use self :: spec:: Spec ;
26
25
27
26
/// Version 3.1.0 of the OpenAPI specification.
28
27
///
@@ -32,63 +31,48 @@ pub use self::{error::Error, spec::Spec};
32
31
pub type OpenApiV3Spec = spec:: Spec ;
33
32
34
33
/// Try deserializing an OpenAPI spec (YAML or JSON) from a file, giving the path.
35
- ///
36
- /// If the `yaml` feature flag is disabled only `JSON` specs are supported
37
- pub fn from_path < P > ( path : P ) -> Result < OpenApiV3Spec , Error >
38
- where
39
- P : AsRef < Path > ,
40
- {
41
- from_reader ( File :: open ( path) ?)
34
+ #[ cfg( all( test, feature = "yaml-spec" ) ) ]
35
+ pub ( crate ) fn from_path (
36
+ path : impl AsRef < std:: path:: Path > ,
37
+ ) -> std:: io:: Result < Result < OpenApiV3Spec , serde_yaml:: Error > > {
38
+ let file = std:: fs:: File :: open ( path. as_ref ( ) ) ?;
39
+ Ok ( from_reader ( file) )
42
40
}
43
41
44
42
/// Try deserializing an OpenAPI spec (YAML or JSON) from a [`Read`] type.
45
- ///
46
- /// If the `yaml` feature flag is disabled only `JSON` specs are supported
47
- pub fn from_reader < R > ( read : R ) -> Result < OpenApiV3Spec , Error >
48
- where
49
- R : Read ,
50
- {
51
- #[ cfg( feature = "yaml_spec" ) ]
52
- {
53
- Ok ( serde_yaml:: from_reader :: < R , OpenApiV3Spec > ( read) ?)
54
- }
55
- #[ cfg( not( feature = "yaml_spec" ) ) ]
56
- {
57
- Ok ( serde_json:: from_reader :: < R , OpenApiV3Spec > ( read) ?)
58
- }
43
+ #[ cfg( all( test, feature = "yaml-spec" ) ) ]
44
+ pub ( crate ) fn from_reader ( read : impl std:: io:: Read ) -> Result < OpenApiV3Spec , serde_yaml:: Error > {
45
+ serde_yaml:: from_reader :: < _ , OpenApiV3Spec > ( read)
59
46
}
60
47
61
- /// Try deserializing an OpenAPI spec (YAML or JSON) from string.
62
- ///
63
- /// If the `yaml` feature flag is disabled only `JSON` specs are supported
64
- pub fn from_str ( val : impl AsRef < str > ) -> Result < OpenApiV3Spec , Error > {
65
- #[ cfg( feature = "yaml_spec" ) ]
66
- {
67
- Ok ( serde_yaml:: from_str :: < OpenApiV3Spec > ( val. as_ref ( ) ) ?)
68
- }
69
- #[ cfg( not( feature = "yaml_spec" ) ) ]
70
- {
71
- Ok ( serde_json:: from_str :: < OpenApiV3Spec > ( val. as_ref ( ) ) ?)
72
- }
48
+ /// Deserializes an OpenAPI spec (YAML-format) from a string.
49
+ #[ cfg( feature = "yaml-spec" ) ]
50
+ pub fn from_yaml ( yaml : impl AsRef < str > ) -> Result < OpenApiV3Spec , serde_yaml:: Error > {
51
+ serde_yaml:: from_str ( yaml. as_ref ( ) )
52
+ }
53
+
54
+ /// Deserializes an OpenAPI spec (JSON-format) from a string.
55
+ pub fn from_json ( json : impl AsRef < str > ) -> Result < OpenApiV3Spec , serde_json:: Error > {
56
+ serde_json:: from_str ( json. as_ref ( ) )
73
57
}
74
58
75
- /// Try serializing to a YAML string.
76
- #[ cfg( feature = "yaml_spec " ) ]
77
- pub fn to_yaml ( spec : & OpenApiV3Spec ) -> Result < String , Error > {
78
- Ok ( serde_yaml:: to_string ( spec) ? )
59
+ /// Serializes OpenAPI spec to a YAML string.
60
+ #[ cfg( feature = "yaml-spec " ) ]
61
+ pub fn to_yaml ( spec : & OpenApiV3Spec ) -> Result < String , serde_yaml :: Error > {
62
+ serde_yaml:: to_string ( spec)
79
63
}
80
64
81
- /// Try serializing to a JSON string.
82
- pub fn to_json ( spec : & OpenApiV3Spec ) -> Result < String , Error > {
83
- Ok ( serde_json:: to_string_pretty ( spec) ? )
65
+ /// Serializes OpenAPI spec to a JSON string.
66
+ pub fn to_json ( spec : & OpenApiV3Spec ) -> Result < String , serde_json :: Error > {
67
+ serde_json:: to_string_pretty ( spec)
84
68
}
85
69
86
- #[ cfg( all( test, feature = "yaml_spec " ) ) ]
70
+ #[ cfg( all( test, feature = "yaml-spec " ) ) ]
87
71
mod tests {
88
72
use std:: {
89
73
fs:: { self , read_to_string, File } ,
90
74
io:: Write ,
91
- path,
75
+ path:: { self , Path } ,
92
76
} ;
93
77
94
78
use pretty_assertions:: assert_eq;
@@ -143,7 +127,7 @@ mod tests {
143
127
// File -> `Spec` -> `serde_json::Value` -> `String`
144
128
145
129
// Parse the input file
146
- let parsed_spec = from_path ( input_file) . unwrap ( ) ;
130
+ let parsed_spec = from_path ( input_file) . unwrap ( ) . unwrap ( ) ;
147
131
// Convert to serde_json::Value
148
132
let parsed_spec_json = serde_json:: to_value ( parsed_spec) . unwrap ( ) ;
149
133
// Convert to a JSON string
0 commit comments