uqa_core/relation_identity/
borrowed.rs1use std::{cmp::Ordering, fmt, str::Chars};
10
11#[derive(Debug, Clone, Copy)]
13pub struct RelationName<'a> {
14 text: &'a str,
15 escaped: bool,
16}
17
18impl<'a> RelationName<'a> {
19 pub fn decoded(text: &'a str) -> Self {
21 Self {
22 text,
23 escaped: false,
24 }
25 }
26
27 pub fn chars(self) -> impl Iterator<Item = char> + 'a {
29 DecodedChars {
30 chars: self.text.chars(),
31 escaped: self.escaped,
32 }
33 }
34}
35
36struct DecodedChars<'a> {
37 chars: Chars<'a>,
38 escaped: bool,
39}
40
41impl Iterator for DecodedChars<'_> {
42 type Item = char;
43 fn next(&mut self) -> Option<char> {
44 let character = self.chars.next()?;
45 if self.escaped && character == '"' {
46 self.chars.next();
47 }
48 Some(character)
49 }
50}
51
52impl PartialEq for RelationName<'_> {
53 fn eq(&self, other: &Self) -> bool {
54 self.chars().eq(other.chars())
55 }
56}
57impl Eq for RelationName<'_> {}
58impl PartialOrd for RelationName<'_> {
59 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
60 Some(self.cmp(other))
61 }
62}
63impl Ord for RelationName<'_> {
64 fn cmp(&self, other: &Self) -> Ordering {
65 self.chars().cmp(other.chars())
66 }
67}
68impl fmt::Display for RelationName<'_> {
69 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70 use fmt::Write as _;
71 for character in self.chars() {
72 formatter.write_char(character)?;
73 }
74 Ok(())
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub struct RelationReference<'a> {
81 pub schema: Option<RelationName<'a>>,
82 pub name: RelationName<'a>,
83}
84
85pub(super) enum InvalidReference {
86 Empty,
87 Unterminated,
88 Invalid,
89}
90
91impl InvalidReference {
92 pub(super) fn message(self, value: &str) -> String {
93 match self {
94 Self::Empty => "persisted relation name is empty".to_owned(),
95 Self::Unterminated => format!("unterminated quoted relation name `{value}`"),
96 Self::Invalid => format!("invalid persisted relation name `{value}`"),
97 }
98 }
99}
100
101pub(super) fn parse(value: &str) -> Result<RelationReference<'_>, InvalidReference> {
102 if value.is_empty() {
103 return Err(InvalidReference::Empty);
104 }
105 let mut remaining = value;
106 let first = component(&mut remaining)?;
107 if remaining.is_empty() {
108 return Ok(RelationReference {
109 schema: None,
110 name: first,
111 });
112 }
113 let name = component(&mut remaining)?;
114 if !remaining.is_empty() {
115 return Err(InvalidReference::Invalid);
116 }
117 Ok(RelationReference {
118 schema: Some(first),
119 name,
120 })
121}
122
123fn component<'a>(remaining: &mut &'a str) -> Result<RelationName<'a>, InvalidReference> {
124 let input = *remaining;
125 let quoted = input.starts_with('"');
126 let start = usize::from(quoted);
127 let bytes = input.as_bytes();
128 let mut index = start;
129 let end;
130 if quoted {
131 loop {
132 match bytes.get(index) {
133 None => return Err(InvalidReference::Unterminated),
134 Some(b'"') if bytes.get(index + 1) == Some(&b'"') => index += 2,
135 Some(b'"') => {
136 end = index;
137 index += 1;
138 break;
139 }
140 Some(_) => index += 1,
141 }
142 }
143 } else {
144 while let Some(byte) = bytes.get(index) {
145 match byte {
146 b'.' => break,
147 b'"' => return Err(InvalidReference::Invalid),
148 _ => index += 1,
149 }
150 }
151 end = index;
152 }
153 if end == start {
154 return Err(InvalidReference::Invalid);
155 }
156 *remaining = match bytes.get(index) {
157 None => "",
158 Some(b'.') if index + 1 < bytes.len() => &input[index + 1..],
159 _ => return Err(InvalidReference::Invalid),
160 };
161 Ok(RelationName {
162 text: &input[start..end],
163 escaped: quoted,
164 })
165}