Skip to main content

reovim_driver_syntax/
factory.rs

1//! Syntax driver factory trait.
2//!
3//! This module defines the [`SyntaxDriverFactory`] trait for creating
4//! syntax drivers for specific languages.
5
6use crate::driver::SyntaxDriver;
7
8/// Factory for creating syntax drivers.
9///
10/// Implementations know how to create drivers for specific languages.
11/// The factory is registered with the runtime and called when files are opened.
12///
13/// # Thread Safety
14///
15/// Implementations must be `Send + Sync` to allow use across threads.
16///
17/// # Example
18///
19/// ```ignore
20/// // Factory creates drivers for supported languages
21/// let factory: Box<dyn SyntaxDriverFactory> = get_factory();
22///
23/// // Check if language is supported
24/// if factory.supports("rust") {
25///     // Create driver for Rust
26///     let driver = factory.create("rust").unwrap();
27///     assert_eq!(driver.language(), "rust");
28/// }
29///
30/// // List all supported languages
31/// for lang in factory.supported_languages() {
32///     println!("Supports: {}", lang);
33/// }
34/// ```
35pub trait SyntaxDriverFactory: Send + Sync {
36    /// Create a syntax driver for a language.
37    ///
38    /// # Arguments
39    ///
40    /// * `language_id` - The language identifier (e.g., "rust", "python")
41    ///
42    /// # Returns
43    ///
44    /// `Some(driver)` if the language is supported, `None` otherwise.
45    fn create(&self, language_id: &str) -> Option<Box<dyn SyntaxDriver>>;
46
47    /// List all supported language IDs.
48    ///
49    /// Returns a list of language identifiers that can be passed to
50    /// [`create()`](Self::create).
51    fn supported_languages(&self) -> Vec<&str>;
52
53    /// Check if a language is supported.
54    ///
55    /// # Default
56    ///
57    /// Checks if the language ID is in [`supported_languages()`](Self::supported_languages).
58    fn supports(&self, language_id: &str) -> bool {
59        self.supported_languages().contains(&language_id)
60    }
61}
62
63#[cfg(test)]
64#[path = "factory_tests.rs"]
65mod tests;