Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Next

* Fixes `TryFrom<&str>` for a string-constrained newtype over a string-like native type
* A string constraint's patterns are compiled with the ECMA-262 `u` flag, as
JSON Schema specifies, so `\p{..}` and `\u{..}` escapes work; a pattern with
an identity escape the `u` flag forbids now fails in the consumer's build
* A string-constrained newtype whose inner type is neither `String` nor
`FromStr` is rejected at `finalize` rather than in the consumer's build
* Schemas for tuple structs with a direct standard `Vec` remainder now describe
Expand Down
2 changes: 1 addition & 1 deletion typespace/src/build/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,7 @@ impl<Id: Clone + Ord + std::fmt::Debug + std::fmt::Display> NewtypeStruct<Id> {
quote! {
static PATTERN: ::std::sync::LazyLock<#regress::Regex> =
::std::sync::LazyLock::new(|| {
#regress::Regex::new(#p).unwrap()
#regress::Regex::with_flags(#p, "u").unwrap()
});
if PATTERN.find(value).is_none() {
return Err(#err.into());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ::std::convert::TryFrom<&str> for AnchoredUrl {
value: &str,
) -> ::std::result::Result<Self, self::error::ConversionError> {
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(||
{ ::regress::Regex::new("^https://").unwrap() });
{ ::regress::Regex::with_flags("^https://", "u").unwrap() });
if PATTERN.find(value).is_none() {
return Err("doesn't match pattern \"^https://\"".into());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[derive(::serde::Serialize)]
#[serde(transparent)]
pub struct Word(::std::string::String);
impl ::std::ops::Deref for Word {
type Target = ::std::string::String;
fn deref(&self) -> &::std::string::String {
&self.0
}
}
impl ::std::convert::From<Word> for ::std::string::String {
fn from(value: Word) -> Self {
value.0
}
}
impl ::std::convert::TryFrom<&str> for Word {
type Error = self::error::ConversionError;
fn try_from(
value: &str,
) -> ::std::result::Result<Self, self::error::ConversionError> {
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(||
{ ::regress::Regex::with_flags("^\\p{Letter}+\\u{1F354}?$", "u").unwrap() });
if PATTERN.find(value).is_none() {
return Err("doesn't match pattern \"^\\p{Letter}+\\u{1F354}?$\"".into());
}
Ok(Self(value.to_string()))
}
}
impl ::std::convert::TryFrom<::std::string::String> for Word {
type Error = self::error::ConversionError;
fn try_from(
value: ::std::string::String,
) -> ::std::result::Result<Self, self::error::ConversionError> {
::std::convert::TryFrom::try_from(value.as_str())
}
}
impl<'de> ::serde::Deserialize<'de> for Word {
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
::std::convert::TryFrom::try_from(
::std::string::String::deserialize(deserializer)?,
)
.map_err(|e: self::error::ConversionError| {
<D::Error as ::serde::de::Error>::custom(e.to_string())
})
}
}
/// Error types.
pub mod error {
/// Error from a `TryFrom` or `FromStr` implementation.
pub struct ConversionError(::std::borrow::Cow<'static, str>);
impl ::std::error::Error for ConversionError {}
impl ::std::fmt::Display for ConversionError {
fn fmt(
&self,
f: &mut ::std::fmt::Formatter<'_>,
) -> Result<(), ::std::fmt::Error> {
::std::fmt::Display::fmt(&self.0, f)
}
}
impl ::std::fmt::Debug for ConversionError {
fn fmt(
&self,
f: &mut ::std::fmt::Formatter<'_>,
) -> Result<(), ::std::fmt::Error> {
::std::fmt::Debug::fmt(&self.0, f)
}
}
impl From<&'static str> for ConversionError {
fn from(value: &'static str) -> Self {
Self(value.into())
}
}
impl From<String> for ConversionError {
fn from(value: String) -> Self {
Self(value.into())
}
}
}
2 changes: 1 addition & 1 deletion typespace/tests/output/test_crate_path_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl ::std::convert::TryFrom<&str> for Code {
value: &str,
) -> ::std::result::Result<Self, self::error::ConversionError> {
static PATTERN: ::std::sync::LazyLock<super::regex_engine::Regex> = ::std::sync::LazyLock::new(||
{ super::regex_engine::Regex::new("^x").unwrap() });
{ super::regex_engine::Regex::with_flags("^x", "u").unwrap() });
if PATTERN.find(value).is_none() {
return Err("doesn't match pattern \"^x\"".into());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ impl ::std::convert::TryFrom<&str> for ConstrainedString {
}
{
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(||
{ ::regress::Regex::new("^a").unwrap() });
{ ::regress::Regex::with_flags("^a", "u").unwrap() });
if PATTERN.find(value).is_none() {
return Err("doesn't match pattern \"^a\"".into());
}
}
{
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(||
{ ::regress::Regex::new("k$").unwrap() });
{ ::regress::Regex::with_flags("k$", "u").unwrap() });
if PATTERN.find(value).is_none() {
return Err("doesn't match pattern \"k$\"".into());
}
Expand Down
46 changes: 46 additions & 0 deletions typespace/tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6801,6 +6801,52 @@ fn array_constraints_need_a_sequence_inner_type() {
);
}

// JSON Schema patterns carry ECMA-262 unicode semantics (the `u` flag),
// so a unicode property escape matches characters, not bytes, and a
// `\u{..}` escape names a code point.
#[test]
fn string_constraint_patterns_have_unicode_semantics() {
let mut builder = TypespaceBuilder::new(
Settings::minimal()
.with_required_trait(TypespaceTrait::Serialize)
.with_required_trait(TypespaceTrait::Deserialize),
);
builder.insert("string".to_string(), Type::String).unwrap();
builder
.insert(
"Word".to_string(),
Type::NewtypeStruct(
NewtypeStruct::new("string".to_string())
.name("Word")
.constraints(NewtypeConstraints::String {
min: None,
max: None,
patterns: vec![r"^\p{Letter}+\u{1F354}?$".to_string()],
}),
),
)
.unwrap();

let ts = builder.finalize(no_cycles).unwrap();
let out = common::codespace(&ts).into_stream();

#[check_and_include(
"tests/output/string_constraint_patterns_have_unicode_semantics.rs",
out
)]
fn inner() {
use import::*;

assert!(Word::try_from("h\u{e9}llo").is_ok());
assert!(Word::try_from("h\u{e9}llo\u{1F354}").is_ok());
assert!(Word::try_from("123").is_err());
assert!(
Word::try_from("\u{1F354}").is_err(),
"the letters are required"
);
}
}

// A string constraint checks a `&str` and then constructs the inner value
// from it, which takes `String` or `FromStr`. A native that declares
// neither is rejected at finalize rather than in the consumer's build.
Expand Down
Loading