diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
commit | 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch) | |
tree | 3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/askama/src/lib.rs | |
parent | Releasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip |
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/askama/src/lib.rs')
-rw-r--r-- | vendor/askama/src/lib.rs | 133 |
1 files changed, 76 insertions, 57 deletions
diff --git a/vendor/askama/src/lib.rs b/vendor/askama/src/lib.rs index 185e8cc76..17085b5d6 100644 --- a/vendor/askama/src/lib.rs +++ b/vendor/askama/src/lib.rs @@ -59,23 +59,27 @@ //! in the configuration file. The default syntax , "default", is the one //! provided by Askama. -#![allow(unused_imports)] +#![forbid(unsafe_code)] #![deny(elided_lifetimes_in_paths)] +#![deny(unreachable_pub)] -#[macro_use] -extern crate askama_derive; -pub use askama_shared as shared; +mod error; +pub mod filters; +pub mod helpers; -use std::fs::{self, DirEntry}; -use std::io; -use std::path::Path; +use std::fmt; -pub use askama_escape::{Html, Text}; +pub use askama_derive::Template; +pub use askama_escape::{Html, MarkupDisplay, Text}; + +#[doc(hidden)] +pub use crate as shared; +pub use crate::error::{Error, Result}; /// Main `Template` trait; implementations are generally derived /// /// If you need an object-safe template, use [`DynTemplate`]. -pub trait Template { +pub trait Template: fmt::Display { /// Helper method which allocates a new `String` and renders into it fn render(&self) -> Result<String> { let mut buf = String::with_capacity(Self::SIZE_HINT); @@ -83,14 +87,23 @@ pub trait Template { Ok(buf) } - /// Renders the template to the given `writer` buffer + /// Renders the template to the given `writer` fmt buffer fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()>; + /// Renders the template to the given `writer` io buffer + #[inline] + fn write_into(&self, writer: &mut (impl std::io::Write + ?Sized)) -> std::io::Result<()> { + writer.write_fmt(format_args!("{self}")) + } + /// The template's extension, if provided const EXTENSION: Option<&'static str>; /// Provides a conservative estimate of the expanded length of the rendered template const SIZE_HINT: usize; + + /// The MIME type (Content-Type) of the data that gets rendered by this Template + const MIME_TYPE: &'static str; } /// Object-safe wrapper trait around [`Template`] implementers @@ -100,14 +113,20 @@ pub trait DynTemplate { /// Helper method which allocates a new `String` and renders into it fn dyn_render(&self) -> Result<String>; - /// Renders the template to the given `writer` buffer + /// Renders the template to the given `writer` fmt buffer fn dyn_render_into(&self, writer: &mut dyn std::fmt::Write) -> Result<()>; + /// Renders the template to the given `writer` io buffer + fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()>; + /// Helper function to inspect the template's extension fn extension(&self) -> Option<&'static str>; /// Provides a conservative estimate of the expanded length of the rendered template fn size_hint(&self) -> usize; + + /// The MIME type (Content-Type) of the data that gets rendered by this Template + fn mime_type(&self) -> &'static str; } impl<T: Template> DynTemplate for T { @@ -119,6 +138,11 @@ impl<T: Template> DynTemplate for T { <Self as Template>::render_into(self, writer) } + #[inline] + fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> { + writer.write_fmt(format_args!("{self}")) + } + fn extension(&self) -> Option<&'static str> { Self::EXTENSION } @@ -126,75 +150,70 @@ impl<T: Template> DynTemplate for T { fn size_hint(&self) -> usize { Self::SIZE_HINT } -} -pub use crate::shared::filters; -pub use crate::shared::helpers; -pub use crate::shared::{read_config_file, Error, MarkupDisplay, Result}; -pub use askama_derive::*; - -pub mod mime { - #[cfg(all(feature = "mime_guess", feature = "mime"))] - pub fn extension_to_mime_type(ext: &str) -> mime_guess::Mime { - let basic_type = mime_guess::from_ext(ext).first_or_octet_stream(); - for (simple, utf_8) in &TEXT_TYPES { - if &basic_type == simple { - return utf_8.clone(); - } - } - basic_type + fn mime_type(&self) -> &'static str { + Self::MIME_TYPE } - - #[cfg(all(feature = "mime_guess", feature = "mime"))] - const TEXT_TYPES: [(mime_guess::Mime, mime_guess::Mime); 6] = [ - (mime::TEXT_PLAIN, mime::TEXT_PLAIN_UTF_8), - (mime::TEXT_HTML, mime::TEXT_HTML_UTF_8), - (mime::TEXT_CSS, mime::TEXT_CSS_UTF_8), - (mime::TEXT_CSV, mime::TEXT_CSV_UTF_8), - ( - mime::TEXT_TAB_SEPARATED_VALUES, - mime::TEXT_TAB_SEPARATED_VALUES_UTF_8, - ), - ( - mime::APPLICATION_JAVASCRIPT, - mime::APPLICATION_JAVASCRIPT_UTF_8, - ), - ]; } -/// Old build script helper to rebuild crates if contained templates have changed -/// -/// This function is now deprecated and does nothing. -#[deprecated( - since = "0.8.1", - note = "file-level dependency tracking is handled automatically without build script" -)] -pub fn rerun_if_templates_changed() {} +impl fmt::Display for dyn DynTemplate { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.dyn_render_into(f).map_err(|_| ::std::fmt::Error {}) + } +} #[cfg(test)] mod tests { - use super::{DynTemplate, Template}; + use std::fmt; + + use super::*; + use crate::{DynTemplate, Template}; #[test] fn dyn_template() { struct Test; impl Template for Test { - fn render_into( - &self, - writer: &mut (impl std::fmt::Write + ?Sized), - ) -> askama_shared::Result<()> { + fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()> { Ok(writer.write_str("test")?) } const EXTENSION: Option<&'static str> = Some("txt"); const SIZE_HINT: usize = 4; + + const MIME_TYPE: &'static str = "text/plain; charset=utf-8"; + } + + impl fmt::Display for Test { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.render_into(f).map_err(|_| fmt::Error {}) + } } fn render(t: &dyn DynTemplate) -> String { t.dyn_render().unwrap() } - assert_eq!(render(&Test), "test"); + let test = &Test as &dyn DynTemplate; + + assert_eq!(render(test), "test"); + + assert_eq!(test.to_string(), "test"); + + assert_eq!(format!("{test}"), "test"); + + let mut vec = Vec::new(); + test.dyn_write_into(&mut vec).unwrap(); + assert_eq!(vec, vec![b't', b'e', b's', b't']); } } + +/// Old build script helper to rebuild crates if contained templates have changed +/// +/// This function is now deprecated and does nothing. +#[deprecated( + since = "0.8.1", + note = "file-level dependency tracking is handled automatically without build script" +)] +pub fn rerun_if_templates_changed() {} |