Skip to content

Commit

Permalink
Make compact_str dependency optional, too
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Sep 13, 2024
1 parent 5c78a9b commit a41abf4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ check:
pushd zerodeps-example
cargo check
cargo check --features=merde
cargo tree --prefix none --no-dedupe | grep -v compact_str
cargo tree --prefix none --no-dedupe --features=merde | grep compact_str
cargo tree --prefix none --no-dedupe | grep -v merde-core
cargo tree --prefix none --no-dedupe --features=merde | grep merde-core
popd
7 changes: 6 additions & 1 deletion merde-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ keywords = ["merde", "serialization", "deserialization"]
categories = ["encoding", "parser-implementations"]

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

[features]
default = []
full = ["compact_str"]
compact_str = ["dep:compact_str"]
13 changes: 10 additions & 3 deletions merde-core/src/cowstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
ops::Deref,
};

#[cfg(feature = "compact_str")]
use compact_str::CompactString;

use crate::IntoStatic;
Expand All @@ -17,7 +18,10 @@ use crate::IntoStatic;
#[derive(Clone)]
pub enum CowStr<'s> {
Borrowed(&'s str),
#[cfg(feature = "compact_str")]
Owned(CompactString),
#[cfg(not(feature = "compact_str"))]
Owned(String),
}

impl Deref for CowStr<'_> {
Expand All @@ -35,7 +39,8 @@ impl<'a> From<Cow<'a, str>> for CowStr<'a> {
fn from(s: Cow<'a, str>) -> Self {
match s {
Cow::Borrowed(s) => CowStr::Borrowed(s),
Cow::Owned(s) => CowStr::Owned(CompactString::from(s)),
#[allow(clippy::useless_conversion)]
Cow::Owned(s) => CowStr::Owned(s.into()),
}
}
}
Expand All @@ -48,13 +53,14 @@ impl<'s> From<&'s str> for CowStr<'s> {

impl From<String> for CowStr<'_> {
fn from(s: String) -> Self {
CowStr::Owned(CompactString::from(s))
#[allow(clippy::useless_conversion)]
CowStr::Owned(s.into())
}
}

impl From<Box<str>> for CowStr<'_> {
fn from(s: Box<str>) -> Self {
CowStr::Owned(CompactString::from(s))
CowStr::Owned(s.into())
}
}

Expand All @@ -68,6 +74,7 @@ impl From<CowStr<'_>> for String {
fn from(s: CowStr<'_>) -> Self {
match s {
CowStr::Borrowed(s) => s.into(),
#[allow(clippy::useless_conversion)]
CowStr::Owned(s) => s.into(),
}
}
Expand Down
1 change: 1 addition & 0 deletions merde-core/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<'s> ValueDeserialize<'s> for Cow<'s, str> {
match value {
Some(Value::Str(s)) => match s {
CowStr::Borrowed(b) => Ok(Cow::Borrowed(b)),
#[allow(clippy::useless_conversion)]
CowStr::Owned(o) => Ok(Cow::Owned(o.into())),
},
Some(v) => Err(MerdeError::MismatchedType {
Expand Down
1 change: 1 addition & 0 deletions zerodeps-example/Cargo.lock

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

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

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

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

0 comments on commit a41abf4

Please sign in to comment.