Skip to content

Commit

Permalink
Add serde feature for merde/merde_core for CowStr
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Sep 14, 2024
1 parent 1abc0a6 commit 32661bd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions merde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ default = ["core", "deserialize"]
full = ["core", "deserialize", "json", "time"]
deserialize = ["core"]
core = ["dep:merde_core"]
compact_str = ["merde_core/compact_str"]
serde = ["merde_core/serde"]

json = ["dep:merde_json"]
time = ["dep:merde_time"]
4 changes: 3 additions & 1 deletion merde_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ categories = ["encoding", "parser-implementations"]

[dependencies]
compact_str = { version = "0.8.0", optional = true }
serde = { version = "1", optional = true }

[features]
default = []
full = ["compact_str"]
full = ["compact_str", "serde"]
compact_str = ["dep:compact_str"]
serde = ["dep:serde", "compact_str/serde"]
31 changes: 31 additions & 0 deletions merde_core/src/cowstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,34 @@ impl IntoStatic for CowStr<'_> {
}
}
}

#[cfg(feature = "serde")]
mod serde_impls {
use super::*;

use serde::{Deserialize, Serialize};

impl Serialize for CowStr<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self)
}
}

impl<'de> Deserialize<'de> for CowStr<'_> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[cfg(feature = "compact_str")]
let s = compact_str::CompactString::deserialize(deserializer)?;

#[cfg(not(feature = "compact_str"))]
let s = String::deserialize(deserializer)?;

Ok(CowStr::Owned(s))
}
}
}
5 changes: 2 additions & 3 deletions zerodeps-example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions zerodeps-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ publish = false

[dependencies]
merde = { version = "4.0.0", path = "../merde", default-features = false }
merdelcore = { version = "4.0.0", path = "../merde_core", default-features = false }

[features]
default = []
merde = ["merde/core", "merde_core/compact_str"]
merde = ["merde/core", "merde/compact_str"]

0 comments on commit 32661bd

Please sign in to comment.