basalt_cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use byte_unit::{ Byte, Unit }; use clap::{ Args, Parser, Subcommand }; use std::error::{ self }; use std::fmt::{ self }; use std::io::{ self }; use std::path::{ Path, PathBuf }; use std::process::{ ExitCode }; // ------------------------------------------------------------------------------------------------- // Constants // ------------------------------------------------------------------------------------------------- const ONE_GIBIBYTE: u64 = 1024u64.pow(3); const ONE_MEBIBYTE: u64 = 1024u64.pow(2); const ONE_KIBIBYTE: u64 = 1024u64.pow(1); // ------------------------------------------------------------------------------------------------- // Command line arguments // ------------------------------------------------------------------------------------------------- #[derive(Debug, Parser)] #[command(author, version, about, propagate_version=true)] struct Arguments { /// The database file to operate on. database: PathBuf, /// The subcommand. See `--help` for a list of possible subcommands. #[command(subcommand)] command: Command, } #[derive(Debug, Subcommand)] enum Command { /// Creates a new database file. CreateDatabase, /// Creates a new table in the database. CreateTable(CreateTableArguments), /// Inserts one or more rows into a table. Insert(InsertArguments), /// List the available kinds of column. ListKinds, /// Shows information about the whole database. ShowDatabase, /// Shows information about one table. ShowTable(ShowTableArguments), } #[derive(Args, Debug)] struct CreateTableArguments { /// The name of the table to create. table: String, /// The columns of the table, in the format `name:kind`. See `list-kinds` for a list of possible /// kinds. columns: Vec<basalt::Column>, } #[derive(Args, Debug)] struct InsertArguments { /// The name of the table to insert into. table: String, /// The names and order of fields to insert into the table. Defaults to the order of the columns /// specified when the table was created. All columns must be present. #[arg(short, long)] format: Option<basalt::RowFormat>, /// The rows to insert into the table. rows: Vec<basalt::Row>, } #[derive(Args, Debug)] struct ShowTableArguments { /// The name of the table to show. table: String, } impl Command { fn run(&self, path: &Path) -> Result<()> { match *self { Command::CreateDatabase => { let db = basalt::Database::create_new(path)?; db.synchronize()?; Ok(()) }, Command::CreateTable(ref arguments) => { let mut db = basalt::Database::open(path)?; db.create_table(&arguments.table, &arguments.columns)?; db.synchronize()?; Ok(()) }, Command::Insert(ref arguments) => { println!("{:?}", arguments); let mut db = basalt::Database::open(path)?; let table = db.find_table_mut(&arguments.table)?; let format = arguments.format.clone().unwrap_or_else(|| table.default_format()); table.insert(&format, &arguments.rows)?; db.synchronize()?; Ok(()) }, Command::ListKinds => { println!("Available column kinds:"); println!(); println!(" - bool: a true or false value"); println!(" - `s8`, `s16`, `s32`, `s64`, `s128`: a signed integer with the specified bit width"); println!(" - `u8`, `u16`, `u32`, `u64`, `u128`: an unsigned integer with the specified bit width"); println!(" - `f32`, `f64`: an IEEE 754 single- or double-precision floating point number"); println!(" - `utf8`: a UTF-8 encoded string"); println!(" - `uuid`: a UUID (version 4, format 1)"); println!(" - `time`: an unsigned 64-bit timestamp with optional timezone (1ms resolution)"); println!(" - `tick`: a signed 64-bit interval (1ms resolution)"); println!(); println!("You can also put `[]` around any scalar kind to make it an array kind, e.g. `[bool]`, `[utf8]`, `[uuid]`"); Ok(()) }, Command::ShowDatabase => { let db = basalt::Database::open(path)?; println!("Database: {}", db.path().display()); let disk_size = db.disk_size()?; let nice_disk_size = if disk_size >= ONE_GIBIBYTE { Byte::from_u64(disk_size).get_adjusted_unit(Unit::GiB) } else if disk_size >= ONE_MEBIBYTE { Byte::from_u64(disk_size).get_adjusted_unit(Unit::MiB) } else if disk_size >= ONE_KIBIBYTE { Byte::from_u64(disk_size).get_adjusted_unit(Unit::KiB) } else { Byte::from_u64(disk_size).get_adjusted_unit(Unit::B) }; println!(" - Size on disk: {}", nice_disk_size); for table in db.iter_tables() { println!(" - Table: {}", table.name()); println!(" - Default row format: {}", table.default_format()); for column in table.iter_columns() { println!(" - Column: {}:{}", column.name(), column.kind()); } } Ok(()) }, Command::ShowTable(ref arguments) => { let db = basalt::Database::open(path)?; let table = db.find_table(&arguments.table)?; println!("Table: {}", table.name()); println!(" - Default row format: {}", table.default_format()); for column in table.iter_columns() { println!(" - Column: {}:{}", column.name(), column.kind()); } Ok(()) }, } } } // ------------------------------------------------------------------------------------------------- // Error handling // ------------------------------------------------------------------------------------------------- type Result<T> = ::std::result::Result<T, Error>; #[derive(Debug)] enum Error { FromBasalt(basalt::Error), FromStdIo(io::Error), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use Error::*; match *self { FromBasalt(ref e) => write!(f, "Basalt error: {}", e), FromStdIo(ref e) => write!(f, "IO error: {}", e), } } } impl From<io::Error> for Error { fn from(e: io::Error) -> Error { Error::FromStdIo(e) } } impl From<basalt::Error> for Error { fn from(e: basalt::Error) -> Error { Error::FromBasalt(e) } } impl error::Error for Error {} // ------------------------------------------------------------------------------------------------- // Entry point // ------------------------------------------------------------------------------------------------- fn main() -> ExitCode { let arguments = Arguments::parse(); match arguments.command.run(&arguments.database) { Ok(_) => ExitCode::SUCCESS, Err(e) => { eprintln!("{}", e); ExitCode::FAILURE } } }