Skip to content

Commit 47ffb27

Browse files
committed
feat: use cow to error key.
1 parent 661293b commit 47ffb27

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

crates/serde_valid/src/features/fluent/localize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Localize for PropertyErrorsMap<crate::validation::Error> {
8989
M: fluent::memoizer::MemoizerKind,
9090
{
9191
self.iter()
92-
.map(|(property, error)| (property.to_string(), error.localize(bundle)))
92+
.map(|(property, error)| ((*property).clone(), error.localize(bundle)))
9393
.collect()
9494
}
9595
}

crates/serde_valid/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ pub use error::{
586586
#[allow(unused_imports)]
587587
pub use features::*;
588588
use indexmap::IndexMap;
589-
use std::collections::HashMap;
589+
use std::{borrow::Cow, collections::HashMap};
590590
pub use validation::{
591591
ValidateEnumerate, ValidateExclusiveMaximum, ValidateExclusiveMinimum, ValidateMaxItems,
592592
ValidateMaxLength, ValidateMaxProperties, ValidateMaximum, ValidateMinItems, ValidateMinLength,
@@ -661,7 +661,7 @@ where
661661

662662
for (key, value) in self.iter() {
663663
if let Err(errors) = value.validate() {
664-
items.insert(key.into(), errors);
664+
items.insert(Cow::from(key.into()), errors);
665665
}
666666
}
667667

@@ -685,7 +685,7 @@ where
685685

686686
for (key, value) in self.iter() {
687687
if let Err(errors) = value.validate() {
688-
items.insert(key.into(), errors);
688+
items.insert(Cow::from(key.into()), errors);
689689
}
690690
}
691691

crates/serde_valid/src/validation/error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod into_error;
55
mod message;
66
mod object_errors;
77

8+
use std::borrow::Cow;
9+
810
pub use crate::error::{
911
EnumerateError, ExclusiveMaximumError, ExclusiveMinimumError, MaxItemsError, MaxLengthError,
1012
MaxPropertiesError, MaximumError, MinItemsError, MinLengthError, MinPropertiesError,
@@ -104,5 +106,5 @@ where
104106
pub type VecErrors<E = crate::validation::Error> = Vec<E>;
105107
pub type ItemErrorsMap<E> = IndexMap<usize, Errors<E>>;
106108
pub type ItemVecErrorsMap<E> = IndexMap<usize, VecErrors<E>>;
107-
pub type PropertyErrorsMap<E> = IndexMap<String, Errors<E>>;
108-
pub type PropertyVecErrorsMap<E> = IndexMap<String, VecErrors<E>>;
109+
pub type PropertyErrorsMap<E> = IndexMap<Cow<'static, str>, Errors<E>>;
110+
pub type PropertyVecErrorsMap<E> = IndexMap<Cow<'static, str>, VecErrors<E>>;

crates/serde_valid_derive/src/serde/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn collect_serde_rename_map(fields: &syn::FieldsNamed) -> RenameMap {
1515
if let Some(rename) = find_rename_from_serde_attributes(attribute) {
1616
renames.insert(
1717
field.ident.to_token_stream().to_string(),
18-
quote!(#rename.to_string()),
18+
quote!(std::borrow::Cow::from(#rename)),
1919
);
2020
}
2121
}

crates/serde_valid_derive/src/types/field/named.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Field for NamedField<'_> {
3333

3434
fn key(&self) -> proc_macro2::TokenStream {
3535
let name = &self.name;
36-
quote!(#name.to_string())
36+
quote!(std::borrow::Cow::from(#name))
3737
}
3838

3939
fn errors_variable(&self) -> proc_macro2::TokenStream {

0 commit comments

Comments
 (0)