minigraf 1.1.1

Zero-config, single-file, embedded graph database with bi-temporal Datalog queries
Documentation
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use crate::db::Minigraf;
use std::io::{self, BufRead, IsTerminal, Write};

/// An interactive REPL for a [`Minigraf`] database.
///
/// Construct via [`Minigraf::repl`] and call [`Repl::run`] to start the session.
pub struct Repl<'a> {
    db: &'a Minigraf,
}

impl<'a> Repl<'a> {
    pub(crate) fn new(db: &'a Minigraf) -> Self {
        Repl { db }
    }

    /// Start the interactive REPL loop.
    ///
    /// Reads Datalog commands from stdin line-by-line. When stdin is a TTY,
    /// a banner and prompts are printed; when piped, output is suppressed so
    /// the REPL can be driven by scripts.
    pub fn run(&self) {
        let interactive = io::stdin().is_terminal();
        self.run_impl(io::BufReader::new(io::stdin()), interactive);
    }

    /// Source `init_path` silently, without starting the interactive loop.
    ///
    /// Commands in the init file are executed exactly as if they had been
    /// typed at the prompt, but without any banner or prompts. Errors are
    /// printed to stderr and execution of the init file continues. Call
    /// [`Repl::run`] afterwards to start the interactive REPL.
    pub fn run_with_init(&self, init_path: &std::path::Path) {
        match std::fs::File::open(init_path) {
            Ok(file) => {
                self.run_impl(io::BufReader::new(file), false);
            }
            Err(e) => {
                let path = init_path.display();
                eprintln!("error: could not open init file '{path}': {e}");
            }
        }
    }

    fn run_impl<R: BufRead>(&self, mut reader: R, interactive: bool) {
        if interactive {
            println!(
                "Minigraf v{} - Interactive Datalog Console",
                env!("CARGO_PKG_VERSION")
            );
            println!();
            println!("Data operations:");
            println!("  (transact [...])                    - assert facts");
            println!("  (transact {{:valid-from ... :valid-to ...}} [...]) - with valid time");
            println!("  (retract [...])                     - retract facts");
            println!();
            println!("Queries:");
            println!("  (query [:find ?x :where ...])       - basic query");
            println!("  (rule [(name ?a ?b) [?a :attr ?b]]) - define a rule");
            println!();
            println!("Temporal queries:");
            println!(
                "  (query [:find ?x :as-of 50 :where ...])                     - state as of tx counter 50"
            );
            println!(
                "  (query [:find ?x :as-of \"2024-01-15T10:00:00Z\" :where ...]) - state as of UTC timestamp"
            );
            println!(
                "  (query [:find ?x :valid-at \"2023-06-01\" :where ...])        - facts valid on date"
            );
            println!(
                "  (query [:find ?x :valid-at :any-valid-time :where ...])     - all facts, ignoring validity"
            );
            println!();
            println!("Note: queries without :valid-at return only currently valid facts.");
            println!();
            println!("Type EXIT to quit.\n");
        }

        let mut command_buffer = String::new();
        let mut is_multiline = false;
        // Only print the prompt once per command, not once per blank/comment line.
        let mut prompt_needed = true;

        loop {
            if interactive && !is_multiline && prompt_needed {
                print!("minigraf> ");
                io::stdout().flush().ok();
                prompt_needed = false;
            }

            let mut input = String::new();
            match reader.read_line(&mut input) {
                Ok(n) => {
                    if n == 0 {
                        // EOF (Ctrl-D): emit a newline so the shell prompt starts
                        // on a fresh line rather than appending to the REPL prompt.
                        if interactive {
                            println!();
                        }
                        break;
                    }

                    let line = input.trim();

                    if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
                        continue;
                    }

                    if line.to_uppercase() == "EXIT" {
                        break;
                    }

                    if !command_buffer.is_empty() {
                        command_buffer.push('\n');
                    }
                    command_buffer.push_str(line);

                    if Self::is_command_complete(&command_buffer) {
                        match self.db.execute(&command_buffer) {
                            Ok(result) => {
                                Self::print_result(result);
                                io::stdout().flush().ok();
                            }
                            Err(e) => {
                                eprintln!("Error: {}", e);
                            }
                        }

                        command_buffer.clear();
                        is_multiline = false;
                        prompt_needed = true;
                        if interactive {
                            println!();
                            io::stdout().flush().ok();
                        }
                    } else {
                        is_multiline = true;
                    }
                }
                Err(e) => {
                    eprintln!("Error reading input: {}", e);
                    break;
                }
            }
        }
    }

    fn is_command_complete(input: &str) -> bool {
        let mut depth = 0;
        let mut in_string = false;
        let mut escape_next = false;

        for ch in input.chars() {
            if escape_next {
                escape_next = false;
                continue;
            }

            match ch {
                '\\' if in_string => {
                    escape_next = true;
                }
                '"' => {
                    in_string = !in_string;
                }
                '(' if !in_string => {
                    #[allow(clippy::arithmetic_side_effects)]
                    {
                        depth += 1;
                    }
                }
                ')' if !in_string => {
                    #[allow(clippy::arithmetic_side_effects)]
                    {
                        depth -= 1;
                    }
                }
                _ => {}
            }
        }

        depth == 0 && input.contains('(')
    }

    fn print_result(result: crate::query::datalog::QueryResult) {
        use crate::query::datalog::QueryResult as DResult;

        match result {
            DResult::Transacted(tx_id) => {
                println!("✓ Transacted successfully (tx: {tx_id})");
            }
            DResult::Retracted(tx_id) => {
                println!("✓ Retracted successfully (tx: {tx_id})");
            }
            DResult::QueryResults { vars, results } => {
                if results.is_empty() {
                    println!("No results found.");
                } else {
                    println!("{}", vars.join("\t"));
                    println!("{}", "-".repeat(vars.len().saturating_mul(20)));

                    for row in &results {
                        let formatted_row: Vec<String> =
                            row.iter().map(Self::format_value).collect();
                        println!("{}", formatted_row.join("\t"));
                    }

                    println!("\n{} result(s) found.", results.len());
                }
            }
            DResult::Ok => {
                println!("✓ OK");
            }
        }
    }

    fn format_value(value: &crate::graph::types::Value) -> String {
        use crate::graph::types::Value;

        match value {
            Value::String(s) => format!("\"{}\"", s),
            Value::Integer(i) => i.to_string(),
            Value::Float(f) => f.to_string(),
            Value::Boolean(b) => b.to_string(),
            Value::Ref(uuid) => format!("#uuid {}", uuid),
            Value::Keyword(k) => k.clone(),
            Value::Null => "nil".to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Minigraf;

    #[test]
    fn run_public_method_exits_on_non_tty_stdin() {
        // In TTY environments (local dev) stdin.is_terminal() is true and run() would
        // block waiting for input — skip the test gracefully.  In CI the test binary's
        // stdin is a closed pipe, so read_line() returns Ok(0) immediately and run()
        // returns after one loop iteration.  This exercises the two otherwise-uncovered
        // lines in the public `run()` wrapper (is_terminal + run_impl call).
        if io::stdin().is_terminal() {
            return;
        }
        let db = Minigraf::in_memory().expect("in-memory db");
        db.repl().run();
    }

    #[test]
    fn eof_in_interactive_mode_exits_cleanly() {
        // Exercises the `if interactive { println!(); }` branch in the Ok(0) arm.
        // An empty Cursor reaches EOF immediately; interactive=true triggers the newline path.
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(std::io::Cursor::new(b""), true);
    }

    #[test]
    fn eof_in_non_interactive_mode_exits_cleanly() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(std::io::Cursor::new(b""), false);
    }

    #[test]
    fn exit_command_terminates_loop() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(std::io::Cursor::new(b"EXIT\n"), false);
    }

    #[test]
    fn comment_and_blank_lines_are_skipped() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(b"# hash comment\n; edn comment\n\nEXIT\n"),
            false,
        );
    }

    #[test]
    fn query_with_no_results_runs_without_panic() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(b"(query [:find ?e :where [?e :x 1]])\nEXIT\n"),
            false,
        );
    }

    #[test]
    fn transact_and_query_with_results() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/name \"Widget\"]])\n\
                  (query [:find ?n :where [_ :item/name ?n]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn retract_command_runs_without_panic() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/name \"Widget\"]])\n\
                  (retract [[:db/retract \"e1\" :item/name \"Widget\"]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn multiline_command_is_buffered_until_complete() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        // The first line is an incomplete command (unmatched paren); the second
        // line completes it — exercises the `is_multiline = true` branch.
        repl.run_impl(
            std::io::Cursor::new(b"(query [:find ?e\n:where [?e :x 1]])\nEXIT\n"),
            false,
        );
    }

    #[test]
    fn interactive_mode_prints_output_after_command() {
        // interactive=true exercises the `println!()` after a successful command.
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(b"(query [:find ?e :where [?e :x 1]])\nEXIT\n"),
            true,
        );
    }

    #[test]
    fn read_error_exits_loop() {
        struct ErrorReader;
        impl std::io::Read for ErrorReader {
            fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
                Err(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "simulated read error",
                ))
            }
        }
        impl std::io::BufRead for ErrorReader {
            fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
                Err(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "simulated read error",
                ))
            }
            fn consume(&mut self, _amt: usize) {}
        }

        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(ErrorReader, false);
    }

    #[test]
    fn query_integer_result_covers_format_value_integer() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/count 42]])\n\
                  (query [:find ?c :where [_ :item/count ?c]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn query_float_result_covers_format_value_float() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/price 9.99]])\n\
                  (query [:find ?p :where [_ :item/price ?p]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn query_keyword_result_covers_format_value_keyword() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/status :active]])\n\
                  (query [:find ?s :where [_ :item/status ?s]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn query_boolean_result_covers_format_value_boolean() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(transact [[:db/add \"e1\" :item/in-stock true]])\n\
                  (query [:find ?s :where [_ :item/in-stock ?s]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn rule_definition_covers_result_ok_arm() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_impl(
            std::io::Cursor::new(
                b"(rule [(parent ?x ?y) [?x :parent/of ?y]])\n\
                  EXIT\n",
            ),
            false,
        );
    }

    #[test]
    fn multiline_command_in_interactive_mode_covers_continuation_prompt() {
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        // First line is incomplete (unmatched paren) → no continuation prompt (suppressed in multiline mode).
        repl.run_impl(
            std::io::Cursor::new(b"(query [:find ?e\n:where [?e :x 1]])\nEXIT\n"),
            true,
        );
    }

    // --- Direct tests for print_result ---

    #[test]
    fn print_result_transacted() {
        use crate::query::datalog::QueryResult as DResult;
        Repl::print_result(DResult::Transacted(12345678));
    }

    #[test]
    fn print_result_retracted() {
        use crate::query::datalog::QueryResult as DResult;
        Repl::print_result(DResult::Retracted(12345678));
    }

    #[test]
    fn print_result_ok() {
        use crate::query::datalog::QueryResult as DResult;
        Repl::print_result(DResult::Ok);
    }

    #[test]
    fn print_result_query_no_results() {
        use crate::query::datalog::QueryResult as DResult;
        Repl::print_result(DResult::QueryResults {
            vars: vec!["?x".to_string()],
            results: vec![],
        });
    }

    #[test]
    fn print_result_query_with_rows() {
        use crate::graph::types::Value;
        use crate::query::datalog::QueryResult as DResult;
        Repl::print_result(DResult::QueryResults {
            vars: vec!["?x".to_string(), "?y".to_string()],
            results: vec![vec![Value::String("hello".to_string()), Value::Integer(42)]],
        });
    }

    // --- Direct tests for format_value ---

    #[test]
    fn format_value_string() {
        use crate::graph::types::Value;
        assert_eq!(
            Repl::format_value(&Value::String("hi".to_string())),
            "\"hi\""
        );
    }

    #[test]
    fn format_value_integer() {
        use crate::graph::types::Value;
        assert_eq!(Repl::format_value(&Value::Integer(7)), "7");
    }

    #[test]
    fn format_value_float() {
        use crate::graph::types::Value;
        assert_eq!(Repl::format_value(&Value::Float(3.14)), "3.14");
    }

    #[test]
    fn format_value_boolean() {
        use crate::graph::types::Value;
        assert_eq!(Repl::format_value(&Value::Boolean(true)), "true");
    }

    #[test]
    fn format_value_ref() {
        use crate::graph::types::Value;
        let id = uuid::Uuid::new_v4();
        assert_eq!(Repl::format_value(&Value::Ref(id)), format!("#uuid {}", id));
    }

    #[test]
    fn format_value_keyword() {
        use crate::graph::types::Value;
        assert_eq!(
            Repl::format_value(&Value::Keyword(":active".to_string())),
            ":active"
        );
    }

    #[test]
    fn format_value_null() {
        use crate::graph::types::Value;
        assert_eq!(Repl::format_value(&Value::Null), "nil");
    }

    // --- Direct tests for is_command_complete ---

    #[test]
    fn is_command_complete_handles_escaped_quote_in_string() {
        // Exercises the `escape_next` branches (lines 135-136, 140-141).
        assert!(Repl::is_command_complete(
            r#"(query [:find ?x :where [?x :name "he said \"hi\""]])"#
        ));
    }

    #[test]
    fn is_command_complete_default_arm() {
        // A plain character inside a string hits the `_ => {}` arm.
        assert!(Repl::is_command_complete(r#"(foo "bar")"#));
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn run_with_init_sources_file() {
        // run_with_init only processes the init file (no interactive loop), so
        // this test is safe to run in any environment without blocking on stdin.
        use std::io::Write;
        let mut tmp = tempfile::NamedTempFile::new().expect("tempfile");
        writeln!(
            tmp,
            "(transact [[:alice :person/name \"Alice\"]])\n\
             (rule [(has-name ?e ?n) [?e :person/name ?n]])"
        )
        .expect("write init");
        tmp.flush().expect("flush");

        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_with_init(tmp.path());
        let result = db
            .execute("(query [:find ?n :where (has-name _ ?n)])")
            .expect("query");
        match result {
            crate::query::datalog::QueryResult::QueryResults { results, .. } => {
                assert_eq!(results.len(), 1);
            }
            _ => panic!("expected query results"),
        }
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn run_with_init_missing_file_prints_error() {
        // A non-existent init path should print an error to stderr but not panic.
        // run_with_init no longer starts the interactive loop, so no stdin blocking.
        let db = Minigraf::in_memory().expect("in-memory db");
        let repl = db.repl();
        repl.run_with_init(std::path::Path::new("/nonexistent/path/rules.datalog"));
    }
}