#![cfg_attr(coverage_nightly, coverage(off))]
#[cfg(feature = "polyglot-typescript")]
use crate::ast::polyglot::language_mapper::TypeScriptMapper;
#[cfg(feature = "csharp-ast")]
use crate::ast::polyglot::language_mapper::CSharpMapper;
#[cfg(feature = "java-ast")]
use crate::ast::polyglot::language_mapper::JavaMapper;
#[cfg(feature = "javascript-ast")]
use crate::ast::polyglot::language_mapper::JavaScriptMapper;
#[cfg(feature = "kotlin-ast")]
use crate::ast::polyglot::language_mapper::KotlinMapper;
#[cfg(feature = "ruby-ast")]
use crate::ast::polyglot::language_mapper::RubyMapper;
#[cfg(feature = "scala-ast")]
use crate::ast::polyglot::language_mapper::ScalaMapper;
use crate::ast::polyglot::{Language, LanguageMapper, PolyglotPathValidator, UnifiedNode};
use crate::services::context::AstItem;
use anyhow::Result;
use async_trait::async_trait;
use std::path::Path;
use std::sync::Arc;
pub struct LanguageMapperFactory;
impl LanguageMapperFactory {
pub fn create(language: Language) -> Result<Arc<dyn LanguageMapper>> {
match language {
#[cfg(feature = "polyglot-java")]
Language::Java => Ok(Arc::new(JavaMapper::new())),
#[cfg(feature = "polyglot-kotlin")]
Language::Kotlin => Ok(Arc::new(KotlinMapper::new())),
#[cfg(feature = "polyglot-scala")]
Language::Scala => Ok(Arc::new(ScalaMapper::new())),
#[cfg(feature = "polyglot-typescript")]
Language::TypeScript => Ok(Arc::new(TypeScriptMapper::new())),
#[cfg(feature = "polyglot-javascript")]
Language::JavaScript => Ok(Arc::new(JavaScriptMapper::new())),
#[cfg(feature = "polyglot-csharp")]
Language::CSharp => Ok(Arc::new(CSharpMapper::new())),
#[cfg(feature = "polyglot-ruby")]
Language::Ruby => Ok(Arc::new(RubyMapper::new())),
_ => {
Ok(Arc::new(StubMapper::new(language)))
}
}
}
}
#[derive(Clone)]
pub struct StubMapper {
language: Language,
}
impl StubMapper {
pub fn new(language: Language) -> Self {
Self { language }
}
}
#[async_trait]
impl LanguageMapper for StubMapper {
fn language(&self) -> Language {
self.language
}
async fn map_file(&self, path: &Path) -> Result<Vec<UnifiedNode>> {
PolyglotPathValidator::validate_file_path(path)?;
Ok(Vec::new())
}
async fn map_directory(&self, path: &Path, _recursive: bool) -> Result<Vec<UnifiedNode>> {
PolyglotPathValidator::validate_directory_path(path)?;
Ok(Vec::new())
}
async fn map_source(&self, _source: &str, _path: &Path) -> Result<Vec<UnifiedNode>> {
Ok(Vec::new())
}
fn convert_ast_items(&self, _items: &[AstItem], _path: &Path) -> Vec<UnifiedNode> {
Vec::new()
}
fn clone_box(&self) -> Box<dyn LanguageMapper> {
Box::new(self.clone())
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
use super::*;
use std::fs;
use tempfile::TempDir;
include!("language_mapper_factory_tests.rs");
include!("language_mapper_factory_tests_extended.rs");
}