PoiScript / orgize

A Rust library for parsing org-mode files.
https://poiscript.github.io/orgize/
MIT License
277 stars 34 forks source link

Get properties for zeroth section #65

Closed andrewebdev closed 1 year ago

andrewebdev commented 1 year ago

Hi, firstly apologies for the newbie question. I'm new to rust and still learning.

I'm writing a tool that uses orgize. I'm attempting to extract only the properties from the Zeroth section of the org-mode file. My problem is that the iterator returns an indextree Arena and I don't know how to match it against "Element::Drawer" from the arena. Do I have to make some kind of type inference or typecast for this to work?

Here is my code currently:

    let doc_node = org.document().section_node().unwrap();
    for node_id in doc_node.children(org.arena()) {
        let node = org.arena().get(node_id).unwrap().into();
        println!("\nNode? {node:?}");
        match node {
            Element::Drawer(d) => {
                println!("We found the drawer: {d:?}");
            },
            _ => {
                println!("Something else: {node:?}");
            }
        }
    }

Of course the code above does not work because the node and the Element::Drawer isn't the same type. How could I match against specific element types when traversing a sub-section of the tree as I have above?

andrewebdev commented 1 year ago

Okay, turns out that was simpler than I thought in the end:

    let zeroth = org.document().section_node().unwrap();
    for node_id in zeroth.children(org.arena()) {
        let node = org.arena().get(node_id).unwrap();
        let data = node.get();
        match data {
            Element::Drawer(d) => {
                println!("\nData? {d:?}");
            },
            _ => {}
        }
    }