bin.rs (3977B)
1 extern crate alloc; 2 use super::{ 3 super::super::bin::{Decode, Encode}, 4 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 Nickname<'_> { 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 [`Nickname::decode`]. 46 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 47 pub enum DecodeNicknameErr { 48 /// Variant returned when the encoded data could not be decoded 49 /// into a [`Nickname`]. 50 Nickname(NicknameErr), 51 /// Variant returned when the [`Nickname`] was not encoded 52 /// into its canonical form. 53 NotCanonical, 54 } 55 impl Display for DecodeNicknameErr { 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("Nickname was not encoded in its canonical form"), 61 } 62 } 63 } 64 impl Error for DecodeNicknameErr {} 65 impl<'b> Decode for Nickname<'b> { 66 type Input<'a> = &'b str; 67 type Err = DecodeNicknameErr; 68 #[inline] 69 fn decode(input: Self::Input<'_>) -> Result<Self, Self::Err> { 70 match Nickname::try_from(input).map_err(DecodeNicknameErr::Nickname) { 71 Ok(v) => match v.0 { 72 Cow::Borrowed(name) => { 73 if name == input { 74 Ok(Self(Cow::Borrowed(input))) 75 } else { 76 Err(DecodeNicknameErr::NotCanonical) 77 } 78 } 79 Cow::Owned(_) => Err(DecodeNicknameErr::NotCanonical), 80 }, 81 Err(e) => Err(e), 82 } 83 } 84 } 85 impl Encode for Username<'_> { 86 type Output<'a> 87 = &'a str 88 where 89 Self: 'a; 90 type Err = Infallible; 91 #[inline] 92 fn encode(&self) -> Result<Self::Output<'_>, Self::Err> { 93 Ok(self.as_ref()) 94 } 95 } 96 /// Error returned from [`Username::decode`]. 97 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 98 pub enum DecodeUsernameErr { 99 /// Variant returned when the encoded data could not be decoded 100 /// into a [`Username`]. 101 Username(UsernameErr), 102 /// Variant returned when the [`Username`] was not encoded 103 /// into its canonical form. 104 NotCanonical, 105 } 106 impl Display for DecodeUsernameErr { 107 #[inline] 108 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 109 match *self { 110 Self::Username(e) => e.fmt(f), 111 Self::NotCanonical => f.write_str("Username was not encoded in its canonical form"), 112 } 113 } 114 } 115 impl Error for DecodeUsernameErr {} 116 impl<'b> Decode for Username<'b> { 117 type Input<'a> = &'b str; 118 type Err = DecodeUsernameErr; 119 #[inline] 120 fn decode(input: Self::Input<'_>) -> Result<Self, Self::Err> { 121 match Username::try_from(input).map_err(DecodeUsernameErr::Username) { 122 Ok(v) => match v.0 { 123 Cow::Borrowed(name) => { 124 if name == input { 125 Ok(Self(Cow::Borrowed(input))) 126 } else { 127 Err(DecodeUsernameErr::NotCanonical) 128 } 129 } 130 Cow::Owned(_) => Err(DecodeUsernameErr::NotCanonical), 131 }, 132 Err(e) => Err(e), 133 } 134 } 135 }