dbml_rs/ast/
unit.rs

1use alloc::string::{
2  String,
3  ToString,
4};
5use core::str::FromStr;
6
7use super::SpanRange;
8
9/// Represents a string literal.
10#[derive(Debug, Clone)]
11pub struct Literal {
12  /// The range of the span in the source text.
13  pub span_range: SpanRange,
14  /// The value associated with the literal.
15  pub raw: String,
16  /// The value associated with the literal.
17  pub value: Value,
18}
19
20/// Represents an identifier.
21#[derive(Debug, Clone, Default)]
22pub struct Ident {
23  /// The range of the span in the source text.
24  pub span_range: SpanRange,
25  /// The raw string value of the identifier.
26  pub raw: String,
27  /// The string representation of the identifier.
28  pub to_string: String,
29}
30
31/// Represents an attribute with a key-value pair. It can have solely key without specified any value.
32#[derive(Debug, Clone, Default)]
33pub struct Attribute {
34  /// The range of the span in the source text.
35  pub span_range: SpanRange,
36  /// The key of the attribute.
37  pub key: Ident,
38  /// The value associated with the attribute, if any.
39  pub value: Option<Literal>,
40}
41
42/// Represents a key-value property.
43#[derive(Debug, Clone)]
44pub struct Property {
45  /// The range of the span in the source text.
46  pub span_range: SpanRange,
47  /// Identifier representing the key of the property.
48  pub key: Ident,
49  /// Literal value associated with the property.
50  pub value: Literal,
51}
52
53/// Represents whether a value is explicitly specified as either null or not null.
54#[derive(Debug, PartialEq, Eq, Clone)]
55pub enum Nullable {
56  NotNull,
57  Null,
58}
59
60/// Represents settings and arguments values.
61#[derive(Debug, PartialEq, Clone)]
62pub enum Value {
63  Enum(String),
64  String(String),
65  Integer(i64),
66  Decimal(f64),
67  Bool(bool),
68  HexColor(String),
69  Expr(String),
70  Null,
71}
72
73impl FromStr for Value {
74  type Err = ();
75
76  fn from_str(s: &str) -> Result<Self, Self::Err> {
77    match s {
78      "true" => Ok(Value::Bool(true)),
79      "false" => Ok(Value::Bool(false)),
80      "null" => Ok(Value::Null),
81      _ => Err(()),
82    }
83  }
84}
85
86impl ToString for Value {
87  fn to_string(&self) -> String {
88    match self {
89      Self::Enum(v) => v.clone(),
90      Self::String(v) => v.clone(),
91      Self::Integer(v) => format!("{v}"),
92      Self::Decimal(v) => format!("{v}"),
93      Self::Bool(v) => format!("{v}"),
94      Self::HexColor(v) => v.clone(),
95      Self::Expr(v) => v.clone(),
96      Self::Null => "null".to_string(),
97    }
98  }
99}
100
101/// Represents a note block.
102#[derive(Debug, Clone)]
103pub struct NoteBlock {
104  /// The range of the span in the source text.
105  pub span_range: SpanRange,
106  /// The literal value associated with the note block. It must be a string literal.
107  pub value: Literal,
108}