From 7aea053240100e8be84c75215050af215438bcae Mon Sep 17 00:00:00 2001 From: lfn3 Date: Mon, 29 Nov 2021 20:10:32 +0000 Subject: [PATCH] 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. --- Cargo.toml | 1 + src/types/external/mod.rs | 2 ++ src/types/external/smol_str.rs | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/types/external/smol_str.rs diff --git a/Cargo.toml b/Cargo.toml index b062eec3..fdaaac7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/types/external/mod.rs b/src/types/external/mod.rs index 73b326e5..4a17cb3f 100644 --- a/src/types/external/mod.rs +++ b/src/types/external/mod.rs @@ -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")] diff --git a/src/types/external/smol_str.rs b/src/types/external/smol_str.rs new file mode 100644 index 00000000..9a4df853 --- /dev/null +++ b/src/types/external/smol_str.rs @@ -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 { + 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()) + } +}