fluidattacks-blends 0.6.0

Blends imperative shell: parsing, AST-graph construction, serialization
Documentation
//! Source content → tree-sitter parse tree.

use tree_sitter::{Parser, Tree};

use crate::content::Content;
use crate::language::LanguageExt;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ParseError;

pub fn parse(content: &Content) -> Result<Tree, ParseError> {
    let mut parser = Parser::new();
    parser
        .set_language(&content.language.tree_sitter())
        .map_err(|_| ParseError)?;

    let tree = parser.parse(&content.bytes, None).ok_or(ParseError)?;
    if tree.root_node().has_error() {
        return Err(ParseError);
    }

    Ok(tree)
}

#[cfg(test)]
mod tests {
    use super::parse;
    use crate::content::Content;
    use std::fs;

    fn file_content(source: &[u8]) -> Content {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("snippet.java");
        fs::write(&path, source).unwrap();
        Content::from_path(&path, None).unwrap()
    }

    #[test]
    fn parses_valid_file() {
        assert!(parse(&file_content(b"class A {}")).is_ok());
    }

    #[test]
    fn rejects_malformed_file() {
        assert!(parse(&file_content(b"class A {")).is_err());
    }
}