Skip to main content

dump/
dump.rs

1//! Print one profile's effective tree at some paths: `cargo run --example dump -- <pkg> <url> <path>...`
2use fhir_diff::{Node, Pool, load};
3
4fn find<'a>(node: &'a Node, path: &[&str]) -> Option<&'a Node> {
5    match path.split_first() {
6        None => Some(node),
7        Some((head, rest)) => {
8            let (name, slice) = head
9                .split_once(':')
10                .map_or((*head, None), |(n, s)| (n, Some(s)));
11            let child = node.children.get(name)?;
12            let child = match slice {
13                Some(s) => child.slices.get(s)?,
14                None => child,
15            };
16            find(child, rest)
17        }
18    }
19}
20
21fn main() {
22    let args: Vec<String> = std::env::args().skip(1).collect();
23    let loaded = load(&args[0]).expect("load");
24    let pool = Pool::new(&loaded);
25    let profile = pool
26        .profiles()
27        .into_iter()
28        .find(|p| p.url == args[1] || p.name == args[1])
29        .expect("profile");
30    for path in &args[2..] {
31        let parts: Vec<&str> = path.split('.').collect();
32        match find(&profile.tree, &parts) {
33            Some(n) => println!(
34                "{path}: min={} max={:?} types={:?} ms={} slices={:?} children={:?}",
35                n.min,
36                n.max,
37                n.types,
38                n.must_support,
39                n.slices.keys().collect::<Vec<_>>(),
40                n.children.keys().collect::<Vec<_>>()
41            ),
42            None => println!("{path}: (absent)"),
43        }
44    }
45}