Skip to content

Commit

Permalink
chore: remove unnecessary arc
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Nov 28, 2024
1 parent 84aebf7 commit e510072
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
9 changes: 4 additions & 5 deletions crates/cli/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use minijinja::{context, Environment};
use serde::Serialize;
use sqruff_lib::core::rules::base::ErasedRule;
use sqruff_lib::rules::rules;
use sqruff_lib::templaters::templaters;
use sqruff_lib::templaters::TEMPLATERS;

use crate::commands::Cli;

Expand Down Expand Up @@ -47,8 +47,7 @@ pub(crate) fn codegen_docs() {
env.add_template("templaters", &template).unwrap();

let tmpl = env.get_template("templaters").unwrap();
let templater = templaters();
let templaters = templater
let templaters = TEMPLATERS
.into_iter()
.map(Templater::from)
.collect::<Vec<_>>();
Expand All @@ -69,8 +68,8 @@ struct Templater {
description: &'static str,
}

impl From<Box<dyn sqruff_lib::templaters::Templater>> for Templater {
fn from(value: Box<dyn sqruff_lib::templaters::Templater>) -> Self {
impl From<&'static dyn sqruff_lib::templaters::Templater> for Templater {
fn from(value: &'static dyn sqruff_lib::templaters::Templater) -> Self {
Templater {
name: value.name(),
description: value.description(),
Expand Down
27 changes: 12 additions & 15 deletions crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use std::sync::OnceLock;

use ahash::{AHashMap, AHashSet};
use itertools::Itertools;
Expand Down Expand Up @@ -31,34 +31,31 @@ use crate::core::rules::base::{ErasedRule, LintPhase, RulePack};
use crate::core::rules::noqa::IgnoreMask;
use crate::rules::get_ruleset;
use crate::templaters::raw::RawTemplater;
use crate::templaters::{templaters, Templater};
use crate::templaters::{Templater, TEMPLATERS};

pub struct Linter {
config: FluffConfig,
formatter: Option<OutputStreamFormatter>,
templater: Arc<dyn Templater>,
templater: &'static dyn Templater,
rules: OnceLock<Vec<ErasedRule>>,
}

impl Linter {
pub fn new(
config: FluffConfig,
formatter: Option<OutputStreamFormatter>,
templater: Option<Arc<dyn Templater>>,
templater: Option<&'static dyn Templater>,
) -> Linter {
let templater: Arc<dyn Templater> = match templater {
let templater: &'static dyn Templater = match templater {
Some(templater) => templater,
None => {
let templater = config.get("templater", "core").as_string();
match templater {
Some(templater) => {
let templaters = templaters();
match templaters.into_iter().find(|t| t.name() == templater) {
Some(t) => t.into(),
None => panic!("Unknown templater: {}", templater),
}
}
None => Arc::<RawTemplater>::default(),
let templater_name = config.get("templater", "core").as_string();
match templater_name {
Some(name) => match TEMPLATERS.into_iter().find(|t| t.name() == name) {
Some(t) => t,
None => panic!("Unknown templater: {}", name),
},
None => &RawTemplater,
}
}
};
Expand Down
18 changes: 8 additions & 10 deletions crates/lib/src/templaters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ pub mod placeholder;
pub mod python;
pub mod raw;

pub static RAW_TEMPLATER: RawTemplater = RawTemplater;
pub static PLACEHOLDER_TEMPLATER: PlaceholderTemplater = PlaceholderTemplater;
#[cfg(feature = "python")]
pub static PYTHON_TEMPLATER: PythonTemplater = PythonTemplater;

// templaters returns all the templaters that are available in the library
#[cfg(feature = "python")]
pub fn templaters() -> Vec<Box<dyn Templater>> {
vec![
Box::new(RawTemplater),
Box::new(PlaceholderTemplater),
Box::new(PythonTemplater),
]
}
pub static TEMPLATERS: [&'static dyn Templater; 3] =
[&RAW_TEMPLATER, &PLACEHOLDER_TEMPLATER, &PYTHON_TEMPLATER];

#[cfg(not(feature = "python"))]
pub fn templaters() -> Vec<Box<dyn Templater>> {
vec![Box::new(RawTemplater), Box::new(PlaceholderTemplater)]
}
pub static TEMPLATERS: [&'static dyn Templater; 2] = [&RAW_TEMPLATER, &PLACEHOLDER_TEMPLATER];

pub trait Templater: Send + Sync {
/// The name of the templater.
Expand Down

0 comments on commit e510072

Please sign in to comment.