mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 07:20:55 +00:00
derive: Replace Rc
s with Arc
s
This commit is contained in:
parent
85330b353a
commit
b295ff62ef
@ -1,7 +1,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
use parser::node::Whitespace;
|
use parser::node::Whitespace;
|
||||||
@ -120,7 +120,7 @@ impl<'a> Config<'a> {
|
|||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
start_at: Option<&Path>,
|
start_at: Option<&Path>,
|
||||||
) -> std::result::Result<Rc<Path>, CompileError> {
|
) -> std::result::Result<Arc<Path>, CompileError> {
|
||||||
if let Some(root) = start_at {
|
if let Some(root) = start_at {
|
||||||
let relative = root.with_file_name(path);
|
let relative = root.with_file_name(path);
|
||||||
if relative.exists() {
|
if relative.exists() {
|
||||||
@ -294,8 +294,8 @@ fn str_set<'a>(vals: &[&'a str]) -> Vec<Cow<'a, str>> {
|
|||||||
|
|
||||||
pub(crate) fn get_template_source(
|
pub(crate) fn get_template_source(
|
||||||
tpl_path: &Path,
|
tpl_path: &Path,
|
||||||
import_from: Option<(&Rc<Path>, &str, &str)>,
|
import_from: Option<(&Arc<Path>, &str, &str)>,
|
||||||
) -> Result<Rc<str>, CompileError> {
|
) -> Result<Arc<str>, CompileError> {
|
||||||
match fs::read_to_string(tpl_path) {
|
match fs::read_to_string(tpl_path) {
|
||||||
Ok(mut source) => {
|
Ok(mut source) => {
|
||||||
if source.ends_with('\n') {
|
if source.ends_with('\n') {
|
||||||
|
@ -3,7 +3,7 @@ use std::collections::hash_map::{Entry, HashMap};
|
|||||||
use std::fmt::{Arguments, Display, Write};
|
use std::fmt::{Arguments, Display, Write};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::{cmp, hash, mem, str};
|
use std::{cmp, hash, mem, str};
|
||||||
|
|
||||||
use parser::node::{
|
use parser::node::{
|
||||||
@ -21,7 +21,7 @@ pub(crate) struct Generator<'a> {
|
|||||||
// The template input state: original struct AST and attributes
|
// The template input state: original struct AST and attributes
|
||||||
input: &'a TemplateInput<'a>,
|
input: &'a TemplateInput<'a>,
|
||||||
// All contexts, keyed by the package-relative template path
|
// All contexts, keyed by the package-relative template path
|
||||||
contexts: &'a HashMap<&'a Rc<Path>, Context<'a>>,
|
contexts: &'a HashMap<&'a Arc<Path>, Context<'a>>,
|
||||||
// The heritage contains references to blocks and their ancestry
|
// The heritage contains references to blocks and their ancestry
|
||||||
heritage: Option<&'a Heritage<'a>>,
|
heritage: Option<&'a Heritage<'a>>,
|
||||||
// Variables accessible directly from the current scope (not redirected to context)
|
// Variables accessible directly from the current scope (not redirected to context)
|
||||||
@ -44,7 +44,7 @@ pub(crate) struct Generator<'a> {
|
|||||||
impl<'a> Generator<'a> {
|
impl<'a> Generator<'a> {
|
||||||
pub(crate) fn new<'n>(
|
pub(crate) fn new<'n>(
|
||||||
input: &'n TemplateInput<'_>,
|
input: &'n TemplateInput<'_>,
|
||||||
contexts: &'n HashMap<&'n Rc<Path>, Context<'n>>,
|
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
|
||||||
heritage: Option<&'n Heritage<'_>>,
|
heritage: Option<&'n Heritage<'_>>,
|
||||||
locals: MapChain<'n, Cow<'n, str>, LocalMeta>,
|
locals: MapChain<'n, Cow<'n, str>, LocalMeta>,
|
||||||
buf_writable_discard: bool,
|
buf_writable_discard: bool,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use parser::node::{BlockDef, Macro};
|
use parser::node::{BlockDef, Macro};
|
||||||
use parser::{Node, Parsed, WithSpan};
|
use parser::{Node, Parsed, WithSpan};
|
||||||
@ -16,7 +16,7 @@ pub(crate) struct Heritage<'a> {
|
|||||||
impl Heritage<'_> {
|
impl Heritage<'_> {
|
||||||
pub(crate) fn new<'n>(
|
pub(crate) fn new<'n>(
|
||||||
mut ctx: &'n Context<'n>,
|
mut ctx: &'n Context<'n>,
|
||||||
contexts: &'n HashMap<&'n Rc<Path>, Context<'n>>,
|
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
|
||||||
) -> Heritage<'n> {
|
) -> Heritage<'n> {
|
||||||
let mut blocks: BlockAncestry<'n> = ctx
|
let mut blocks: BlockAncestry<'n> = ctx
|
||||||
.blocks
|
.blocks
|
||||||
@ -40,10 +40,10 @@ type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a BlockDef<'a>
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct Context<'a> {
|
pub(crate) struct Context<'a> {
|
||||||
pub(crate) nodes: &'a [Node<'a>],
|
pub(crate) nodes: &'a [Node<'a>],
|
||||||
pub(crate) extends: Option<Rc<Path>>,
|
pub(crate) extends: Option<Arc<Path>>,
|
||||||
pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>>,
|
pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>>,
|
||||||
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
|
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
|
||||||
pub(crate) imports: HashMap<&'a str, Rc<Path>>,
|
pub(crate) imports: HashMap<&'a str, Arc<Path>>,
|
||||||
path: Option<&'a Path>,
|
path: Option<&'a Path>,
|
||||||
parsed: &'a Parsed,
|
parsed: &'a Parsed,
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::hash_map::{Entry, HashMap};
|
use std::collections::hash_map::{Entry, HashMap};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
@ -22,7 +22,7 @@ pub(crate) struct TemplateInput<'a> {
|
|||||||
pub(crate) escaper: &'a str,
|
pub(crate) escaper: &'a str,
|
||||||
pub(crate) ext: Option<&'a str>,
|
pub(crate) ext: Option<&'a str>,
|
||||||
pub(crate) mime_type: String,
|
pub(crate) mime_type: String,
|
||||||
pub(crate) path: Rc<Path>,
|
pub(crate) path: Arc<Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TemplateInput<'_> {
|
impl TemplateInput<'_> {
|
||||||
@ -113,18 +113,18 @@ impl TemplateInput<'_> {
|
|||||||
|
|
||||||
pub(crate) fn find_used_templates(
|
pub(crate) fn find_used_templates(
|
||||||
&self,
|
&self,
|
||||||
map: &mut HashMap<Rc<Path>, Parsed>,
|
map: &mut HashMap<Arc<Path>, Parsed>,
|
||||||
) -> Result<(), CompileError> {
|
) -> Result<(), CompileError> {
|
||||||
let (source, source_path) = match &self.source {
|
let (source, source_path) = match &self.source {
|
||||||
Source::Source(s) => (s.clone(), None),
|
Source::Source(s) => (s.clone(), None),
|
||||||
Source::Path(_) => (
|
Source::Path(_) => (
|
||||||
get_template_source(&self.path, None)?,
|
get_template_source(&self.path, None)?,
|
||||||
Some(Rc::clone(&self.path)),
|
Some(Arc::clone(&self.path)),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut dependency_graph = Vec::new();
|
let mut dependency_graph = Vec::new();
|
||||||
let mut check = vec![(Rc::clone(&self.path), source, source_path)];
|
let mut check = vec![(Arc::clone(&self.path), source, source_path)];
|
||||||
while let Some((path, source, source_path)) = check.pop() {
|
while let Some((path, source, source_path)) = check.pop() {
|
||||||
let parsed = Parsed::new(source, source_path, self.syntax)?;
|
let parsed = Parsed::new(source, source_path, self.syntax)?;
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ impl TemplateInput<'_> {
|
|||||||
let mut nested = vec![parsed.nodes()];
|
let mut nested = vec![parsed.nodes()];
|
||||||
while let Some(nodes) = nested.pop() {
|
while let Some(nodes) = nested.pop() {
|
||||||
for n in nodes {
|
for n in nodes {
|
||||||
let mut add_to_check = |new_path: Rc<Path>| -> Result<(), CompileError> {
|
let mut add_to_check = |new_path: Arc<Path>| -> Result<(), CompileError> {
|
||||||
if let Entry::Vacant(e) = map.entry(new_path) {
|
if let Entry::Vacant(e) = map.entry(new_path) {
|
||||||
// Add a dummy entry to `map` in order to prevent adding `path`
|
// Add a dummy entry to `map` in order to prevent adding `path`
|
||||||
// multiple times to `check`.
|
// multiple times to `check`.
|
||||||
@ -422,7 +422,7 @@ fn extension(path: &Path) -> Option<&str> {
|
|||||||
#[derive(Debug, Hash, PartialEq)]
|
#[derive(Debug, Hash, PartialEq)]
|
||||||
pub(crate) enum Source {
|
pub(crate) enum Source {
|
||||||
Path(String),
|
Path(String),
|
||||||
Source(Rc<str>),
|
Source(Arc<str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
|
||||||
@ -483,7 +483,7 @@ const TEXT_TYPES: [(Mime, Mime); 7] = [
|
|||||||
(mime::IMAGE_SVG, mime::IMAGE_SVG),
|
(mime::IMAGE_SVG, mime::IMAGE_SVG),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn cyclic_graph_error(dependency_graph: &[(Rc<Path>, Rc<Path>)]) -> Result<(), CompileError> {
|
fn cyclic_graph_error(dependency_graph: &[(Arc<Path>, Arc<Path>)]) -> Result<(), CompileError> {
|
||||||
Err(CompileError::no_file_info(format!(
|
Err(CompileError::no_file_info(format!(
|
||||||
"cyclic dependency in graph {:#?}",
|
"cyclic dependency in graph {:#?}",
|
||||||
dependency_graph
|
dependency_graph
|
||||||
|
@ -6,7 +6,7 @@ use std::cell::Cell;
|
|||||||
use std::env::current_dir;
|
use std::env::current_dir;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
use nom::branch::alt;
|
use nom::branch::alt;
|
||||||
@ -30,7 +30,7 @@ mod tests;
|
|||||||
|
|
||||||
mod _parsed {
|
mod _parsed {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
|
|
||||||
use super::node::Node;
|
use super::node::Node;
|
||||||
@ -40,15 +40,15 @@ mod _parsed {
|
|||||||
// `source` must outlive `ast`, so `ast` must be declared before `source`
|
// `source` must outlive `ast`, so `ast` must be declared before `source`
|
||||||
ast: Ast<'static>,
|
ast: Ast<'static>,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
source: Rc<str>,
|
source: Arc<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parsed {
|
impl Parsed {
|
||||||
/// If `file_path` is `None`, it means the `source` is an inline template. Therefore, if
|
/// If `file_path` is `None`, it means the `source` is an inline template. Therefore, if
|
||||||
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
|
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
source: Rc<str>,
|
source: Arc<str>,
|
||||||
file_path: Option<Rc<Path>>,
|
file_path: Option<Arc<Path>>,
|
||||||
syntax: &Syntax<'_>,
|
syntax: &Syntax<'_>,
|
||||||
) -> Result<Self, ParseError> {
|
) -> Result<Self, ParseError> {
|
||||||
// Self-referential borrowing: `self` will keep the source alive as `String`,
|
// Self-referential borrowing: `self` will keep the source alive as `String`,
|
||||||
@ -105,7 +105,7 @@ impl<'a> Ast<'a> {
|
|||||||
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
|
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
|
||||||
pub fn from_str(
|
pub fn from_str(
|
||||||
src: &'a str,
|
src: &'a str,
|
||||||
file_path: Option<Rc<Path>>,
|
file_path: Option<Arc<Path>>,
|
||||||
syntax: &Syntax<'_>,
|
syntax: &Syntax<'_>,
|
||||||
) -> Result<Self, ParseError> {
|
) -> Result<Self, ParseError> {
|
||||||
let parse = |i: &'a str| Node::many(i, &State::new(syntax));
|
let parse = |i: &'a str| Node::many(i, &State::new(syntax));
|
||||||
@ -204,7 +204,7 @@ pub enum ParseError {
|
|||||||
row: usize,
|
row: usize,
|
||||||
column: usize,
|
column: usize,
|
||||||
source_after: String,
|
source_after: String,
|
||||||
file_path: Option<Rc<Path>>,
|
file_path: Option<Arc<Path>>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user