Add support for SmolStr via a feature.

SmolStr is an immutable "packed" string that uses the space normally occupied
by a String's header if the String is short enough.
This commit is contained in:
lfn3 2021-11-29 20:10:32 +00:00 committed by Sunli
parent 4a265ed29d
commit 7aea053240
3 changed files with 28 additions and 0 deletions

View File

@ -61,6 +61,7 @@ opentelemetry = { version = "0.16.0", optional = true, default-features = false,
uuid = { version = "0.8.2", optional = true, features = ["v4", "serde"] }
rust_decimal = { version = "1.14.3", optional = true }
url = { version = "2.2.1", optional = true }
smol_str = { version = "0.1.21", optional = true }
# Non-feature optional dependencies
blocking = { version = "1.0.2", optional = true }

View File

@ -26,6 +26,8 @@ mod duration;
mod naive_time;
#[cfg(feature = "secrecy")]
mod secrecy;
#[cfg(feature = "smol_str")]
mod smol_str;
#[cfg(feature = "url")]
mod url;
#[cfg(feature = "uuid")]

25
src/types/external/smol_str.rs vendored Normal file
View File

@ -0,0 +1,25 @@
use smol_str::SmolStr;
use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
#[Scalar(
internal,
name = "SmolStr",
specified_by_url = "https://docs.rs/smol_str/latest/smol_str/struct.SmolStr.html"
)]
impl ScalarType for SmolStr {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(SmolStr::new(s)),
_ => Err(InputValueError::expected_type(value)),
}
}
fn is_valid(value: &Value) -> bool {
matches!(value, Value::String(_))
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}