jmespath_extensions 0.9.0

Extended functions for JMESPath queries - 400+ functions for strings, arrays, dates, hashing, encoding, geo, and more
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Named Query Library parser for `.jpx` files.
//!
//! This module provides parsing support for query library files that contain
//! multiple named, reusable JMESPath expressions. The format is inspired by
//! SQLDelight/HugSQL patterns.
//!
//! # File Format
//!
//! ```text
//! -- :name top-keywords
//! -- :desc Extract top keywords from text field
//! tokens(@) | remove_stopwords(@) | stems(@) | frequencies(@)
//!
//! -- :name clean-html
//! -- :desc Strip HTML tags and normalize whitespace
//! regex_replace(@, `"<[^>]+>"`, `" "`) | collapse_whitespace(@)
//! ```
//!
//! ## Directives
//!
//! - `-- :name <name>` - Starts a new query (required)
//! - `-- :desc <description>` - Adds a description to the current query (optional)
//! - `-- ` - Other comment lines are ignored
//!
//! Everything between `-- :name` directives becomes the query expression.
//! Multi-line expressions are supported.
//!
//! # Example
//!
//! ```rust
//! use jmespath_extensions::query_library::QueryLibrary;
//!
//! let content = r#"
//! -- :name greet
//! -- :desc Simple greeting
//! `"hello"`
//!
//! -- :name count
//! length(@)
//! "#;
//!
//! let library = QueryLibrary::parse(content).unwrap();
//!
//! assert_eq!(library.names(), vec!["greet", "count"]);
//!
//! let greet = library.get("greet").unwrap();
//! assert_eq!(greet.name, "greet");
//! assert_eq!(greet.description, Some("Simple greeting".to_string()));
//! assert_eq!(greet.expression, r#"`"hello"`"#);
//!
//! let count = library.get("count").unwrap();
//! assert_eq!(count.expression, "length(@)");
//! ```
//!
//! # Detection
//!
//! Use [`is_query_library`] to check if content looks like a query library:
//!
//! ```rust
//! use jmespath_extensions::query_library::is_query_library;
//!
//! assert!(is_query_library("-- :name foo\nlength(@)"));
//! assert!(!is_query_library("length(@)"));
//! ```

use std::fmt;

/// Error type for query library parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    /// Error message
    pub message: String,
    /// Line number where the error occurred (1-indexed)
    pub line: Option<usize>,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.line {
            Some(line) => write!(f, "{} at line {}", self.message, line),
            None => write!(f, "{}", self.message),
        }
    }
}

impl std::error::Error for ParseError {}

impl ParseError {
    /// Create a new parse error with a message.
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            line: None,
        }
    }

    /// Create a new parse error with a message and line number.
    pub fn with_line(message: impl Into<String>, line: usize) -> Self {
        Self {
            message: message.into(),
            line: Some(line),
        }
    }
}

/// A named query with optional description.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedQuery {
    /// Query name (used for lookup)
    pub name: String,
    /// Optional description
    pub description: Option<String>,
    /// The JMESPath expression
    pub expression: String,
    /// Line number where the query starts (1-indexed, for error messages)
    pub line_number: usize,
}

/// A collection of named queries parsed from a `.jpx` file.
#[derive(Debug, Clone, Default)]
pub struct QueryLibrary {
    queries: Vec<NamedQuery>,
}

impl QueryLibrary {
    /// Create an empty query library.
    pub fn new() -> Self {
        Self::default()
    }

    /// Parse a query library from file content.
    ///
    /// # Format
    ///
    /// - `-- :name <name>` starts a new query
    /// - `-- :desc <description>` adds a description to the current query
    /// - `-- ` other comment lines are ignored
    /// - Non-comment lines are appended to the current query's expression
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - A query has an empty name
    /// - A query has no expression
    /// - Duplicate query names are found
    /// - No queries are found in the content
    ///
    /// # Example
    ///
    /// ```rust
    /// use jmespath_extensions::query_library::QueryLibrary;
    ///
    /// let content = r#"
    /// -- :name count
    /// length(@)
    /// "#;
    ///
    /// let library = QueryLibrary::parse(content).unwrap();
    /// assert_eq!(library.len(), 1);
    /// ```
    pub fn parse(content: &str) -> Result<Self, ParseError> {
        let mut queries = Vec::new();
        let mut current_name: Option<String> = None;
        let mut current_desc: Option<String> = None;
        let mut current_expr = String::new();
        let mut current_line_number = 0usize;

        for (line_num, line) in content.lines().enumerate() {
            let line_number = line_num + 1; // 1-indexed for error messages
            let trimmed = line.trim();

            if let Some(rest) = trimmed.strip_prefix("-- :name ").or_else(|| {
                // Handle "-- :name" without trailing space (empty name case)
                if trimmed == "-- :name" {
                    Some("")
                } else {
                    None
                }
            }) {
                // Save previous query if exists
                if let Some(name) = current_name.take() {
                    let expr = current_expr.trim().to_string();
                    if expr.is_empty() {
                        return Err(ParseError::with_line(
                            format!("Query '{}' has no expression", name),
                            current_line_number,
                        ));
                    }
                    queries.push(NamedQuery {
                        name,
                        description: current_desc.take(),
                        expression: expr,
                        line_number: current_line_number,
                    });
                    current_expr.clear();
                }

                // Start new query
                let name = rest.trim().to_string();
                if name.is_empty() {
                    return Err(ParseError::with_line("Empty query name", line_number));
                }

                // Check for duplicates
                if queries.iter().any(|q| q.name == name) {
                    return Err(ParseError::with_line(
                        format!("Duplicate query name '{}'", name),
                        line_number,
                    ));
                }

                current_name = Some(name);
                current_line_number = line_number;
            } else if let Some(rest) = trimmed.strip_prefix("-- :desc ") {
                // Add description to current query
                if current_name.is_some() {
                    current_desc = Some(rest.trim().to_string());
                }
            } else if trimmed.starts_with("-- ") || trimmed == "--" {
                // Skip other comments
            } else if !trimmed.is_empty() {
                // Append to current expression
                if current_name.is_some() {
                    if !current_expr.is_empty() {
                        current_expr.push('\n');
                    }
                    current_expr.push_str(line);
                }
            }
        }

        // Save final query
        if let Some(name) = current_name {
            let expr = current_expr.trim().to_string();
            if expr.is_empty() {
                return Err(ParseError::with_line(
                    format!("Query '{}' has no expression", name),
                    current_line_number,
                ));
            }
            queries.push(NamedQuery {
                name,
                description: current_desc,
                expression: expr,
                line_number: current_line_number,
            });
        }

        if queries.is_empty() {
            return Err(ParseError::new(
                "No queries found. Use '-- :name <query-name>' to define queries.",
            ));
        }

        Ok(QueryLibrary { queries })
    }

    /// Get a query by name.
    ///
    /// # Example
    ///
    /// ```rust
    /// use jmespath_extensions::query_library::QueryLibrary;
    ///
    /// let lib = QueryLibrary::parse("-- :name test\nlength(@)").unwrap();
    /// let query = lib.get("test").unwrap();
    /// assert_eq!(query.expression, "length(@)");
    /// ```
    pub fn get(&self, name: &str) -> Option<&NamedQuery> {
        self.queries.iter().find(|q| q.name == name)
    }

    /// Get all queries.
    ///
    /// # Example
    ///
    /// ```rust
    /// use jmespath_extensions::query_library::QueryLibrary;
    ///
    /// let lib = QueryLibrary::parse("-- :name a\n`1`\n-- :name b\n`2`").unwrap();
    /// assert_eq!(lib.list().len(), 2);
    /// ```
    pub fn list(&self) -> &[NamedQuery] {
        &self.queries
    }

    /// Get query names.
    ///
    /// # Example
    ///
    /// ```rust
    /// use jmespath_extensions::query_library::QueryLibrary;
    ///
    /// let lib = QueryLibrary::parse("-- :name foo\n`1`\n-- :name bar\n`2`").unwrap();
    /// assert_eq!(lib.names(), vec!["foo", "bar"]);
    /// ```
    pub fn names(&self) -> Vec<&str> {
        self.queries.iter().map(|q| q.name.as_str()).collect()
    }

    /// Get the number of queries in the library.
    pub fn len(&self) -> usize {
        self.queries.len()
    }

    /// Check if the library is empty.
    pub fn is_empty(&self) -> bool {
        self.queries.is_empty()
    }

    /// Iterate over queries.
    pub fn iter(&self) -> impl Iterator<Item = &NamedQuery> {
        self.queries.iter()
    }
}

impl<'a> IntoIterator for &'a QueryLibrary {
    type Item = &'a NamedQuery;
    type IntoIter = std::slice::Iter<'a, NamedQuery>;

    fn into_iter(self) -> Self::IntoIter {
        self.queries.iter()
    }
}

/// Check if content looks like a query library (starts with `-- :name`).
///
/// This function checks if the first non-empty line starts with `-- :name `,
/// indicating a query library format.
///
/// # Example
///
/// ```rust
/// use jmespath_extensions::query_library::is_query_library;
///
/// assert!(is_query_library("-- :name foo\nlength(@)"));
/// assert!(is_query_library("  -- :name foo\nlength(@)"));
/// assert!(is_query_library("\n-- :name foo\nlength(@)"));
/// assert!(!is_query_library("length(@)"));
/// assert!(!is_query_library("-- comment\nlength(@)"));
/// ```
pub fn is_query_library(content: &str) -> bool {
    content
        .lines()
        .find(|line| !line.trim().is_empty())
        .map(|line| line.trim().starts_with("-- :name "))
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_library() {
        let content = r#"
-- :name greet
-- :desc Simple greeting
`"hello"`

-- :name count
length(@)
"#;
        let lib = QueryLibrary::parse(content).unwrap();
        assert_eq!(lib.len(), 2);

        let greet = lib.get("greet").unwrap();
        assert_eq!(greet.name, "greet");
        assert_eq!(greet.description, Some("Simple greeting".to_string()));
        assert_eq!(greet.expression, "`\"hello\"`");

        let count = lib.get("count").unwrap();
        assert_eq!(count.name, "count");
        assert_eq!(count.description, None);
        assert_eq!(count.expression, "length(@)");
    }

    #[test]
    fn test_parse_multiline_expression() {
        let content = r#"
-- :name complex
-- :desc Multi-line query
{
  total: length(@),
  first: @[0]
}
"#;
        let lib = QueryLibrary::parse(content).unwrap();
        let query = lib.get("complex").unwrap();
        assert!(query.expression.contains("total: length(@)"));
        assert!(query.expression.contains("first: @[0]"));
    }

    #[test]
    fn test_parse_empty_name_error() {
        let content = "-- :name \nlength(@)";
        let result = QueryLibrary::parse(content);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("Empty query name"));
        assert_eq!(err.line, Some(1));
    }

    #[test]
    fn test_parse_duplicate_name_error() {
        let content = r#"
-- :name foo
length(@)

-- :name foo
keys(@)
"#;
        let result = QueryLibrary::parse(content);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("Duplicate query name"));
    }

    #[test]
    fn test_parse_no_expression_error() {
        let content = "-- :name empty\n-- :name another\nlength(@)";
        let result = QueryLibrary::parse(content);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("has no expression"));
    }

    #[test]
    fn test_parse_no_queries_error() {
        let content = "-- just a comment\nlength(@)";
        let result = QueryLibrary::parse(content);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("No queries found"));
    }

    #[test]
    fn test_is_query_library() {
        assert!(is_query_library("-- :name foo\nlength(@)"));
        assert!(is_query_library("  -- :name foo\nlength(@)"));
        assert!(is_query_library("\n-- :name foo\nlength(@)"));
        assert!(!is_query_library("length(@)"));
        assert!(!is_query_library("-- comment\nlength(@)"));
    }

    #[test]
    fn test_comments_ignored() {
        let content = r#"
-- :name test
-- :desc Description
-- This is a regular comment
-- Another comment
length(@)
-- Trailing comment
"#;
        let lib = QueryLibrary::parse(content).unwrap();
        let query = lib.get("test").unwrap();
        assert_eq!(query.expression, "length(@)");
    }

    #[test]
    fn test_iter() {
        let content = "-- :name a\n`1`\n-- :name b\n`2`";
        let lib = QueryLibrary::parse(content).unwrap();
        let names: Vec<_> = lib.iter().map(|q| &q.name).collect();
        assert_eq!(names, vec!["a", "b"]);
    }

    #[test]
    fn test_into_iter() {
        let content = "-- :name x\n`1`";
        let lib = QueryLibrary::parse(content).unwrap();
        for query in &lib {
            assert_eq!(query.name, "x");
        }
    }
}