Skip to main content

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