bin.rs (4216B)
1 extern crate alloc; 2 use super::{ 3 super::super::bin::{Decode, Encode}, 4 DisplayName, Nickname, NicknameErr, UserHandle, Username, UsernameErr, 5 }; 6 use alloc::borrow::Cow; 7 use core::{ 8 convert::Infallible, 9 error::Error, 10 fmt::{self, Display, Formatter}, 11 }; 12 impl<const LEN: usize> Encode for UserHandle<LEN> { 13 type Output<'a> 14 = [u8; LEN] 15 where 16 Self: 'a; 17 type Err = Infallible; 18 #[inline] 19 fn encode(&self) -> Result<Self::Output<'_>, Self::Err> { 20 Ok(self.0) 21 } 22 } 23 impl<const LEN: usize> Decode for UserHandle<LEN> 24 where 25 Self: Default, 26 { 27 type Input<'a> = [u8; LEN]; 28 type Err = Infallible; 29 #[inline] 30 fn decode(input: Self::Input<'_>) -> Result<Self, Self::Err> { 31 Ok(Self(input)) 32 } 33 } 34 impl Encode for DisplayName<'_> { 35 type Output<'a> 36 = &'a str 37 where 38 Self: 'a; 39 type Err = Infallible; 40 #[inline] 41 fn encode(&self) -> Result<Self::Output<'_>, Self::Err> { 42 Ok(self.as_ref()) 43 } 44 } 45 /// Error returned from [`DisplayName::decode`]. 46 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 47 pub enum DecodeDisplayNameErr { 48 /// Variant returned when the encoded data could not be decoded 49 /// into a [`DisplayName`]. 50 Nickname(NicknameErr), 51 /// Variant returned when the [`DisplayName`] was not encoded 52 /// into its canonical form. 53 NotCanonical, 54 } 55 impl Display for DecodeDisplayNameErr { 56 #[inline] 57 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 58 match *self { 59 Self::Nickname(e) => e.fmt(f), 60 Self::NotCanonical => f.write_str("DisplayName was not encoded in its canonical form"), 61 } 62 } 63 } 64 impl Error for DecodeDisplayNameErr {} 65 impl<'b> Decode for DisplayName<'b> { 66 type Input<'a> = &'b str; 67 type Err = DecodeDisplayNameErr; 68 #[inline] 69 fn decode(input: Self::Input<'_>) -> Result<Self, Self::Err> { 70 match DisplayName::try_from(input).map_err(DecodeDisplayNameErr::Nickname) { 71 Ok(v) => match v { 72 DisplayName::Blank => Ok(Self::Blank), 73 DisplayName::Nickname(name) => match name.0 { 74 Cow::Borrowed(val) => { 75 if val == input { 76 Ok(Self::Nickname(Nickname(Cow::Borrowed(input)))) 77 } else { 78 Err(DecodeDisplayNameErr::NotCanonical) 79 } 80 } 81 Cow::Owned(_) => Err(DecodeDisplayNameErr::NotCanonical), 82 }, 83 }, 84 Err(e) => Err(e), 85 } 86 } 87 } 88 impl Encode for Username<'_> { 89 type Output<'a> 90 = &'a str 91 where 92 Self: 'a; 93 type Err = Infallible; 94 #[inline] 95 fn encode(&self) -> Result<Self::Output<'_>, Self::Err> { 96 Ok(self.as_ref()) 97 } 98 } 99 /// Error returned from [`Username::decode`]. 100 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 101 pub enum DecodeUsernameErr { 102 /// Variant returned when the encoded data could not be decoded 103 /// into a [`Username`]. 104 Username(UsernameErr), 105 /// Variant returned when the [`Username`] was not encoded 106 /// into its canonical form. 107 NotCanonical, 108 } 109 impl Display for DecodeUsernameErr { 110 #[inline] 111 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 112 match *self { 113 Self::Username(e) => e.fmt(f), 114 Self::NotCanonical => f.write_str("Username was not encoded in its canonical form"), 115 } 116 } 117 } 118 impl Error for DecodeUsernameErr {} 119 impl<'b> Decode for Username<'b> { 120 type Input<'a> = &'b str; 121 type Err = DecodeUsernameErr; 122 #[inline] 123 fn decode(input: Self::Input<'_>) -> Result<Self, Self::Err> { 124 match Username::try_from(input).map_err(DecodeUsernameErr::Username) { 125 Ok(v) => match v.0 { 126 Cow::Borrowed(name) => { 127 if name == input { 128 Ok(Self(Cow::Borrowed(input))) 129 } else { 130 Err(DecodeUsernameErr::NotCanonical) 131 } 132 } 133 Cow::Owned(_) => Err(DecodeUsernameErr::NotCanonical), 134 }, 135 Err(e) => Err(e), 136 } 137 } 138 }