|
1 | | -@@ -1,39 +1,35 @@ |
| 1 | +@@ -1,26 +1,23 @@ |
| 2 | + import argv |
| 3 | + import file_streams/read_stream |
| 4 | + import gleam/bit_array |
| 5 | + import gleam/int.{to_string} |
2 | 6 | import gleam/io |
3 | | - import gleam/string |
4 | | - import gleam/int |
5 | | - import gleam/list |
6 | | - import gleam/result |
| 7 | + import gleam/list.{each} |
7 | 8 |
|
8 | | --// You can use print statements as follows for debugging, they'll be visible when running tests. |
9 | | --io.println("Logs from your program will appear here!") |
10 | | -+pub fn main() { |
11 | | -+ let args = io.argv() |
12 | | - |
13 | | --// Uncomment this block to pass the first stage |
14 | | --// pub fn main() { |
15 | | --// let args = io.argv() |
16 | | --// |
17 | | --// case args { |
18 | | --// [_, database_file_path, command] -> { |
19 | | --// case command { |
20 | | --// ".dbinfo" -> { |
21 | | --// io.open(database_file_path, io.Read) |
22 | | --// |> result.then(fn(file) { |
23 | | --// io.seek(file, 16) |
24 | | --// |> result.then(fn(_) { |
25 | | --// io.read_bytes(file, 2) |
26 | | --// |> result.then(fn(bytes) { |
27 | | --// let page_size = bytes |
28 | | --// |> list.reverse |
29 | | --// |> int.from_bytes_be |
30 | | --// io.println(string.concat(["database page size: ", int.to_string(page_size)])) |
31 | | --// |> result.map(fn(_) { io.close(file) }) |
32 | | --// }) |
33 | | --// }) |
34 | | --// }) |
35 | | --// |> result.unwrap(crash) |
36 | | --// } |
37 | | --// _ -> io.println(string.concat(["Invalid command: ", command])) |
38 | | --// } |
39 | | --// } |
40 | | --// _ -> io.println("Usage: gleam run database_file_path command") |
41 | | --// } |
42 | | --// } |
43 | | -\ No newline at end of file |
| 9 | + pub fn main() { |
| 10 | +- // You can use print statements as follows for debugging, they'll be visible when running tests. |
| 11 | +- io.println("Logs from your program will appear here!") |
| 12 | + let args = argv.load().arguments |
| 13 | +- // Uncomment this to pass the first stage |
| 14 | +- // case args { |
| 15 | +- // [database_file_path, ".dbinfo", ..] -> { |
| 16 | +- // let assert Ok(rs) = read_stream.open(database_file_path) |
| 17 | +- // let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16) |
| 18 | +- // let assert Ok(int_value) = read_stream.read_int16_be(rs) |
| 19 | +- // |
| 20 | +- // io.print("database page size: ") |
| 21 | +- // io.println(to_string(int_value)) |
| 22 | +- // } |
| 23 | +- // _ -> { |
| 24 | +- // io.println("No match") |
| 25 | +- // } |
| 26 | +- // } |
44 | 27 | + case args { |
45 | | -+ [_, database_file_path, command] -> { |
46 | | -+ case command { |
47 | | -+ ".dbinfo" -> { |
48 | | -+ io.open(database_file_path, io.Read) |
49 | | -+ |> result.then(fn(file) { |
50 | | -+ io.seek(file, 16) |
51 | | -+ |> result.then(fn(_) { |
52 | | -+ io.read_bytes(file, 2) |
53 | | -+ |> result.then(fn(bytes) { |
54 | | -+ let page_size = bytes |
55 | | -+ |> list.reverse |
56 | | -+ |> int.from_bytes_be |
57 | | -+ io.println(string.concat(["database page size: ", int.to_string(page_size)])) |
58 | | -+ |> result.map(fn(_) { io.close(file) }) |
59 | | -+ }) |
60 | | -+ }) |
61 | | -+ }) |
62 | | -+ |> result.unwrap(crash) |
63 | | -+ } |
64 | | -+ _ -> io.println(string.concat(["Invalid command: ", command])) |
65 | | -+ } |
| 28 | ++ [database_file_path, ".dbinfo", ..] -> { |
| 29 | ++ let assert Ok(rs) = read_stream.open(database_file_path) |
| 30 | ++ let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16) |
| 31 | ++ let assert Ok(int_value) = read_stream.read_int16_be(rs) |
| 32 | ++ |
| 33 | ++ io.print("database page size: ") |
| 34 | ++ io.println(to_string(int_value)) |
| 35 | ++ } |
| 36 | ++ _ -> { |
| 37 | ++ io.println("No match") |
66 | 38 | + } |
67 | | -+ _ -> io.println("Usage: gleam run database_file_path command") |
68 | 39 | + } |
69 | | -+} |
70 | | -\ No newline at end of file |
| 40 | + } |
0 commit comments