1+ use std:: fmt;
2+
13/// Represents a distinct entry in the database schema.
24pub 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.
1826pub 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`.
105176pub 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
0 commit comments