From 024963dc7f60b107bc218476dda591085eafd2fa Mon Sep 17 00:00:00 2001 From: joshua Date: Wed, 29 Jan 2025 18:23:06 -0500 Subject: [PATCH] Add Spanned to Annotation --- rust/annotation.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/rust/annotation.rs b/rust/annotation.rs index 3aa5e31b4..f0bc95cd3 100644 --- a/rust/annotation.rs +++ b/rust/annotation.rs @@ -7,10 +7,11 @@ use std::fmt::{self, Write}; use crate::{ - common::{identifier::Identifier, token, Span}, + common::{identifier::Identifier, Span, token}, util::write_joined, value::{IntegerLiteral, Literal, StringLiteral}, }; +use crate::common::Spanned; #[derive(Debug, Clone, Eq, PartialEq)] pub enum Annotation { @@ -27,6 +28,24 @@ pub enum Annotation { Values(Values), } +impl Spanned for Annotation { + fn span(&self) -> Option { + match self { + Annotation::Abstract(annotation) => annotation.span(), + Annotation::Cardinality(annotation) => annotation.span(), + Annotation::Cascade(annotation) => annotation.span(), + Annotation::Distinct(annotation) => annotation.span(), + Annotation::Independent(annotation) => annotation.span(), + Annotation::Key(annotation) => annotation.span(), + Annotation::Range(annotation) => annotation.span(), + Annotation::Regex(annotation) => annotation.span(), + Annotation::Subkey(annotation) => annotation.span(), + Annotation::Unique(annotation) => annotation.span(), + Annotation::Values(annotation) => annotation.span(), + } + } +} + impl fmt::Display for Annotation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -56,6 +75,12 @@ impl Abstract { } } +impl Spanned for Abstract { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Abstract { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Abstract) @@ -74,6 +99,12 @@ impl Cardinality { } } +impl Spanned for Cardinality { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Cardinality { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}({})", token::Annotation::Cardinality, self.range) @@ -107,6 +138,12 @@ impl Cascade { } } +impl Spanned for Cascade { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Cascade { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Cascade) @@ -124,6 +161,12 @@ impl Distinct { } } +impl Spanned for Distinct { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Distinct { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Distinct) @@ -141,6 +184,12 @@ impl Independent { } } +impl Spanned for Independent { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Independent { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Independent) @@ -158,6 +207,12 @@ impl Key { } } +impl Spanned for Key { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Key { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Key) @@ -177,6 +232,12 @@ impl Range { } } +impl Spanned for Range { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Range { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}(", token::Annotation::Range)?; @@ -204,6 +265,12 @@ impl Regex { } } +impl Spanned for Regex { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Regex { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}({})", token::Annotation::Regex, self.regex.value) @@ -222,6 +289,12 @@ impl Subkey { } } +impl Spanned for Subkey { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Subkey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}({})", token::Annotation::Subkey, self.ident) @@ -239,6 +312,12 @@ impl Unique { } } +impl Spanned for Unique { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Unique { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", token::Annotation::Unique) @@ -257,6 +336,12 @@ impl Values { } } +impl Spanned for Values { + fn span(&self) -> Option { + self.span + } +} + impl fmt::Display for Values { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}(", token::Annotation::Values)?;