Skip to content

Commit 5e3544f

Browse files
committed
Use fmt::Display trait for writing dbscheme
1 parent a7a18b8 commit 5e3544f

File tree

2 files changed

+80
-62
lines changed

2 files changed

+80
-62
lines changed

generator/src/dbscheme.rs

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::fmt;
2+
13
/// Represents a distinct entry in the database schema.
24
pub enum Entry {
35
/// An entry defining a database table.
46
Table(Table),
57

68
/// An entry defining type that is a union of other types.
7-
Union { name: String, members: Vec<String> },
9+
Union(Union),
810
}
911

1012
/// A table in the database schema.
@@ -14,6 +16,12 @@ pub struct Table {
1416
pub keysets: Option<Vec<String>>,
1517
}
1618

19+
/// A union in the database schema.
20+
pub struct Union {
21+
pub name: String,
22+
pub members: Vec<String>,
23+
}
24+
1725
/// A column in a table.
1826
pub struct Column {
1927
pub db_type: DbColumnType,
@@ -101,6 +109,69 @@ pub fn escape_name(name: &str) -> String {
101109
result
102110
}
103111

112+
impl fmt::Display for Table {
113+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114+
for keyset in &self.keysets {
115+
write!(f, "#keyset[")?;
116+
for (key_index, key) in keyset.iter().enumerate() {
117+
if key_index > 0 {
118+
write!(f, ", ")?;
119+
}
120+
write!(f, "{}", key)?;
121+
}
122+
write!(f, "]\n")?;
123+
}
124+
125+
write!(f, "{}(\n", self.name)?;
126+
for (column_index, column) in self.columns.iter().enumerate() {
127+
write!(f, " ")?;
128+
if column.unique {
129+
write!(f, "unique ")?;
130+
}
131+
write!(
132+
f,
133+
"{} ",
134+
match column.db_type {
135+
DbColumnType::Int => "int",
136+
DbColumnType::String => "string",
137+
}
138+
)?;
139+
write!(f, "{}: ", column.name)?;
140+
match &column.ql_type {
141+
QlColumnType::Int => write!(f, "int")?,
142+
QlColumnType::String => write!(f, "string")?,
143+
QlColumnType::Custom(name) => write!(f, "@{}", name)?,
144+
}
145+
if column.ql_type_is_ref {
146+
write!(f, " ref")?;
147+
}
148+
if column_index + 1 != self.columns.len() {
149+
write!(f, ",")?;
150+
}
151+
write!(f, "\n")?;
152+
}
153+
write!(f, ");")?;
154+
155+
Ok(())
156+
}
157+
}
158+
159+
impl fmt::Display for Union {
160+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161+
write!(f, "@{} = ", self.name)?;
162+
let mut first = true;
163+
for member in &self.members {
164+
if first {
165+
first = false;
166+
} else {
167+
write!(f, " | ")?;
168+
}
169+
write!(f, "@{}", member)?;
170+
}
171+
Ok(())
172+
}
173+
}
174+
104175
/// Generates the dbscheme by writing the given dbscheme `entries` to the `file`.
105176
pub fn write(
106177
language_name: &str,
@@ -115,61 +186,8 @@ pub fn write(
115186

116187
for entry in entries {
117188
match entry {
118-
Entry::Table(table) => {
119-
for keyset in &table.keysets {
120-
write!(file, "#keyset[")?;
121-
for (key_index, key) in keyset.iter().enumerate() {
122-
if key_index > 0 {
123-
write!(file, ", ")?;
124-
}
125-
write!(file, "{}", key)?;
126-
}
127-
write!(file, "]\n")?;
128-
}
129-
130-
write!(file, "{}(\n", table.name)?;
131-
for (column_index, column) in table.columns.iter().enumerate() {
132-
write!(file, " ")?;
133-
if column.unique {
134-
write!(file, "unique ")?;
135-
}
136-
write!(
137-
file,
138-
"{} ",
139-
match column.db_type {
140-
DbColumnType::Int => "int",
141-
DbColumnType::String => "string",
142-
}
143-
)?;
144-
write!(file, "{}: ", column.name)?;
145-
match &column.ql_type {
146-
QlColumnType::Int => write!(file, "int")?,
147-
QlColumnType::String => write!(file, "string")?,
148-
QlColumnType::Custom(name) => write!(file, "@{}", name)?,
149-
}
150-
if column.ql_type_is_ref {
151-
write!(file, " ref")?;
152-
}
153-
if column_index + 1 != table.columns.len() {
154-
write!(file, ",")?;
155-
}
156-
write!(file, "\n")?;
157-
}
158-
write!(file, ");\n\n")?;
159-
}
160-
Entry::Union { name, members } => {
161-
write!(file, "@{} = ", name)?;
162-
let mut first = true;
163-
for member in members {
164-
if first {
165-
first = false;
166-
} else {
167-
write!(file, " | ")?;
168-
}
169-
write!(file, "@{}", member)?;
170-
}
171-
write!(file, "\n\n")?;
172-
}
189+
Entry::Table(table) => write!(file, "{}\n\n", table)?,
190+
Entry::Union(union) => write!(file, "{}\n\n", union)?,
173191
}
174192
}
175193

generator/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ fn make_field_type(
5757
field_type.named,
5858
)));
5959
}
60-
entries.push(dbscheme::Entry::Union {
60+
entries.push(dbscheme::Entry::Union(dbscheme::Union {
6161
name: field_union_name.clone(),
6262
members,
63-
});
63+
}));
6464
field_union_name
6565
}
6666
}
@@ -144,10 +144,10 @@ fn convert_nodes(nodes: &[NodeInfo]) -> Vec<dbscheme::Entry> {
144144
subtype.named,
145145
)))
146146
}
147-
entries.push(dbscheme::Entry::Union {
147+
entries.push(dbscheme::Entry::Union(dbscheme::Union {
148148
name: dbscheme::escape_name(&node_type_name(&node.kind, node.named)),
149149
members,
150-
});
150+
}));
151151
} else {
152152
// It's a product type, defined by a table.
153153
let name = node_type_name(&node.kind, node.named);
@@ -207,10 +207,10 @@ fn convert_nodes(nodes: &[NodeInfo]) -> Vec<dbscheme::Entry> {
207207
}
208208

209209
// Create a union of all database types.
210-
entries.push(dbscheme::Entry::Union {
210+
entries.push(dbscheme::Entry::Union(dbscheme::Union {
211211
name: "top".to_string(),
212212
members: top_members,
213-
});
213+
}));
214214

215215
entries
216216
}

0 commit comments

Comments
 (0)