sqruff_lib/
templaters.rs

1use std::sync::Arc;
2
3use sqruff_lib_core::errors::SQLFluffUserError;
4use sqruff_lib_core::templaters::TemplatedFile;
5
6use crate::Formatter;
7use crate::core::config::FluffConfig;
8use crate::templaters::placeholder::PlaceholderTemplater;
9use crate::templaters::raw::RawTemplater;
10
11#[cfg(feature = "python")]
12use crate::templaters::jinja::JinjaTemplater;
13#[cfg(feature = "python")]
14use crate::templaters::python::PythonTemplater;
15
16#[cfg(feature = "python")]
17pub mod dbt;
18#[cfg(feature = "python")]
19pub mod jinja;
20pub mod placeholder;
21#[cfg(feature = "python")]
22pub mod python;
23#[cfg(feature = "python")]
24pub mod python_shared;
25pub mod raw;
26
27pub static RAW_TEMPLATER: RawTemplater = RawTemplater;
28pub static PLACEHOLDER_TEMPLATER: PlaceholderTemplater = PlaceholderTemplater;
29#[cfg(feature = "python")]
30pub static PYTHON_TEMPLATER: PythonTemplater = PythonTemplater;
31#[cfg(feature = "python")]
32pub static JINJA_TEMPLATER: JinjaTemplater = JinjaTemplater;
33#[cfg(feature = "python")]
34pub static DBT_TEMPLATER: dbt::DBTTemplater = dbt::DBTTemplater;
35
36// templaters returns all the templaters that are available in the library
37#[cfg(feature = "python")]
38pub static TEMPLATERS: [&'static dyn Templater; 5] = [
39    &RAW_TEMPLATER,
40    &PLACEHOLDER_TEMPLATER,
41    &PYTHON_TEMPLATER,
42    &JINJA_TEMPLATER,
43    &DBT_TEMPLATER,
44];
45
46#[cfg(not(feature = "python"))]
47pub static TEMPLATERS: [&'static dyn Templater; 2] = [&RAW_TEMPLATER, &PLACEHOLDER_TEMPLATER];
48
49/// How a templater processes files.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum ProcessingMode {
52    /// Files can be processed individually and in parallel using Rayon.
53    /// Used by simple templaters like raw and placeholder.
54    Parallel,
55    /// Files must be processed sequentially, one at a time.
56    /// Used by templaters that have Python GIL restrictions.
57    Sequential,
58    /// Files benefit from batch processing with shared state.
59    /// The templater will receive all files at once and can optimize initialization.
60    /// Used by dbt to share manifest loading across files.
61    Batch,
62}
63
64pub trait Templater: Send + Sync {
65    /// The name of the templater.
66    fn name(&self) -> &'static str;
67
68    /// Description of the templater.
69    fn description(&self) -> &'static str;
70
71    /// Returns the processing mode for this templater.
72    fn processing_mode(&self) -> ProcessingMode;
73
74    /// Process one or more files and return TemplatedFiles.
75    ///
76    /// Arguments:
77    /// - files: Slice of (file_content, file_name) tuples
78    /// - config: The configuration to use
79    /// - formatter: Optional formatter for output
80    ///
81    /// Returns a vector of results in the same order as the input files.
82    fn process(
83        &self,
84        files: &[(&str, &str)],
85        config: &FluffConfig,
86        formatter: &Option<Arc<dyn Formatter>>,
87    ) -> Vec<Result<TemplatedFile, SQLFluffUserError>>;
88}