iref_core/iri/authority/
userinfo.rs1use pct_str::{PctStr, PctString};
2use std::{
3 cmp,
4 hash::{self, Hash},
5 ops,
6};
7
8use static_regular_grammar::RegularGrammar;
9
10use crate::common::authority::UserInofImpl;
11
12#[derive(RegularGrammar)]
13#[grammar(
14 file = "src/iri/grammar.abnf",
15 entry_point = "iuserinfo",
16 name = "IRI user info",
17 no_deref,
18 cache = "automata/iri/userinfo.aut.cbor"
19)]
20#[grammar(sized(
21 UserInfoBuf,
22 derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)
23))]
24#[cfg_attr(feature = "serde", grammar(serde))]
25#[cfg_attr(feature = "ignore-grammars", grammar(disable))]
26pub struct UserInfo(str);
27
28impl UserInofImpl for UserInfo {
29 unsafe fn new_unchecked(bytes: &[u8]) -> &Self {
30 Self::new_unchecked(std::str::from_utf8_unchecked(bytes))
31 }
32
33 fn as_bytes(&self) -> &[u8] {
34 self.0.as_bytes()
35 }
36}
37
38impl UserInfo {
39 #[inline]
41 pub fn as_pct_str(&self) -> &PctStr {
42 unsafe { PctStr::new_unchecked(self.as_str()) }
43 }
44}
45
46impl ops::Deref for UserInfo {
47 type Target = PctStr;
48
49 fn deref(&self) -> &Self::Target {
50 self.as_pct_str()
51 }
52}
53
54impl cmp::PartialEq for UserInfo {
55 #[inline]
56 fn eq(&self, other: &UserInfo) -> bool {
57 self.as_pct_str() == other.as_pct_str()
58 }
59}
60
61impl Eq for UserInfo {}
62
63impl<'a> PartialEq<&'a str> for UserInfo {
64 #[inline]
65 fn eq(&self, other: &&'a str) -> bool {
66 self.as_str() == *other
67 }
68}
69
70impl PartialOrd for UserInfo {
71 #[inline]
72 fn partial_cmp(&self, other: &UserInfo) -> Option<cmp::Ordering> {
73 Some(self.cmp(other))
74 }
75}
76
77impl Ord for UserInfo {
78 #[inline]
79 fn cmp(&self, other: &UserInfo) -> cmp::Ordering {
80 self.as_pct_str().cmp(other.as_pct_str())
81 }
82}
83
84impl Hash for UserInfo {
85 #[inline]
86 fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
87 self.as_pct_str().hash(hasher)
88 }
89}
90
91impl UserInfoBuf {
92 pub fn into_pct_string(self) -> PctString {
93 unsafe { PctString::new_unchecked(self.0) }
94 }
95}