garde/rules/length/
utf16.rs1use crate::error::Error;
4
5pub fn apply<T: Utf16CodeUnits>(v: &T, (min, max): (usize, usize)) -> Result<(), Error> {
6 v.validate_num_code_units(min, max)
7}
8
9pub trait Utf16CodeUnits {
10 fn validate_num_code_units(&self, min: usize, max: usize) -> Result<(), Error>;
11}
12
13impl<T: HasUtf16CodeUnits> Utf16CodeUnits for T {
14 fn validate_num_code_units(&self, min: usize, max: usize) -> Result<(), Error> {
15 super::check_len(self.num_code_units(), min, max)
16 }
17}
18
19impl<T: Utf16CodeUnits> Utf16CodeUnits for Option<T> {
20 fn validate_num_code_units(&self, min: usize, max: usize) -> Result<(), Error> {
21 match self {
22 Some(v) => v.validate_num_code_units(min, max),
23 None => Ok(()),
24 }
25 }
26}
27
28pub trait HasUtf16CodeUnits {
29 fn num_code_units(&self) -> usize;
30}
31
32macro_rules! impl_str {
33 ($(in<$lifetime:lifetime>)? $T:ty) => {
34 impl<$($lifetime)?> HasUtf16CodeUnits for $T {
35 fn num_code_units(&self) -> usize {
36 self.encode_utf16().count()
37 }
38 }
39 };
40}
41
42impl_str!(std::string::String);
43impl_str!(in<'a> &'a std::string::String);
44impl_str!(in<'a> &'a str);
45impl_str!(in<'a> std::borrow::Cow<'a, str>);
46impl_str!(std::rc::Rc<str>);
47impl_str!(std::sync::Arc<str>);
48impl_str!(std::boxed::Box<str>);