Skip to content

Commit

Permalink
Other clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Apr 19, 2023
1 parent 90ff56e commit 3ef951d
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub struct Context<'a> {
global_scope: Arc<RwLock<ValuesMap>>,
external_scope: ValuesMap,
scopes: Vec<Arc<RwLock<ValuesMap>>>,
callback_renderer: Arc<&'a TemplateEnv<'a>>,
callback_renderer: &'a TemplateEnv<'a>,
}

impl<'a> Context<'a> {
pub fn new(
external_scope: impl Serialize,
callback_renderer: Arc<&'a TemplateEnv<'_>>,
callback_renderer: &'a TemplateEnv<'_>,
) -> Self {
let v = serde_json::to_value(&external_scope).unwrap();
let external_scope: ValuesMap = serde_json::from_value(v).unwrap();
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<'a> Context<'a> {
pub fn set_global(&mut self, global_scope: Arc<RwLock<ValuesMap>>) {
self.global_scope = global_scope;
}
pub fn get_renderer_callback(&self) -> Arc<&'a TemplateEnv<'a>> {
self.callback_renderer.clone()
pub fn get_renderer_callback(&self) -> &'a TemplateEnv<'a> {
self.callback_renderer
}
}
4 changes: 2 additions & 2 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ impl Filter {
))),
}
}
pub fn filter<'a>(
pub fn filter(
&self,
base_value: Value,
params: &Option<CallParams<'a>>,
params: &Option<CallParams<'_>>,
context: Context<'_>,
) -> Result<Value> {
match &self {
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ where

#[test]
fn lex_numbers() {
let tokens: Vec<_> = Token::lexer("1 42 -100 3.14 -77.77").collect();
let tokens: Vec<_> = Token::lexer("1 42 -100 3.18 -77.77").collect();
assert_eq!(
tokens,
&[
Token::IntegerNum(1),
Token::IntegerNum(42),
Token::Minus,
Token::IntegerNum(100),
Token::FloatNum(3.14),
Token::FloatNum(3.18),
Token::Minus,
Token::FloatNum(77.77),
]
Expand Down
2 changes: 1 addition & 1 deletion src/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'a> Render for IncludeStatement<'a> {
if self.with_context {
template.render(out, params)
} else {
let mut context = Context::new(ValuesMap::default(), template_env.clone());
let mut context = Context::new(ValuesMap::default(), template_env);
context.set_global(template_env.globals());
template.render(out, context)
}
Expand Down
16 changes: 7 additions & 9 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use crate::template_parser::TemplateParser;
use serde::Serialize;
use std::borrow::Cow;
use std::io::Write;
use std::sync::Arc;


pub struct Template<'a> {
body: Cow<'a, str>,
template_env: Arc<&'a TemplateEnv<'a>>,
template_env: &'a TemplateEnv<'a>,
renderer: Option<ComposedRenderer<'a>>,
template_name: Option<String>,
}

impl<'a> Template<'a> {
pub fn new(template_env: Arc<&'a TemplateEnv<'_>>) -> Result<Self> {
pub fn new(template_env: &'a TemplateEnv<'_>) -> Result<Self> {
Ok(Self {
template_env,
renderer: None,
Expand All @@ -25,7 +25,7 @@ impl<'a> Template<'a> {
})
}
pub fn new_with_filename(
template_env: Arc<&'a TemplateEnv<'_>>,
template_env: &'a TemplateEnv<'_>,
template_name: String,
) -> Result<Self> {
Ok(Self {
Expand All @@ -38,14 +38,12 @@ impl<'a> Template<'a> {

pub fn parse(&self) -> Result<ComposedRenderer<'a>> {
let mut parser = match &self.body {
Cow::Borrowed(template_body) => {
TemplateParser::new(template_body, self.template_env.clone())?
}
Cow::Borrowed(template_body) => TemplateParser::new(template_body, self.template_env)?,
Cow::Owned(_template_body_owned) => {
// This allows the parser to have references to the template body.
// This is safe as long as `body` field is never mutated or dropped.
let unsafe_source: &'a str = unsafe { &*(&*self.body as *const str) };
TemplateParser::new(unsafe_source, self.template_env.clone())?
TemplateParser::new(unsafe_source, self.template_env)?
}
};
parser.parse()
Expand All @@ -63,7 +61,7 @@ impl<'a> Template<'a> {

pub fn render_as_string(&self, params: impl Serialize) -> Result<String> {
let mut b: Vec<u8> = Vec::new();
let mut context = Context::new(params, self.template_env.clone());
let mut context = Context::new(params, self.template_env);
context.set_global(self.template_env.globals());

self.render(&mut b, context)?;
Expand Down
2 changes: 1 addition & 1 deletion src/template_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a> TemplateEnv<'a> {
Ok(())
}
pub fn load_template(&self, filename: &str) -> Result<Template<'_>> {
let mut template = Template::new_with_filename(Arc::new(self), filename.to_string())?;
let mut template = Template::new_with_filename(self, filename.to_string())?;
let mut not_found = true;
for handler in &self.filesystem_handlers {
let stream = handler.open_stream(filename);
Expand Down
4 changes: 2 additions & 2 deletions src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::{Arc, RwLock};

pub struct TemplateParser<'a> {
template_body: &'a str,
env: RwLock<Arc<&'a TemplateEnv<'a>>>,
env: RwLock<&'a TemplateEnv<'a>>,
rough_tokenizer: Regex,
text_blocks: RwLock<Vec<TextBlockInfo>>,
current_block_info: RwLock<TextBlockInfo>,
Expand All @@ -22,7 +22,7 @@ pub struct TemplateParser<'a> {
}

impl<'a> TemplateParser<'a> {
pub fn new(body: &'a str, env: Arc<&'a TemplateEnv<'_>>) -> Result<Self> {
pub fn new(body: &'a str, env: &'a TemplateEnv<'_>) -> Result<Self> {
let rough_tokenizer = Regex::new(&ROUGH_TOKENIZER[..ROUGH_TOKENIZER.len() - 1]).unwrap();

Ok(Self {
Expand Down
10 changes: 3 additions & 7 deletions tests/scoped_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::sync::Arc;
use temple::error::Result;
use temple::value::{Value, ValuesMap};
use temple::{Template, TemplateEnv};
Expand All @@ -7,8 +6,7 @@ use temple::{Template, TemplateEnv};
fn test_global_variable() -> Result<()> {
let mut temp_env = TemplateEnv::default();
temp_env.add_global("GLOBAL_VAR".to_string(), "Global");
let template_env = Arc::new(&temp_env);
let mut template = Template::new(template_env)?;
let mut template = Template::new(&temp_env)?;
template.load("{{ GLOBAL_VAR }}")?;
let context = ValuesMap::default();
let result = template.render_as_string(context)?;
Expand All @@ -20,8 +18,7 @@ fn test_global_variable() -> Result<()> {
fn test_both_global_and_external_variables() -> Result<()> {
let mut temp_env = TemplateEnv::default();
temp_env.add_global("GLOBAL_VAR".to_string(), "Global");
let template_env = Arc::new(&temp_env);
let mut template = Template::new(template_env)?;
let mut template = Template::new(&temp_env)?;
template.load(
"global: {{ GLOBAL_VAR }}
external: {{external_variable}}",
Expand All @@ -46,8 +43,7 @@ fn test_override_value() -> Result<()> {
let mut temp_env = TemplateEnv::default();

temp_env.add_global("key".to_string(), "Global value");
let template_env = Arc::new(&temp_env);
let mut template = Template::new(template_env)?;
let mut template = Template::new(&temp_env)?;
template.load("{{ key }}")?;
let mut context = ValuesMap::default();
context.insert(
Expand Down
4 changes: 1 addition & 3 deletions tests/statement_include.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::sync::Arc;
use temple::error::Result;
use temple::value::{Value, ValuesMap};
use temple::{MemoryFileSystem, Template, TemplateEnv};
Expand All @@ -18,8 +17,7 @@ fn assert_render_template_with_includes_eq(

temp_env.add_global("bar".to_string(), 23);
temp_env.add_global("o".to_string(), 0);
let template_env = Arc::new(&temp_env);
let mut template = Template::new(template_env)?;
let mut template = Template::new(&temp_env)?;
template.load(input)?;
let default_context = ValuesMap::default();
let context = params.unwrap_or(default_context);
Expand Down
6 changes: 2 additions & 4 deletions tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::sync::Arc;
use temple::error::Result;
use temple::value::ValuesMap;
use temple::{Template, TemplateEnv};
Expand All @@ -8,9 +7,8 @@ pub fn assert_render_template_eq(
expected: &str,
params: Option<ValuesMap>,
) -> Result<()> {
let temp_env = TemplateEnv::default();
let template_env = Arc::new(&temp_env);
let mut template = Template::new(template_env)?;
let temp_env: TemplateEnv = TemplateEnv::default();
let mut template = Template::new(&temp_env)?;
template.load(input)?;
let default_context = ValuesMap::default();
let context = params.unwrap_or(default_context);
Expand Down

0 comments on commit 3ef951d

Please sign in to comment.