1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Core traits for is-functions that support both function and "is" test syntax.
//!
//! This module provides the `IsFunction` trait which allows a single implementation
//! to be registered as both a MiniJinja function and test (for `{% if x is name %}` syntax).
//!
//! # Example
//!
//! ```rust,ignore
//! use crate::is_functions::traits::IsFunction;
//! use crate::functions::metadata::{FunctionMetadata, ArgumentMetadata, SyntaxVariants};
//!
//! pub struct Email;
//!
//! impl IsFunction for Email {
//! const FUNCTION_NAME: &'static str = "is_email";
//! const IS_NAME: &'static str = "email";
//! const METADATA: FunctionMetadata = FunctionMetadata {
//! name: "is_email",
//! category: "validation",
//! description: "Validate email address format",
//! arguments: &[ArgumentMetadata {
//! name: "string",
//! arg_type: "string",
//! required: true,
//! default: None,
//! description: "The string to validate",
//! }],
//! return_type: "boolean",
//! examples: &[
//! "{{ is_email(string=\"user@example.com\") }}",
//! "{% if email is email %}valid{% endif %}",
//! ],
//! syntax: SyntaxVariants::FUNCTION_AND_TEST,
//! };
//!
//! fn call_as_function(kwargs: Kwargs) -> Result<Value, Error> {
//! let input: String = kwargs.get("string")?;
//! Ok(Value::from(Self::validate(&input)))
//! }
//!
//! fn call_as_is(value: &Value) -> bool {
//! value.as_str().map(|s| Self::validate(s)).unwrap_or(false)
//! }
//! }
//!
//! // Registration:
//! Email::register(&mut env);
//! ```
use crateTemplateContext;
use crateFunctionMetadata;
use Kwargs;
use ;
use Arc;
/// Trait for types that can be registered as both a MiniJinja function and test.
///
/// Implementors define how to handle both calling conventions:
/// - Function: `{{ is_email(string="value") }}` or `{% if is_email(string=x) %}`
/// - Is-test: `{% if value is email %}` - value is passed as first argument
///
/// # Usage
///
/// Both syntaxes are equivalent and produce the same result:
/// ```jinja
/// {% if is_email(string=user_input) %}valid{% endif %}
/// {% if user_input is email %}valid{% endif %}
/// ```
/// Trait for context-aware is-functions that need filesystem access.
///
/// This is similar to `IsFunction` but for functions that need access to
/// `TemplateContext` for path resolution and security checks.
///
/// # Example
///
/// ```rust,ignore
/// pub struct File;
///
/// impl ContextIsFunction for File {
/// const FUNCTION_NAME: &'static str = "is_file";
/// const IS_NAME: &'static str = "file";
///
/// fn call_as_function(context: Arc<TemplateContext>, kwargs: Kwargs) -> Result<Value, Error> {
/// let path: String = kwargs.get("path")?;
/// let resolved = context.validate_and_resolve_path(&path)?;
/// Ok(Value::from(resolved.is_file()))
/// }
///
/// fn call_as_is(context: Arc<TemplateContext>, value: &Value) -> bool {
/// value.as_str()
/// .and_then(|s| context.validate_and_resolve_path(s).ok())
/// .map(|p| p.is_file())
/// .unwrap_or(false)
/// }
/// }
/// ```