Skip to content

Commit

Permalink
feat: enum value settings
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 12, 2024
1 parent 4238391 commit 11c8820
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 200 deletions.
4 changes: 3 additions & 1 deletion src/analyzer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl Indexer {

let mut value_sets = BTreeSet::new();
for value in r#enum.values.iter() {
check_attr_duplicate_keys(&value.attributes, input)?;
if let Some(settings) = &value.settings {
check_attr_duplicate_keys(&settings.attributes, input)?;
}

if !value_sets.insert(value.value.to_string.clone()) {
throw_err(Err::DuplicateEnumValue, &value.span_range, input)?;
Expand Down
11 changes: 10 additions & 1 deletion src/ast/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ pub struct EnumBlock {
pub struct EnumValue {
/// The range of the span in the source text.
pub span_range: SpanRange,
pub attributes: Vec<Attribute>,
pub value: Ident,
pub settings: Option<EnumValueSettings>,
}

#[derive(Debug, Clone, Default)]
pub struct EnumValueSettings {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A vector of key and optional value pairs representing attributes of the enum value.
pub attributes: Vec<Attribute>,
/// A note associated with the enum value.
pub note: Option<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl FromStr for ColumnTypeName {
pub struct ColumnSettings {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A vector of key-value pairs representing properties of the column.
/// A vector of key and optional value pairs representing attributes of the column.
pub attributes: Vec<Attribute>,
/// A boolean indicating if the column is a primary key.
pub is_pk: bool,
Expand Down
11 changes: 9 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,28 @@ fn parse_enum_value(pair: Pair<Rule>) -> ParserResult<EnumValue> {
match p1.as_rule() {
Rule::ident => acc.value = parse_ident(p1)?,
Rule::enum_settings => {
let mut settings = EnumValueSettings {
span_range: s2r(p1.as_span()),
..Default::default()
};

for p2 in p1.into_inner() {
match p2.as_rule() {
Rule::attribute => {
let attr = parse_attribute(p2)?;

match attr.key.to_string.as_str() {
"note" => acc.note = attr.value.clone().map(|v| v.value.to_string()),
"note" => settings.note = attr.value.clone().map(|v| v.value.to_string()),
_ => (),
}

acc.attributes.push(attr);
settings.attributes.push(attr);
}
_ => throw_rules(&[Rule::attribute], p2)?,
}
}

acc.settings = Some(settings);
}
_ => throw_rules(&[Rule::ident, Rule::enum_settings], p1)?,
}
Expand Down
97 changes: 53 additions & 44 deletions tests/out/comment.in.ron
Original file line number Diff line number Diff line change
Expand Up @@ -290,72 +290,81 @@ SchemaBlock {
values: [
EnumValue {
span_range: 338..353,
attributes: [],
value: Ident {
span_range: 338..350,
raw: "out_of_stock",
to_string: "out_of_stock",
},
note: None,
settings: None,
},
EnumValue {
span_range: 353..380,
attributes: [
Attribute {
span_range: 363..379,
key: Ident {
span_range: 363..367,
raw: "note",
to_string: "note",
},
value: Some(
Literal {
span_range: 369..379,
raw: "'In stock'",
value: String(
"In stock",
),
},
),
},
],
value: Ident {
span_range: 353..361,
raw: "in_stock",
to_string: "in_stock",
},
note: Some(
"In stock",
),
},
EnumValue {
span_range: 383..417,
attributes: [
Attribute {
span_range: 396..416,
key: Ident {
span_range: 396..400,
raw: "note",
to_string: "note",
},
value: Some(
Literal {
span_range: 402..416,
raw: "'less than 20'",
value: String(
"less than 20",
settings: Some(
EnumValueSettings {
span_range: 362..380,
attributes: [
Attribute {
span_range: 363..379,
key: Ident {
span_range: 363..367,
raw: "note",
to_string: "note",
},
value: Some(
Literal {
span_range: 369..379,
raw: "'In stock'",
value: String(
"In stock",
),
},
),
},
],
note: Some(
"In stock",
),
},
],
),
},
EnumValue {
span_range: 383..417,
value: Ident {
span_range: 383..394,
raw: "running_low",
to_string: "running_low",
},
note: Some(
"less than 20",
settings: Some(
EnumValueSettings {
span_range: 395..417,
attributes: [
Attribute {
span_range: 396..416,
key: Ident {
span_range: 396..400,
raw: "note",
to_string: "note",
},
value: Some(
Literal {
span_range: 402..416,
raw: "'less than 20'",
value: String(
"less than 20",
),
},
),
},
],
note: Some(
"less than 20",
),
},
),
},
],
Expand Down
Loading

0 comments on commit 11c8820

Please sign in to comment.