use anyhow::Result;
use tree_sitter::Language;

use crate::types;
use super::LanguageParser;

/// Java parser using tree-sitter
pub struct JavaParser {
    language: Language,
}

impl JavaParser {
    pub fn new() -> Self {
        Self {
            language: tree_sitter_java::language(),
        }
    }
}

impl LanguageParser for JavaParser {
    fn language(&self) -> types::Language {
        types::Language::Java
    }

    fn parse(&self, source: &str) -> Result<tree_sitter::Tree> {
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(self.language)?;
        parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse Java"))
    }

    fn get_tree_sitter_language(&self) -> Language {
        self.language
    }
}
