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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Context information for the call to a helper.
use std::ops::Range;

use serde_json::{Map, Value};

use crate::{
    error::HelperError,
    helper::HelperResult,
    json,
    parser::ast::{Call, Node, Slice},
    render::assert::{assert, Type},
};

/// Represents a value to use when a variable lookup fails.
///
/// The underlying value is guaranteed to be `Value::String` and
/// encapsulates the raw value which can be either a path or sub-expression.
#[derive(Debug, Eq, PartialEq)]
pub enum MissingValue {
    /// Stores the raw value for a missing argument.
    Argument(usize, Value),
    /// Stores the raw value for a missing parameter.
    Parameter(String, Value),
}

/// Property represents a key/value pair.
///
/// This is used so that `blockHelperMissing` handlers have access
/// to the resolved property.
#[derive(Debug)]
pub struct Property {
    /// The path to the property.
    pub name: String,
    /// The resolved property value.
    pub value: Value,
}

/// Context for the call to a helper exposes immutable access to
/// the arguments and hash parameters.
///
/// It also provides some useful functions for asserting on argument
/// arity and type.
pub struct Context<'call> {
    // TODO: use call to generate context specific errors!
    call: &'call Call<'call>,
    name: String,
    arguments: Vec<Value>,
    parameters: Map<String, Value>,
    text: Option<&'call str>,
    property: Option<Property>,
    missing: Vec<MissingValue>,
}

impl<'call> Context<'call> {
    pub(crate) fn new(
        call: &'call Call<'call>,
        name: String,
        arguments: Vec<Value>,
        parameters: Map<String, Value>,
        text: Option<&'call str>,
        property: Option<Property>,
        missing: Vec<MissingValue>,
    ) -> Self {
        Self {
            call,
            name,
            arguments,
            parameters,
            text,
            property,
            missing,
        }
    }

    /// Get the name for the call.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the list of arguments.
    pub fn arguments(&self) -> &Vec<Value> {
        &self.arguments
    }

    /// Get the map of hash parameters.
    pub fn parameters(&self) -> &Map<String, Value> {
        &self.parameters
    }

    /// Get an argument at an index.
    pub fn get(&self, index: usize) -> Option<&Value> {
        self.arguments.get(index)
    }

    /// Get a hash parameter for the name.
    pub fn param(&self, name: &str) -> Option<&Value> {
        self.parameters.get(name)
    }

    /// Get an argument at an index and use a fallback string
    /// value when the argument is missing.
    pub fn get_fallback(&self, index: usize) -> Option<&Value> {
        let value = self.arguments.get(index);
        if let Some(&Value::Null) = value {
            if let Some(value) = self.missing(index) {
                return Some(value);
            }
        }
        value
    }

    /// Get a hash parameter for the name and use a fallback string
    /// value when the parameter is missing.
    pub fn param_fallback(&self, name: &str) -> Option<&Value> {
        let value = self.parameters.get(name);
        if let Some(&Value::Null) = value {
            if let Some(value) = self.missing_param(name) {
                return Some(value);
            }
        }
        value
    }

    /// Get the value for a missing argument.
    ///
    /// When the value for an argument is missing it is coerced to
    /// `Value::Null`; this function allows a helper to distinguish
    /// between a literal null value and a null resulting from a missing
    /// value.
    pub fn missing(&self, index: usize) -> Option<&Value> {
        for m in self.missing.iter() {
            if let MissingValue::Argument(ref i, ref value) = m {
                if i == &index {
                    return Some(value);
                }
            }
        }
        None
    }

    /// Get the value for a missing parameter.
    ///
    /// When the value for a parameter is missing it is coerced to
    /// `Value::Null`; this function allows a helper to distinguish
    /// between a literal null value and a null resulting from a missing
    /// value.
    pub fn missing_param(&self, name: &str) -> Option<&Value> {
        for m in self.missing.iter() {
            if let MissingValue::Parameter(ref key, ref value) = m {
                if key == name {
                    return Some(value);
                }
            }
        }
        None
    }

    /// Get the call syntax tree element.
    pub fn call(&self) -> &'call Call<'call> {
        self.call
    }

    /// Get the raw string value for an argument at an index.
    pub fn raw(&self, index: usize) -> Option<&str> {
        self.call.arguments().get(index).map(|v| v.as_str())
    }

    /// Get the raw string value for a hash parameter with the given name.
    pub fn raw_param(&self, name: &str) -> Option<&str> {
        self.call.parameters().get(name).map(|v| v.as_str())
    }

    /// Get an argument at an index and assert that the value
    /// is one of the given types.
    ///
    /// If no argument exists at the given index the value is
    /// treated as null and type assertion is performed on the
    /// null value.
    pub fn try_get(
        &self,
        index: usize,
        kinds: &[Type],
    ) -> HelperResult<&Value> {
        let value = self.arguments.get(index).or(Some(&Value::Null)).unwrap();
        // TODO: print ErrorInfo code snippet
        self.assert(value, kinds)?;
        Ok(value)
    }

    /// Get a hash parameter for the name and assert that the value
    /// is one of the given types.
    ///
    /// If no parameter exists for the given name the value is
    /// treated as null and type assertion is performed on the
    /// null value.
    pub fn try_param(
        &self,
        name: &str,
        kinds: &[Type],
    ) -> HelperResult<&Value> {
        let value = self.parameters.get(name).or(Some(&Value::Null)).unwrap();
        // TODO: print ErrorInfo code snippet
        self.assert(value, kinds)?;
        Ok(value)
    }

    /// Assert that a value is one of the given kinds.
    pub fn try_value<'a>(
        &self,
        value: &'a Value,
        kinds: &[Type],
    ) -> HelperResult<&'a Value> {
        self.assert(value, kinds)?;
        Ok(value)
    }

    /// Get the text for this context.
    ///
    /// Only available when invoked as a raw block.
    pub fn text(&self) -> &Option<&'call str> {
        &self.text
    }

    /// Get a resolved property.
    ///
    /// Only available to `blockHelperMissing` handlers.
    pub fn property(&self) -> &Option<Property> {
        &self.property
    }

    /// Assert that the call arguments have a valid arity.
    ///
    /// If the range start and end are equal than an exact number
    /// of arguments are expected and a more concise error message
    /// is used. Range ends are inclusive so 0..1 indicates zero or
    /// one arguments are allowed.
    pub fn arity(&self, range: Range<usize>) -> HelperResult<()> {
        if range.start == range.end {
            if self.arguments.len() != range.start {
                return Err(HelperError::ArityExact(
                    self.name.clone(),
                    range.start,
                ));
            }
        } else {
            if self.arguments.len() < range.start
                || self.arguments.len() > range.end
            {
                return Err(HelperError::ArityRange(
                    self.name.clone(),
                    range.start,
                    range.end,
                ));
            }
        }
        Ok(())
    }

    /// Assert on the type of a value.
    pub fn assert(&self, value: &Value, kinds: &[Type]) -> HelperResult<()> {
        let (result, kind) = assert(value, kinds);
        if !result {
            return Err(HelperError::TypeAssert(
                self.name().to_string(),
                kind.unwrap(),
                Type::from(value).to_string(),
            ));
        }
        Ok(())
    }

    /// Map an optional template to a result.
    ///
    /// If the template is `None` this will yield an error; use this
    /// to assert when an inner block template is required.
    pub fn assert_block<'a>(
        &self,
        template: Option<&'a Node<'a>>,
    ) -> HelperResult<&'a Node<'a>> {
        if let Some(node) = template {
            return Ok(node);
        }
        Err(HelperError::BlockTemplate(self.name().to_string()))
    }

    /// Assert that a block template is empty.
    ///
    /// Helpers that do not accept inner block templates can call this 
    /// to ensure that they are not invoked with the block syntax.
    pub fn assert_statement<'a>(
        &self,
        template: Option<&'a Node<'a>>) -> HelperResult<()> {
        if template.is_some() {
            return Err(HelperError::BlockTemplateNotAllowed(self.name().to_string()))
        }
        Ok(())
    }

    /// Lookup a field of a value.
    ///
    /// If the target value is not an object or array then this
    /// will yield `None`.
    pub fn lookup<'a, S: AsRef<str>>(
        &self,
        target: &'a Value,
        field: S,
    ) -> Option<&'a Value> {
        json::find_field(target, field)
    }

    /// Determine if a value is truthy.
    pub fn is_truthy(&self, value: &Value) -> bool {
        json::is_truthy(value)
    }
}