Skip to main content

dlin_core/render/
plain.rs

1use std::io::{self, Write};
2
3use crate::graph::types::*;
4
5/// Render the lineage graph as plain text (one label per line) to stdout.
6/// Output order follows graph insertion order (not alphabetical or topological).
7pub fn render_plain(graph: &LineageGraph) {
8    super::handle_stdout_result(render_plain_to_writer(graph, &mut std::io::stdout().lock()));
9}
10
11pub fn render_plain_to_writer<W: Write>(graph: &LineageGraph, w: &mut W) -> io::Result<()> {
12    for idx in graph.node_indices() {
13        let node = &graph[idx];
14        writeln!(w, "{}", node.label)?;
15    }
16    Ok(())
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use crate::render::test_helpers::make_node;
23
24    fn render_to_string(graph: &LineageGraph) -> String {
25        let mut buf = Vec::new();
26        render_plain_to_writer(graph, &mut buf).unwrap();
27        String::from_utf8(buf).unwrap()
28    }
29
30    #[test]
31    fn test_empty_graph() {
32        let graph = LineageGraph::new();
33        let output = render_to_string(&graph);
34        assert!(output.is_empty());
35    }
36
37    #[test]
38    fn test_single_node() {
39        let mut graph = LineageGraph::new();
40        graph.add_node(make_node("model.orders", "orders", NodeType::Model));
41        let output = render_to_string(&graph);
42        assert_eq!(output, "orders\n");
43    }
44
45    #[test]
46    fn test_multiple_nodes() {
47        let mut graph = LineageGraph::new();
48        graph.add_node(make_node("model.a", "a", NodeType::Model));
49        graph.add_node(make_node("model.b", "b", NodeType::Model));
50        graph.add_node(make_node("source.raw.c", "raw.c", NodeType::Source));
51        let output = render_to_string(&graph);
52        assert_eq!(output, "a\nb\nraw.c\n");
53    }
54
55    #[test]
56    fn test_snapshot_plain() {
57        let graph = crate::render::test_helpers::make_sample_lineage_graph();
58        let output = render_to_string(&graph);
59        insta::assert_snapshot!(output);
60    }
61}