Skip to content

Commit

Permalink
Merge pull request #93 from yassun7010/use_cow
Browse files Browse the repository at this point in the history
feat: use cow to error key.
  • Loading branch information
yassun7010 authored Jan 9, 2025
2 parents 661293b + 47ffb27 commit 442af40
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/serde_valid/src/features/fluent/localize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Localize for PropertyErrorsMap<crate::validation::Error> {
M: fluent::memoizer::MemoizerKind,
{
self.iter()
.map(|(property, error)| (property.to_string(), error.localize(bundle)))
.map(|(property, error)| ((*property).clone(), error.localize(bundle)))
.collect()
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/serde_valid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ pub use error::{
#[allow(unused_imports)]
pub use features::*;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};
pub use validation::{
ValidateEnumerate, ValidateExclusiveMaximum, ValidateExclusiveMinimum, ValidateMaxItems,
ValidateMaxLength, ValidateMaxProperties, ValidateMaximum, ValidateMinItems, ValidateMinLength,
Expand Down Expand Up @@ -661,7 +661,7 @@ where

for (key, value) in self.iter() {
if let Err(errors) = value.validate() {
items.insert(key.into(), errors);
items.insert(Cow::from(key.into()), errors);
}
}

Expand All @@ -685,7 +685,7 @@ where

for (key, value) in self.iter() {
if let Err(errors) = value.validate() {
items.insert(key.into(), errors);
items.insert(Cow::from(key.into()), errors);
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/serde_valid/src/validation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod into_error;
mod message;
mod object_errors;

use std::borrow::Cow;

pub use crate::error::{
EnumerateError, ExclusiveMaximumError, ExclusiveMinimumError, MaxItemsError, MaxLengthError,
MaxPropertiesError, MaximumError, MinItemsError, MinLengthError, MinPropertiesError,
Expand Down Expand Up @@ -104,5 +106,5 @@ where
pub type VecErrors<E = crate::validation::Error> = Vec<E>;
pub type ItemErrorsMap<E> = IndexMap<usize, Errors<E>>;
pub type ItemVecErrorsMap<E> = IndexMap<usize, VecErrors<E>>;
pub type PropertyErrorsMap<E> = IndexMap<String, Errors<E>>;
pub type PropertyVecErrorsMap<E> = IndexMap<String, VecErrors<E>>;
pub type PropertyErrorsMap<E> = IndexMap<Cow<'static, str>, Errors<E>>;
pub type PropertyVecErrorsMap<E> = IndexMap<Cow<'static, str>, VecErrors<E>>;
2 changes: 1 addition & 1 deletion crates/serde_valid_derive/src/serde/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn collect_serde_rename_map(fields: &syn::FieldsNamed) -> RenameMap {
if let Some(rename) = find_rename_from_serde_attributes(attribute) {
renames.insert(
field.ident.to_token_stream().to_string(),
quote!(#rename.to_string()),
quote!(std::borrow::Cow::from(#rename)),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/serde_valid_derive/src/types/field/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Field for NamedField<'_> {

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

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

0 comments on commit 442af40

Please sign in to comment.