/ radicle-cli / src / commands / node / routing.rs
routing.rs
 1  use radicle::node;
 2  use radicle::prelude::{Id, NodeId};
 3  
 4  use crate::terminal as term;
 5  use crate::terminal::Element;
 6  
 7  pub fn run<S: node::routing::Store>(
 8      routing: &S,
 9      rid: Option<Id>,
10      nid: Option<NodeId>,
11      json: bool,
12  ) -> anyhow::Result<()> {
13      // Filters entries by RID or NID exclusively, or show all of them if none given.
14      let entries = routing.entries()?.filter(|(rid_, nid_)| {
15          (nid.is_none() || Some(nid_) == nid.as_ref())
16              && (rid.is_none() || Some(rid_) == rid.as_ref())
17      });
18  
19      if json {
20          print_json(entries);
21      } else {
22          print_table(entries);
23      }
24  
25      Ok(())
26  }
27  
28  fn print_table(entries: impl IntoIterator<Item = (Id, NodeId)>) {
29      let mut t = term::Table::new(term::table::TableOptions::bordered());
30      t.push([
31          term::format::default(String::from("RID")),
32          term::format::default(String::from("NID")),
33      ]);
34      t.divider();
35  
36      for (rid, nid) in entries {
37          t.push([
38              term::format::highlight(rid.to_string()),
39              term::format::node(&nid),
40          ]);
41      }
42      t.print();
43  }
44  
45  fn print_json(entries: impl IntoIterator<Item = (Id, NodeId)>) {
46      for (rid, nid) in entries {
47          println!("{}", serde_json::json!({ "rid": rid, "nid": nid }));
48      }
49  }