Skip to content

Commit 2060936

Browse files
committed
Replaced smartstring with compact_str
- implement `String` scalar for `compact_str::CompactString`
1 parent 722452b commit 2060936

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

juniper/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
6464
- Made `name()` method returning `ArcStr`.
6565
- `GraphQLValue`:
6666
- Made `type_name()` method returning `ArcStr`.
67+
- Switched `ParseError::UnexpectedToken` to `compact_str::CompactString` instead of `smartstring::SmartString`. ([todo])
6768

6869
### Added
6970

@@ -77,6 +78,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
7778
- `jiff::tz::Offset` as `UtcOffset` scalar.
7879
- `jiff::Span` as `Duration` scalar.
7980
- `http::GraphQLResponse::into_result()` method. ([#1293])
81+
- `String` scalar implementation for `compact_str::CompactString`. ([todo])
8082

8183
### Changed
8284

@@ -103,6 +105,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
103105
[#1318]: /../../pull/1318
104106
[#1319]: /../../pull/1319
105107
[1b1fc618]: /../../commit/1b1fc61879ffdd640d741e187dc20678bf7ab295
108+
[todo]: /../../commit/todo
106109

107110

108111

juniper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bigdecimal = { version = "0.4", optional = true }
5050
bson = { version = "2.4", optional = true }
5151
chrono = { version = "0.4.30", features = ["alloc"], default-features = false, optional = true }
5252
chrono-tz = { version = "0.10", default-features = false, optional = true }
53+
compact_str = "0.9"
5354
fnv = "1.0.5"
5455
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
5556
graphql-parser = { version = "0.4", optional = true }
@@ -60,7 +61,6 @@ rust_decimal = { version = "1.20", default-features = false, optional = true }
6061
ryu = { version = "1.0", optional = true }
6162
serde = { version = "1.0.122", features = ["derive"] }
6263
serde_json = { version = "1.0.18", features = ["std"], default-features = false, optional = true }
63-
smartstring = "1.0"
6464
static_assertions = "1.1"
6565
time = { version = "0.3.37", features = ["formatting", "macros", "parsing"], optional = true }
6666
url = { version = "2.0", optional = true }

juniper/src/parser/parser.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{error::Error, fmt};
22

3-
use smartstring::alias::String;
3+
use compact_str::{CompactString, format_compact};
44

55
use crate::parser::{Lexer, LexerError, Spanning, Token};
66

@@ -10,7 +10,7 @@ pub enum ParseError {
1010
/// An unexpected token occurred in the source
1111
// TODO: Previously was `Token<'a>`.
1212
// Revisit on `graphql-parser` integration.
13-
UnexpectedToken(String),
13+
UnexpectedToken(CompactString),
1414

1515
/// The input source abruptly ended
1616
UnexpectedEndOfFile,
@@ -48,14 +48,7 @@ impl ParseError {
4848
/// Creates a [`ParseError::UnexpectedToken`] out of the provided [`Token`].
4949
#[must_use]
5050
pub fn unexpected_token(token: Token<'_>) -> Self {
51-
use std::fmt::Write as _;
52-
53-
let mut s = String::new();
54-
// PANIC: Unwrapping is OK here, as it may panic only on allocation
55-
// error.
56-
write!(s, "{token}").unwrap();
57-
58-
Self::UnexpectedToken(s)
51+
Self::UnexpectedToken(format_compact!("{token}"))
5952
}
6053
}
6154

juniper/src/types/scalars.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ mod impl_arcstr_scalar {
207207
}
208208
}
209209

210+
#[graphql_scalar]
211+
#[graphql(name = "String", with = impl_compactstring_scalar, parse_token(String))]
212+
type CompactString = compact_str::CompactString;
213+
214+
mod impl_compactstring_scalar {
215+
use crate::{InputValue, ScalarValue, Value};
216+
217+
use super::CompactString;
218+
219+
pub(super) fn to_output<S: ScalarValue>(v: &CompactString) -> Value<S> {
220+
Value::scalar(v.to_string())
221+
}
222+
223+
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<CompactString, String> {
224+
v.as_string_value()
225+
.map(Into::into)
226+
.ok_or_else(|| format!("Expected `String`, found: {v}"))
227+
}
228+
}
229+
210230
impl<S> reflect::WrappedType<S> for str {
211231
const VALUE: reflect::WrappedValue = 1;
212232
}

0 commit comments

Comments
 (0)