@@ -445,8 +445,8 @@ macro_rules! to_string_render {
445
445
} ;
446
446
}
447
447
448
- fn plural_s ( value : usize ) -> & ' static str {
449
- if value == 1 {
448
+ fn plural_s < T : From < u8 > + PartialEq > ( value : T ) -> & ' static str {
449
+ if value == 1 . into ( ) {
450
450
""
451
451
} else {
452
452
"s"
@@ -494,8 +494,8 @@ impl ErrorType {
494
494
Self :: StringType { ..} => "Input should be a valid string" ,
495
495
Self :: StringSubType { ..} => "Input should be a string, not an instance of a subclass of str" ,
496
496
Self :: StringUnicode { ..} => "Input should be a valid string, unable to parse raw data as a unicode string" ,
497
- Self :: StringTooShort { ..} => "String should have at least {min_length} characters " ,
498
- Self :: StringTooLong { ..} => "String should have at most {max_length} characters " ,
497
+ Self :: StringTooShort { ..} => "String should have at least {min_length} character{expected_plural} " ,
498
+ Self :: StringTooLong { ..} => "String should have at most {max_length} character{expected_plural} " ,
499
499
Self :: StringPatternMismatch { ..} => "String should match pattern '{pattern}'" ,
500
500
Self :: Enum { ..} => "Input should be {expected}" ,
501
501
Self :: DictType { ..} => "Input should be a valid dictionary" ,
@@ -512,8 +512,8 @@ impl ErrorType {
512
512
Self :: FloatType { ..} => "Input should be a valid number" ,
513
513
Self :: FloatParsing { ..} => "Input should be a valid number, unable to parse string as a number" ,
514
514
Self :: BytesType { ..} => "Input should be a valid bytes" ,
515
- Self :: BytesTooShort { ..} => "Data should have at least {min_length} bytes " ,
516
- Self :: BytesTooLong { ..} => "Data should have at most {max_length} bytes " ,
515
+ Self :: BytesTooShort { ..} => "Data should have at least {min_length} byte{expected_plural} " ,
516
+ Self :: BytesTooLong { ..} => "Data should have at most {max_length} byte{expected_plural} " ,
517
517
Self :: ValueError { ..} => "Value error, {error}" ,
518
518
Self :: AssertionError { ..} => "Assertion failed, {error}" ,
519
519
Self :: CustomError { ..} => "" , // custom errors are handled separately
@@ -552,16 +552,16 @@ impl ErrorType {
552
552
Self :: UrlType { ..} => "URL input should be a string or URL" ,
553
553
Self :: UrlParsing { ..} => "Input should be a valid URL, {error}" ,
554
554
Self :: UrlSyntaxViolation { ..} => "Input violated strict URL syntax rules, {error}" ,
555
- Self :: UrlTooLong { ..} => "URL should have at most {max_length} characters " ,
555
+ Self :: UrlTooLong { ..} => "URL should have at most {max_length} character{expected_plural} " ,
556
556
Self :: UrlScheme { ..} => "URL scheme should be {expected_schemes}" ,
557
557
Self :: UuidType { ..} => "UUID input should be a string, bytes or UUID object" ,
558
558
Self :: UuidParsing { ..} => "Input should be a valid UUID, {error}" ,
559
559
Self :: UuidVersion { ..} => "UUID version {expected_version} expected" ,
560
560
Self :: DecimalType { ..} => "Decimal input should be an integer, float, string or Decimal object" ,
561
561
Self :: DecimalParsing { ..} => "Input should be a valid decimal" ,
562
- Self :: DecimalMaxDigits { ..} => "Decimal input should have no more than {max_digits} digits in total" ,
563
- Self :: DecimalMaxPlaces { ..} => "Decimal input should have no more than {decimal_places} decimal places " ,
564
- Self :: DecimalWholeDigits { ..} => "Decimal input should have no more than {whole_digits} digits before the decimal point" ,
562
+ Self :: DecimalMaxDigits { ..} => "Decimal input should have no more than {max_digits} digit{expected_plural} in total" ,
563
+ Self :: DecimalMaxPlaces { ..} => "Decimal input should have no more than {decimal_places} decimal place{expected_plural} " ,
564
+ Self :: DecimalWholeDigits { ..} => "Decimal input should have no more than {whole_digits} digit{expected_plural} before the decimal point" ,
565
565
}
566
566
}
567
567
@@ -643,13 +643,25 @@ impl ErrorType {
643
643
to_string_render ! ( tmpl, field_type, max_length, actual_length, expected_plural, )
644
644
}
645
645
Self :: IterationError { error, .. } => render ! ( tmpl, error) ,
646
- Self :: StringTooShort { min_length, .. } => to_string_render ! ( tmpl, min_length) ,
647
- Self :: StringTooLong { max_length, .. } => to_string_render ! ( tmpl, max_length) ,
646
+ Self :: StringTooShort { min_length, .. } => {
647
+ let expected_plural = plural_s ( * min_length) ;
648
+ to_string_render ! ( tmpl, min_length, expected_plural)
649
+ }
650
+ Self :: StringTooLong { max_length, .. } => {
651
+ let expected_plural = plural_s ( * max_length) ;
652
+ to_string_render ! ( tmpl, max_length, expected_plural)
653
+ }
648
654
Self :: StringPatternMismatch { pattern, .. } => render ! ( tmpl, pattern) ,
649
655
Self :: Enum { expected, .. } => to_string_render ! ( tmpl, expected) ,
650
656
Self :: MappingType { error, .. } => render ! ( tmpl, error) ,
651
- Self :: BytesTooShort { min_length, .. } => to_string_render ! ( tmpl, min_length) ,
652
- Self :: BytesTooLong { max_length, .. } => to_string_render ! ( tmpl, max_length) ,
657
+ Self :: BytesTooShort { min_length, .. } => {
658
+ let expected_plural = plural_s ( * min_length) ;
659
+ to_string_render ! ( tmpl, min_length, expected_plural)
660
+ }
661
+ Self :: BytesTooLong { max_length, .. } => {
662
+ let expected_plural = plural_s ( * max_length) ;
663
+ to_string_render ! ( tmpl, max_length, expected_plural)
664
+ }
653
665
Self :: ValueError { error, .. } => {
654
666
let error = & error
655
667
. as_ref ( )
@@ -688,13 +700,25 @@ impl ErrorType {
688
700
Self :: UnionTagNotFound { discriminator, .. } => render ! ( tmpl, discriminator) ,
689
701
Self :: UrlParsing { error, .. } => render ! ( tmpl, error) ,
690
702
Self :: UrlSyntaxViolation { error, .. } => render ! ( tmpl, error) ,
691
- Self :: UrlTooLong { max_length, .. } => to_string_render ! ( tmpl, max_length) ,
703
+ Self :: UrlTooLong { max_length, .. } => {
704
+ let expected_plural = plural_s ( * max_length) ;
705
+ to_string_render ! ( tmpl, max_length, expected_plural)
706
+ }
692
707
Self :: UrlScheme { expected_schemes, .. } => render ! ( tmpl, expected_schemes) ,
693
708
Self :: UuidParsing { error, .. } => render ! ( tmpl, error) ,
694
709
Self :: UuidVersion { expected_version, .. } => to_string_render ! ( tmpl, expected_version) ,
695
- Self :: DecimalMaxDigits { max_digits, .. } => to_string_render ! ( tmpl, max_digits) ,
696
- Self :: DecimalMaxPlaces { decimal_places, .. } => to_string_render ! ( tmpl, decimal_places) ,
697
- Self :: DecimalWholeDigits { whole_digits, .. } => to_string_render ! ( tmpl, whole_digits) ,
710
+ Self :: DecimalMaxDigits { max_digits, .. } => {
711
+ let expected_plural = plural_s ( * max_digits) ;
712
+ to_string_render ! ( tmpl, max_digits, expected_plural)
713
+ }
714
+ Self :: DecimalMaxPlaces { decimal_places, .. } => {
715
+ let expected_plural = plural_s ( * decimal_places) ;
716
+ to_string_render ! ( tmpl, decimal_places, expected_plural)
717
+ }
718
+ Self :: DecimalWholeDigits { whole_digits, .. } => {
719
+ let expected_plural = plural_s ( * whole_digits) ;
720
+ to_string_render ! ( tmpl, whole_digits, expected_plural)
721
+ }
698
722
_ => Ok ( tmpl. to_string ( ) ) ,
699
723
}
700
724
}
0 commit comments