microsoft-webui-parser 0.0.25

Core parser for WebUI framework
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Route element parsing for `<route>` directives.
//!
//! Parses `<route path="..." component="..." ...>` elements into
//! `WebUIFragmentRoute` protocol fragments.

use std::collections::HashSet;

use webui_protocol::WebUiFragmentRoute;

use crate::error::{ParserError, Result};

/// Maximum number of path parameters allowed per route.
const MAX_PARAMS_PER_ROUTE: usize = 20;

/// Parsed attributes from a single `<route>` element.
#[derive(Debug, Default)]
pub(crate) struct RouteAttributes {
    pub path: String,
    pub component: String,
    pub exact: bool,
    /// Comma-separated allowlist of query parameters forwarded as attributes.
    pub query: String,
    /// When true, the router keeps the component alive across navigations.
    pub keep_alive: bool,
    /// Cache tag templates (e.g. `["thread:{threadId}", "inbox"]`).
    /// Placeholders like `{param}` are resolved at render time.
    pub cache_tags: Vec<String>,
    /// Invalidation tag templates (e.g. `["inbox", "sent"]`).
    /// After a mutation action, these tags are auto-invalidated.
    /// Supports `{param}` placeholders resolved at render time.
    pub invalidates: Vec<String>,
    /// Component tag name for pending/loading UI.
    pub pending_component: String,
    /// Component tag name for error boundary UI.
    pub error_component: String,
}

/// Iteratively extract `:param` and `*splat` tokens from a path template.
///
/// Returns param names without the `:` or `*` prefix.
/// Validates the path template for correctness.
/// Handles relative paths (starting with `./`) by stripping the prefix
/// before parameter extraction.
pub(crate) fn extract_params(path: &str) -> Result<Vec<String>> {
    // Strip relative prefix — params are the same regardless
    let normalized = path.strip_prefix("./").unwrap_or(path);

    let mut params = Vec::new();
    let mut seen = HashSet::new();

    for segment in normalized.split('/') {
        if segment.is_empty() {
            continue;
        }

        // :param or :param? (optional)
        if let Some(rest) = segment.strip_prefix(':') {
            let name = rest.trim_end_matches('?');
            if name.is_empty() {
                return Err(ParserError::Directive(format!(
                    "Empty parameter name in path: {path}"
                )));
            }
            if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                return Err(ParserError::Directive(format!(
                    "Invalid parameter name '{name}' in path: {path}"
                )));
            }
            if !seen.insert(name.to_string()) {
                return Err(ParserError::Directive(format!(
                    "Duplicate parameter name '{name}' in path: {path}"
                )));
            }
            params.push(name.to_string());
        }
        // *splat
        else if let Some(rest) = segment.strip_prefix('*') {
            let name = if rest.is_empty() { "rest" } else { rest };
            if !seen.insert(name.to_string()) {
                return Err(ParserError::Directive(format!(
                    "Duplicate splat name '{name}' in path: {path}"
                )));
            }
            params.push(name.to_string());
        }
    }

    if params.len() > MAX_PARAMS_PER_ROUTE {
        return Err(ParserError::Directive(format!(
            "Too many parameters ({}) in path: {path} (max {MAX_PARAMS_PER_ROUTE})",
            params.len()
        )));
    }

    Ok(params)
}

/// Validate route attributes for consistency.
pub(crate) fn validate_attributes(attrs: &RouteAttributes) -> Result<()> {
    // component is required
    if attrs.component.is_empty() {
        return Err(ParserError::Directive(format!(
            "Route '{}' must have a 'component' attribute",
            attrs.path
        )));
    }

    Ok(())
}

/// Parse a comma-separated list of cache tags or invalidation tags.
///
/// Splits on `,`, trims whitespace, and discards empty entries.
pub(crate) fn parse_tag_list(raw: &str) -> Vec<String> {
    let mut tags = Vec::new();
    for part in raw.split(',') {
        let trimmed = part.trim();
        if !trimmed.is_empty() {
            tags.push(trimmed.to_string());
        }
    }
    tags
}

/// Extract `{param}` placeholder names from tag templates.
///
/// Returns the set of param names referenced in tags like `"thread:{threadId}"`.
/// Validates that placeholder syntax is well-formed.
pub(crate) fn extract_tag_placeholders(tags: &[String]) -> Result<HashSet<String>> {
    let mut placeholders = HashSet::new();

    for tag in tags {
        let bytes = tag.as_bytes();
        let len = bytes.len();
        let mut i = 0;

        while i < len {
            if bytes[i] == b'{' {
                let start = i + 1;
                let end = tag[start..].find('}').map(|j| start + j);
                match end {
                    Some(end_idx) => {
                        let name = &tag[start..end_idx];
                        if name.is_empty() {
                            return Err(ParserError::Directive(format!(
                                "Empty placeholder '{{}}' in cache tag: {tag}"
                            )));
                        }
                        if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                            return Err(ParserError::Directive(format!(
                                "Invalid placeholder name '{name}' in cache tag: {tag}"
                            )));
                        }
                        placeholders.insert(name.to_string());
                        i = end_idx + 1;
                    }
                    None => {
                        return Err(ParserError::Directive(format!(
                            "Unclosed placeholder '{{' in cache tag: {tag}"
                        )));
                    }
                }
            } else {
                i += 1;
            }
        }
    }

    Ok(placeholders)
}

/// Validate that `{param}` placeholders in tags reference actual route params.
///
/// `available_params` should include params from this route AND all ancestor routes.
pub(crate) fn validate_tag_placeholders(
    tags: &[String],
    available_params: &HashSet<String>,
    attr_name: &str,
    route_path: &str,
) -> Result<()> {
    let placeholders = extract_tag_placeholders(tags)?;
    for name in &placeholders {
        if !available_params.contains(name) {
            return Err(ParserError::Directive(format!(
                "Placeholder '{{{name}}}' in {attr_name} references unknown param \
                 on route '{route_path}'. Available params: {available}",
                available = if available_params.is_empty() {
                    "(none)".to_string()
                } else {
                    let mut sorted: Vec<&str> =
                        available_params.iter().map(|s| s.as_str()).collect();
                    sorted.sort_unstable();
                    sorted.join(", ")
                }
            )));
        }
    }
    Ok(())
}

/// Build a `WebUiFragmentRoute` from parsed attributes.
pub(crate) fn build_route_fragment(
    attrs: &RouteAttributes,
    fragment_id: String,
    children: Vec<WebUiFragmentRoute>,
) -> WebUiFragmentRoute {
    WebUiFragmentRoute {
        path: attrs.path.clone(),
        fragment_id,
        exact: attrs.exact,
        children,
        allowed_query: attrs.query.clone(),
        keep_alive: attrs.keep_alive,
        cache_tags: attrs.cache_tags.clone(),
        invalidates: attrs.invalidates.clone(),
        pending_component: attrs.pending_component.clone(),
        error_component: attrs.error_component.clone(),
    }
}

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

    #[test]
    fn test_extract_params_simple() {
        let params = extract_params("/profile/:id").unwrap();
        assert_eq!(params, vec!["id"]);
    }

    #[test]
    fn test_extract_params_multiple() {
        let params = extract_params("/profile/:id/view/:section").unwrap();
        assert_eq!(params, vec!["id", "section"]);
    }

    #[test]
    fn test_extract_params_optional() {
        let params = extract_params("/profile/:id?").unwrap();
        assert_eq!(params, vec!["id"]);
    }

    #[test]
    fn test_extract_params_splat() {
        let params = extract_params("/files/*rest").unwrap();
        assert_eq!(params, vec!["rest"]);
    }

    #[test]
    fn test_extract_params_splat_unnamed() {
        let params = extract_params("/files/*").unwrap();
        assert_eq!(params, vec!["rest"]);
    }

    #[test]
    fn test_extract_params_empty_path() {
        let params = extract_params("/").unwrap();
        assert!(params.is_empty());
    }

    #[test]
    fn test_extract_params_no_params() {
        let params = extract_params("/contacts/add").unwrap();
        assert!(params.is_empty());
    }

    #[test]
    fn test_extract_params_duplicate_error() {
        let result = extract_params("/profile/:id/other/:id");
        assert!(result.is_err());
    }

    #[test]
    fn test_extract_params_invalid_name() {
        let result = extract_params("/profile/:id-with-dash");
        assert!(result.is_err());
    }

    #[test]
    fn test_extract_params_empty_name() {
        let result = extract_params("/profile/:");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_attributes_valid() {
        let attrs = RouteAttributes {
            path: "/profile".to_string(),
            component: "profile-page".to_string(),
            ..Default::default()
        };
        assert!(validate_attributes(&attrs).is_ok());
    }

    #[test]
    fn test_validate_missing_component() {
        let attrs = RouteAttributes {
            path: "/page".to_string(),
            ..Default::default()
        };
        assert!(validate_attributes(&attrs).is_err());
    }

    // ── Relative path tests ──

    #[test]
    fn test_extract_params_relative_path() {
        let params = extract_params("./sections/:id").unwrap();
        assert_eq!(params, vec!["id"]);
    }

    #[test]
    fn test_extract_params_relative_multiple() {
        let params = extract_params("./topics/:topicId/lessons/:lessonId").unwrap();
        assert_eq!(params, vec!["topicId", "lessonId"]);
    }

    #[test]
    fn test_extract_params_relative_splat() {
        let params = extract_params("./*rest").unwrap();
        assert_eq!(params, vec!["rest"]);
    }

    // ── Cache tag parsing tests ──

    #[test]
    fn test_parse_tag_list_simple() {
        let tags = parse_tag_list("inbox,counts");
        assert_eq!(tags, vec!["inbox", "counts"]);
    }

    #[test]
    fn test_parse_tag_list_with_placeholders() {
        let tags = parse_tag_list("thread:{threadId},inbox");
        assert_eq!(tags, vec!["thread:{threadId}", "inbox"]);
    }

    #[test]
    fn test_parse_tag_list_whitespace() {
        let tags = parse_tag_list(" inbox , counts , drafts ");
        assert_eq!(tags, vec!["inbox", "counts", "drafts"]);
    }

    #[test]
    fn test_parse_tag_list_empty() {
        let tags = parse_tag_list("");
        assert!(tags.is_empty());
    }

    #[test]
    fn test_parse_tag_list_trailing_comma() {
        let tags = parse_tag_list("inbox,");
        assert_eq!(tags, vec!["inbox"]);
    }

    #[test]
    fn test_extract_tag_placeholders_simple() {
        let tags = vec!["thread:{threadId}".to_string(), "inbox".to_string()];
        let placeholders = extract_tag_placeholders(&tags).unwrap();
        assert_eq!(placeholders.len(), 1);
        assert!(placeholders.contains("threadId"));
    }

    #[test]
    fn test_extract_tag_placeholders_multiple() {
        let tags = vec!["folder:{folderId}".to_string(), "user:{userId}".to_string()];
        let placeholders = extract_tag_placeholders(&tags).unwrap();
        assert_eq!(placeholders.len(), 2);
        assert!(placeholders.contains("folderId"));
        assert!(placeholders.contains("userId"));
    }

    #[test]
    fn test_extract_tag_placeholders_none() {
        let tags = vec!["inbox".to_string(), "counts".to_string()];
        let placeholders = extract_tag_placeholders(&tags).unwrap();
        assert!(placeholders.is_empty());
    }

    #[test]
    fn test_extract_tag_placeholders_unclosed_brace() {
        let tags = vec!["thread:{threadId".to_string()];
        assert!(extract_tag_placeholders(&tags).is_err());
    }

    #[test]
    fn test_extract_tag_placeholders_empty_placeholder() {
        let tags = vec!["thread:{}".to_string()];
        assert!(extract_tag_placeholders(&tags).is_err());
    }

    #[test]
    fn test_extract_tag_placeholders_invalid_name() {
        let tags = vec!["thread:{thread-id}".to_string()];
        assert!(extract_tag_placeholders(&tags).is_err());
    }

    #[test]
    fn test_validate_tag_placeholders_valid() {
        let tags = vec!["thread:{threadId}".to_string(), "inbox".to_string()];
        let mut params = HashSet::new();
        params.insert("threadId".to_string());
        assert!(
            validate_tag_placeholders(&tags, &params, "cache-tags", "/email/:threadId").is_ok()
        );
    }

    #[test]
    fn test_validate_tag_placeholders_missing_param() {
        let tags = vec!["thread:{threadId}".to_string()];
        let params = HashSet::new();
        let result = validate_tag_placeholders(&tags, &params, "cache-tags", "/email");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("threadId"),
            "Error should mention the param: {err}"
        );
    }

    #[test]
    fn test_validate_tag_placeholders_with_ancestor_params() {
        let tags = vec![
            "thread:{threadId}".to_string(),
            "folder:{folderId}".to_string(),
        ];
        let mut params = HashSet::new();
        params.insert("threadId".to_string());
        params.insert("folderId".to_string());
        assert!(
            validate_tag_placeholders(&tags, &params, "cache-tags", "/email/:threadId").is_ok()
        );
    }
}