Browse Source

Improved error

Improved the error so it actually works with Askama.
Also updated some docs.
tags/0.1.2
Steven vanZyl 5 months ago
parent
commit
6ae1fcbe83
3 changed files with 37 additions and 5 deletions
  1. 2
    2
      Cargo.toml
  2. 29
    3
      src/error.rs
  3. 6
    0
      src/lib.rs

+ 2
- 2
Cargo.toml View File

@@ -1,7 +1,7 @@
1 1
 [package]
2 2
 name = "askama-filters"
3 3
 description = "Extra template filters for Askama"
4
-version = "0.1.0"
4
+version = "0.1.1"
5 5
 authors = ["Steven vanZyl <rushsteve1@rushsteve1.us>"]
6 6
 edition = "2018"
7 7
 license = "MIT"
@@ -9,7 +9,7 @@ readme = "README.md"
9 9
 keywords = ["askama", "filter", "text", "html"]
10 10
 
11 11
 [dependencies]
12
-# askama = "0.8.0"
12
+askama_shared = "0.8.0"
13 13
 regex = "1.1.6"
14 14
 pulldown-cmark = { version = "0.4.1", optional = true }
15 15
 chrono = { version = "0.4.6", optional = true }

+ 29
- 3
src/error.rs View File

@@ -1,6 +1,7 @@
1 1
 //! Custom error type handling all the errors the
2 2
 //! filters can throw.
3 3
 
4
+use askama_shared;
4 5
 use regex;
5 6
 use std::fmt;
6 7
 
@@ -18,17 +19,36 @@ pub enum Error {
18 19
     Markdown,
19 20
     #[cfg(feature = "date")]
20 21
     ChronoParse(chrono::ParseError),
22
+
23
+    /// This error needs to be non-exhaustive as
24
+    /// variants existence depend on features.
25
+    #[doc(hidden)]
26
+    __Nonexhaustive,
21 27
 }
22 28
 
23 29
 impl ErrorTrait for Error {
30
+    // Here for compatibility reasons
31
+    fn description(&self) -> &str {
32
+        match self {
33
+            Error::Fmt(e) => e.description(),
34
+            Error::RegEx(e) => e.description(),
35
+            #[cfg(feature = "markdown")]
36
+            Error::Markdown => "Markdown Error",
37
+            #[cfg(feature = "date")]
38
+            Error::ChronoParse(e) => e.description(),
39
+            _ => "unknown error: __Nonexhaustive",
40
+        }
41
+    }
42
+
24 43
     fn source(&self) -> Option<&(dyn ErrorTrait + 'static)> {
25 44
         match self {
26
-            Error::Fmt(e) => Some(e),
27
-            Error::RegEx(e) => Some(e),
45
+            Error::Fmt(ref e) => Some(e),
46
+            Error::RegEx(ref e) => Some(e),
28 47
             #[cfg(feature = "markdown")]
29 48
             Error::Markdown => None,
30 49
             #[cfg(feature = "date")]
31
-            Error::ChronoParse(e) => Some(e),
50
+            Error::ChronoParse(ref e) => Some(e),
51
+            _ => None,
32 52
         }
33 53
     }
34 54
 }
@@ -58,4 +78,10 @@ impl From<chrono::ParseError> for Error {
58 78
     }
59 79
 }
60 80
 
81
+impl From<Error> for askama_shared::Error {
82
+    fn from(_f: Error) -> Self {
83
+        askama_shared::Error::__Nonexhaustive
84
+    }
85
+}
86
+
61 87
 pub type Result<T> = std::result::Result<T, Error>;

+ 6
- 0
src/lib.rs View File

@@ -17,6 +17,12 @@
17 17
 //! to it. Then import that module wherever you are creating `Template`s
18 18
 //! and both sets of filters should be available.
19 19
 //!
20
+//! A number of filters in this library return HTML markup.
21
+//! For security reasons these are not automatically considered safe
22
+//! and you most likely will want to chain them with the `safe` filter
23
+//! like so `var|markdown|safe`. Escaping inbetween is also recommeneded
24
+//! such as `var|e|md|safe`.
25
+//!
20 26
 //! ## Features
21 27
 //!
22 28
 //! There are a few optional features that can be enabled:

Loading…
Cancel
Save