Skip to content

Commit 204ee70

Browse files
committed
Make serde_yaml dependency optional
Fixes #152
1 parent e758e2f commit 204ee70

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

crates/oas3/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ edition = { workspace = true }
1111
rust-version = { workspace = true }
1212

1313
[features]
14-
default = ["validation"]
14+
default = ["validation", "yaml_spec"]
1515
validation = []
16+
yaml_spec = ["dep:serde_yaml"]
1617

1718
[dependencies]
1819
derive_more = { workspace = true, features = ["display", "error", "from"] }
@@ -23,7 +24,7 @@ regex = { workspace = true }
2324
semver = { workspace = true }
2425
serde = { workspace = true, features = ["derive"] }
2526
serde_json = { workspace = true }
26-
serde_yaml = { workspace = true }
27+
serde_yaml = { workspace = true, optional = true }
2728
url = { workspace = true, features = ["serde"] }
2829

2930
[dev-dependencies]

crates/oas3/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub enum Error {
1515

1616
/// YAML error.
1717
#[display("YAML error")]
18+
#[cfg(feature = "yaml_spec")]
1819
Yaml(serde_yaml::Error),
1920

2021
/// JSON error.

crates/oas3/src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub use self::{error::Error, spec::Spec};
3232
pub type OpenApiV3Spec = spec::Spec;
3333

3434
/// 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
3537
pub fn from_path<P>(path: P) -> Result<OpenApiV3Spec, Error>
3638
where
3739
P: AsRef<Path>,
@@ -40,19 +42,38 @@ where
4042
}
4143

4244
/// 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
4347
pub fn from_reader<R>(read: R) -> Result<OpenApiV3Spec, Error>
4448
where
4549
R: Read,
4650
{
47-
Ok(serde_yaml::from_reader::<R, OpenApiV3Spec>(read)?)
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+
}
4859
}
4960

5061
/// Try deserializing an OpenAPI spec (YAML or JSON) from string.
62+
///
63+
/// If the `yaml` feature flag is disabled only `JSON` specs are supported
5164
pub fn from_str(val: impl AsRef<str>) -> Result<OpenApiV3Spec, Error> {
52-
Ok(serde_yaml::from_str::<OpenApiV3Spec>(val.as_ref())?)
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+
}
5373
}
5474

5575
/// Try serializing to a YAML string.
76+
#[cfg(feature = "yaml_spec")]
5677
pub fn to_yaml(spec: &OpenApiV3Spec) -> Result<String, Error> {
5778
Ok(serde_yaml::to_string(spec)?)
5879
}

crates/oas3/src/spec/spec_extensions.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ use std::{
55

66
use serde::{de, Deserializer, Serializer};
77

8+
#[cfg(feature = "yaml_spec")]
9+
type KeyType = serde_yaml::Value;
10+
#[cfg(not(feature = "yaml_spec"))]
11+
type KeyType = serde_json::Value;
12+
813
/// Deserializes fields of a map beginning with `x-`.
914
pub(crate) fn deserialize<'de, D>(
1015
deserializer: D,
@@ -25,7 +30,7 @@ where
2530
where
2631
M: de::MapAccess<'de>,
2732
{
28-
let mut map = HashMap::<serde_yaml::Value, serde_json::Value>::new();
33+
let mut map = HashMap::<KeyType, serde_json::Value>::new();
2934

3035
while let Some((key, value)) = access.next_entry()? {
3136
map.insert(key, value);

crates/roast/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ derive_more = { workspace = true, features = ["display", "error", "from"] }
1717
futures-util = { workspace = true }
1818
http = { workspace = true }
1919
log = { workspace = true }
20-
oas3 = { workspace = true }
20+
oas3 = { workspace = true, default-features = false }
2121
once_cell = { workspace = true }
2222
prettytable-rs = { workspace = true }
2323
reqwest = { workspace = true, features = ["json"] }

0 commit comments

Comments
 (0)