|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +use std::fmt::{self, Write}; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + common::{identifier::Identifier, token, Span}, |
| 11 | + util::write_joined, |
| 12 | + value::Literal, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 16 | +pub enum Annotation { |
| 17 | + Abstract(Abstract), |
| 18 | + Cardinality(Cardinality), |
| 19 | + Cascade(Cascade), |
| 20 | + Distinct(Distinct), |
| 21 | + Independent(Independent), |
| 22 | + Key(Key), |
| 23 | + Range(Range), |
| 24 | + Regex(Regex), |
| 25 | + Subkey(Subkey), |
| 26 | + Unique(Unique), |
| 27 | + Values(Values), |
| 28 | +} |
| 29 | + |
| 30 | +impl fmt::Display for Annotation { |
| 31 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 32 | + match self { |
| 33 | + Self::Abstract(inner) => fmt::Display::fmt(inner, f), |
| 34 | + Self::Cardinality(inner) => fmt::Display::fmt(inner, f), |
| 35 | + Self::Cascade(inner) => fmt::Display::fmt(inner, f), |
| 36 | + Self::Distinct(inner) => fmt::Display::fmt(inner, f), |
| 37 | + Self::Independent(inner) => fmt::Display::fmt(inner, f), |
| 38 | + Self::Key(inner) => fmt::Display::fmt(inner, f), |
| 39 | + Self::Range(inner) => fmt::Display::fmt(inner, f), |
| 40 | + Self::Regex(inner) => fmt::Display::fmt(inner, f), |
| 41 | + Self::Subkey(inner) => fmt::Display::fmt(inner, f), |
| 42 | + Self::Unique(inner) => fmt::Display::fmt(inner, f), |
| 43 | + Self::Values(inner) => fmt::Display::fmt(inner, f), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 49 | +pub struct Abstract { |
| 50 | + span: Option<Span>, |
| 51 | +} |
| 52 | + |
| 53 | +impl Abstract { |
| 54 | + pub fn new(span: Option<Span>) -> Self { |
| 55 | + Self { span } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl fmt::Display for Abstract { |
| 60 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 61 | + write!(f, "@{}", token::Annotation::Abstract) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 66 | +pub struct Cardinality { |
| 67 | + span: Option<Span>, |
| 68 | + min: Literal, |
| 69 | + max: Option<Literal>, |
| 70 | +} |
| 71 | + |
| 72 | +impl Cardinality { |
| 73 | + pub fn new(span: Option<Span>, min: Literal, max: Option<Literal>) -> Self { |
| 74 | + Self { span, min, max } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl fmt::Display for Cardinality { |
| 79 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 80 | + write!(f, "@{}({}..", token::Annotation::Cardinality, self.min)?; |
| 81 | + if let Some(max) = &self.max { |
| 82 | + write!(f, "{}", max)?; |
| 83 | + } |
| 84 | + f.write_char(')')?; |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 90 | +pub struct Cascade { |
| 91 | + span: Option<Span>, |
| 92 | +} |
| 93 | + |
| 94 | +impl Cascade { |
| 95 | + pub fn new(span: Option<Span>) -> Self { |
| 96 | + Self { span } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl fmt::Display for Cascade { |
| 101 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 102 | + write!(f, "@{}", token::Annotation::Cascade) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 107 | +pub struct Distinct { |
| 108 | + span: Option<Span>, |
| 109 | +} |
| 110 | + |
| 111 | +impl Distinct { |
| 112 | + pub fn new(span: Option<Span>) -> Self { |
| 113 | + Self { span } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl fmt::Display for Distinct { |
| 118 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 119 | + write!(f, "@{}", token::Annotation::Distinct) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 124 | +pub struct Independent { |
| 125 | + span: Option<Span>, |
| 126 | +} |
| 127 | + |
| 128 | +impl Independent { |
| 129 | + pub fn new(span: Option<Span>) -> Self { |
| 130 | + Self { span } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl fmt::Display for Independent { |
| 135 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 136 | + write!(f, "@{}", token::Annotation::Independent) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 141 | +pub struct Key { |
| 142 | + span: Option<Span>, |
| 143 | +} |
| 144 | + |
| 145 | +impl Key { |
| 146 | + pub fn new(span: Option<Span>) -> Self { |
| 147 | + Self { span } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl fmt::Display for Key { |
| 152 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 153 | + write!(f, "@{}", token::Annotation::Key) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 158 | +pub struct Range { |
| 159 | + span: Option<Span>, |
| 160 | + min: Option<Literal>, |
| 161 | + max: Option<Literal>, |
| 162 | +} |
| 163 | + |
| 164 | +impl Range { |
| 165 | + pub fn new(span: Option<Span>, min: Option<Literal>, max: Option<Literal>) -> Self { |
| 166 | + Self { span, min, max } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +impl fmt::Display for Range { |
| 171 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 172 | + write!(f, "@{}(", token::Annotation::Range)?; |
| 173 | + if let Some(min) = &self.min { |
| 174 | + write!(f, "{}", min)?; |
| 175 | + } |
| 176 | + f.write_str("..")?; |
| 177 | + if let Some(max) = &self.max { |
| 178 | + write!(f, "{}", max)?; |
| 179 | + } |
| 180 | + f.write_char(')')?; |
| 181 | + Ok(()) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 186 | +pub struct Regex { |
| 187 | + span: Option<Span>, |
| 188 | + regex: Literal, |
| 189 | +} |
| 190 | + |
| 191 | +impl Regex { |
| 192 | + pub fn new(span: Option<Span>, regex: Literal) -> Self { |
| 193 | + Self { span, regex } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +impl fmt::Display for Regex { |
| 198 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 199 | + write!(f, "@{}({})", token::Annotation::Regex, self.regex) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 204 | +pub struct Subkey { |
| 205 | + span: Option<Span>, |
| 206 | + ident: Identifier, |
| 207 | +} |
| 208 | + |
| 209 | +impl Subkey { |
| 210 | + pub fn new(span: Option<Span>, ident: Identifier) -> Self { |
| 211 | + Self { span, ident } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl fmt::Display for Subkey { |
| 216 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 217 | + write!(f, "@{}({})", token::Annotation::Subkey, self.ident) |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 222 | +pub struct Unique { |
| 223 | + span: Option<Span>, |
| 224 | +} |
| 225 | + |
| 226 | +impl Unique { |
| 227 | + pub fn new(span: Option<Span>) -> Self { |
| 228 | + Self { span } |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +impl fmt::Display for Unique { |
| 233 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 234 | + write!(f, "@{}", token::Annotation::Unique) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 239 | +pub struct Values { |
| 240 | + span: Option<Span>, |
| 241 | + values: Vec<Literal>, |
| 242 | +} |
| 243 | + |
| 244 | +impl Values { |
| 245 | + pub fn new(span: Option<Span>, values: Vec<Literal>) -> Self { |
| 246 | + Self { span, values } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +impl fmt::Display for Values { |
| 251 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 252 | + write!(f, "@{}(", token::Annotation::Values)?; |
| 253 | + write_joined!(f, ", ", &self.values)?; |
| 254 | + f.write_char(')')?; |
| 255 | + Ok(()) |
| 256 | + } |
| 257 | +} |
0 commit comments