Module fastobo::visit

source ·
Expand description

Visitor traits for the OBO syntax tree.

Visit can be used to implement an algorithm that can traverse an OBO AST without editing it, e.g. for validation purposes. This reduces the boilerplate needed for functions that only require to work on specific parts of an OBO document.

Example

The following visitor will collect all synonym types of an OBO document and can be used to return a set of references to the undeclared ones:

extern crate fastobo;

use std::collections::HashSet;
use fastobo::ast::*;
use fastobo::visit::Visit;

#[derive(Default)]
struct SynTypeChecker<'ast> {
    declared: HashSet<&'ast SynonymTypeIdent>,
    used: HashSet<&'ast SynonymTypeIdent>,
}

impl<'ast> Visit<'ast> for SynTypeChecker<'ast> {
    fn visit_header_clause(&mut self, clause: &'ast HeaderClause) {
        if let HeaderClause::SynonymTypedef(ty, _, _) = clause {
            self.declared.insert(ty);
        }
    }

    fn visit_synonymtype_ident(&mut self, id: &'ast SynonymTypeIdent) {
        self.used.insert(id);
    }
}

pub fn undeclared_synonym_types(doc: &OboDoc) -> HashSet<&SynonymTypeIdent> {
    let mut checker = SynTypeChecker::default();
    checker.visit_doc(doc);
    checker.used.difference(&checker.declared).cloned().collect()
}

let doc = fastobo::from_file("tests/data/ms.obo").unwrap();
assert!(undeclared_synonym_types(&doc).is_empty());

See also

Modules

  • Default implementation of Visit trait methods.
  • Default implementation of VisitMut trait methods.

Structs

  • A visitor that will compact identifiers in an OBO document.
  • A visitor that will decompact identifiers in an OBO document.

Traits

  • Syntax tree traversal to walk a shared borrow of an OBO syntax tree.
  • Syntax tree traversal to walk a mutable borrow of an OBO syntax tree.