lex-extension-host 0.17.1

Runtime for the Lex extension system: registry, transports, trust gate, sandboxing
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
//! URI parsing for namespace declarations.
//!
//! The accepted forms (proposal §4.2):
//!
//! - `path:<body>` — local path. `body` is everything after the
//!   colon. No `#` or `?` allowed.
//! - `<scheme>:<body>[#rev][?subdir=…]` — github/gitlab tap form.
//!   Examples: `github:acme/lex-labels`, `gitlab:foo/bar#v2.1.0`,
//!   `github:acme/lex-labels?subdir=v2`.
//! - `<scheme>://<authority><path>[#rev][?subdir=…]` — https /
//!   git+ssh. Examples: `https://example.com/labels.tar.gz#v1`,
//!   `git+ssh://git@host/path.git#main`.
//!
//! The parser doesn't validate scheme-specific shapes (it doesn't
//! check that github's `body` is `owner/repo`, doesn't check that
//! `https://` URLs have a hostname). Those validations live in the
//! per-scheme fetcher, which knows what its scheme actually needs.
//! What this parser guarantees is the syntactic split into
//! `(scheme, body, rev, subdir)` plus the few cross-cutting rules
//! the host needs to enforce upstream of dispatch.
//!
//! The recognised query keys are `subdir` and `via`. Multiple keys
//! may appear separated by `&` (e.g. `?subdir=labels&via=git`). Other
//! keys are a parse error. Multiple `?` is a parse error. Multiple
//! `#` is a parse error. These keep silently-swallowed user mistakes
//! out of the contract.
//!
//! The `via` value is opaque to the parser — templates in
//! [`super::template`] interpret it (`"https"` vs `"git"`), keeping
//! the parser layer free of forge-policy concerns.

/// Parsed URI components.
///
/// Constructed via [`ParsedUri::parse`]. The fields are public so
/// fetchers can read them directly — there's no useful invariant to
/// preserve via accessors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedUri {
    /// The original input string (for diagnostics + canonical hash).
    pub original: String,
    /// The scheme — `"path"`, `"github"`, `"gitlab"`, `"https"`,
    /// `"git+ssh"`. Lowercase.
    pub scheme: String,
    /// Everything between `<scheme>:` and the first `#` or `?`.
    /// Includes the `//<authority>` portion for `https:` / `git+ssh:`
    /// — fetchers split that out themselves.
    pub body: String,
    /// The `#rev` fragment, if any. Empty fragment (`uri#`) parses
    /// to `Some("")` — caller's choice whether to treat that as an
    /// error.
    pub rev: Option<String>,
    /// The `?subdir=…` query value, if any. Empty value parses to
    /// `Some("")`.
    pub subdir: Option<String>,
    /// The `?via=…` query value, if any. Empty value parses to
    /// `Some("")`. The parser does not validate the value — templates
    /// in [`super::template`] interpret `"https"` vs `"git"`; other
    /// transports treat a `via` carried into them as a no-op.
    pub via: Option<String>,
}

/// Errors from [`ParsedUri::parse`].
#[derive(Debug)]
#[non_exhaustive]
pub enum UriParseError {
    /// No `:` separator — can't extract a scheme.
    NoScheme,
    /// Empty scheme (leading `:`).
    EmptyScheme,
    /// Multiple `#` fragments.
    MultipleFragments,
    /// Multiple `?` queries.
    MultipleQueries,
    /// `?` before `#` — fragment must come before query in our
    /// canonicalisation (matches lex-config's `canonical_uri` output).
    QueryBeforeFragment,
    /// Query string contains a key other than `subdir` / `via`. We
    /// intentionally don't accept arbitrary query strings — anything
    /// else is a typo or an unsupported feature.
    UnknownQueryKey { key: String },
    /// A query key (`subdir` or `via`) appeared twice in the same
    /// query string. We reject this so a config-file user doesn't
    /// silently get the second value of an accidental duplicate.
    DuplicateQueryKey { key: String },
    /// Query parameter without a value (`?subdir` instead of
    /// `?subdir=…`).
    QueryParamMissingValue,
    /// `via=<value>` carried a value the forge template doesn't
    /// recognise. Templates accept `via=https` (the default) and
    /// `via=git` (route through the git transport); any other value is
    /// reported here. This variant is raised by per-template logic in
    /// [`super::template`], not the parser itself — the parser is
    /// content-agnostic about the `via` value, since each template
    /// decides which transports it can route through.
    UnsupportedVia { value: String },
}

impl std::fmt::Display for UriParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UriParseError::NoScheme => write!(f, "missing scheme (expected `<scheme>:<body>`)"),
            UriParseError::EmptyScheme => write!(f, "scheme is empty"),
            UriParseError::MultipleFragments => write!(f, "multiple `#` fragments"),
            UriParseError::MultipleQueries => write!(f, "multiple `?` query strings"),
            UriParseError::QueryBeforeFragment => write!(
                f,
                "`?` appears before `#` — `#rev` must come first, then `?subdir=…`"
            ),
            UriParseError::UnknownQueryKey { key } => write!(
                f,
                "unknown query parameter `{key}` (recognised keys: `subdir`, `via`)"
            ),
            UriParseError::DuplicateQueryKey { key } => {
                write!(f, "query parameter `{key}` appears more than once")
            }
            UriParseError::QueryParamMissingValue => write!(f, "query parameter has no `=` value"),
            UriParseError::UnsupportedVia { value } => write!(
                f,
                "unsupported `via={value}` (recognised values: `https`, `git`)"
            ),
        }
    }
}

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

impl ParsedUri {
    /// Parse a namespace URI string. See module docs for the accepted
    /// forms.
    pub fn parse(input: &str) -> Result<Self, UriParseError> {
        let colon = input.find(':').ok_or(UriParseError::NoScheme)?;
        if colon == 0 {
            return Err(UriParseError::EmptyScheme);
        }
        let scheme = input[..colon].to_ascii_lowercase();
        let rest = &input[colon + 1..];

        // Extract fragment + query. Fragment must come before query
        // when both are present — `<body>#<rev>?subdir=<v>`. This
        // matches `lex-config::NamespaceSpec::canonical_uri` output,
        // which is the only producer of URI strings the resolver
        // receives.
        let hash = rest.find('#');
        let question = rest.find('?');

        if let (Some(h), Some(q)) = (hash, question) {
            if q < h {
                return Err(UriParseError::QueryBeforeFragment);
            }
        }

        // Body is everything up to the first `#` or `?`.
        let body_end = match (hash, question) {
            (Some(h), Some(q)) => h.min(q),
            (Some(h), None) => h,
            (None, Some(q)) => q,
            (None, None) => rest.len(),
        };
        let body = rest[..body_end].to_string();

        // Fragment is between `#` and the next `?` (or end).
        let rev = if let Some(h) = hash {
            // Reject `<body>#<a>#<b>` — multiple fragments.
            let after_hash = &rest[h + 1..];
            if after_hash.contains('#') {
                return Err(UriParseError::MultipleFragments);
            }
            let frag_end = after_hash.find('?').unwrap_or(after_hash.len());
            Some(after_hash[..frag_end].to_string())
        } else {
            None
        };

        // Query is everything after the first `?` (which must be
        // after the `#` if present). Multi-`?` is an error.
        let (subdir, via) = if let Some(q) = question {
            let after_q = &rest[q + 1..];
            if after_q.contains('?') {
                return Err(UriParseError::MultipleQueries);
            }
            parse_query(after_q)?
        } else {
            (None, None)
        };

        Ok(Self {
            original: input.to_string(),
            scheme,
            body,
            rev,
            subdir,
            via,
        })
    }

    /// True when this URI carried a `#` fragment. Distinct from
    /// `rev.is_some()` only in that it would still be true for an
    /// empty `#` (i.e. `uri#`). The path-scheme handler uses this to
    /// reject `path:dir#` even when the rev is empty.
    pub fn has_fragment(&self) -> bool {
        self.rev.is_some()
    }

    /// True when this URI carried a `?` query. Same shape as
    /// [`Self::has_fragment`] — true regardless of which recognised
    /// key (`subdir` or `via`) appeared, and for the empty-query
    /// marker (`uri?` → `subdir = Some("")`).
    pub fn has_query(&self) -> bool {
        self.subdir.is_some() || self.via.is_some()
    }
}

/// Parse the query string after `?`. Recognised keys are
/// `subdir=<value>` and `via=<value>`; multi-key queries separate
/// pairs with `&`. Any other key is a parse error.
///
/// To check whether a URI carried a `?` (even an empty one), callers
/// should use [`ParsedUri::has_query`] — it is the authoritative
/// "URI had a query string" check across both slots.
///
/// The `subdir` slot is `Some(String)` when `subdir=<value>` is
/// present in the query, OR when the query string is empty
/// (`uri?` → `Some("")`). The empty-query sentinel lives here because
/// the symmetry with the fragment parser — where `uri#` yields
/// `Some("")` not `None` — is load-bearing: callers (notably the
/// `path:` resolver) used to inspect `subdir.is_some()` directly to
/// decide whether the remote-only `?` knob was used, and the sentinel
/// keeps `has_query()` correctly identifying the empty-`?` case even
/// when no recognised keys appear.
///
/// The `via` slot is `Some(String)` only when `via=<value>` is
/// explicitly present in the query string. It does not carry the
/// empty-query sentinel — that's `subdir`'s job alone.
fn parse_query(q: &str) -> Result<(Option<String>, Option<String>), UriParseError> {
    if q.is_empty() {
        // `uri?` with empty query string — represent as
        // `subdir = Some("")` so `has_query()` correctly reports
        // "URI carried a `?`". `via` stays `None` (it isn't part of
        // the syntactic-presence contract).
        return Ok((Some(String::new()), None));
    }
    let mut subdir = None;
    let mut via = None;
    for pair in q.split('&') {
        let Some((key, value)) = pair.split_once('=') else {
            return Err(UriParseError::QueryParamMissingValue);
        };
        match key {
            "subdir" => {
                if subdir.is_some() {
                    return Err(UriParseError::DuplicateQueryKey {
                        key: key.to_string(),
                    });
                }
                subdir = Some(value.to_string());
            }
            "via" => {
                if via.is_some() {
                    return Err(UriParseError::DuplicateQueryKey {
                        key: key.to_string(),
                    });
                }
                via = Some(value.to_string());
            }
            _ => {
                return Err(UriParseError::UnknownQueryKey {
                    key: key.to_string(),
                });
            }
        }
    }
    Ok((subdir, via))
}

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

    #[test]
    fn parses_path_uri() {
        let p = ParsedUri::parse("path:acme-labels").unwrap();
        assert_eq!(p.scheme, "path");
        assert_eq!(p.body, "acme-labels");
        assert_eq!(p.rev, None);
        assert_eq!(p.subdir, None);
    }

    #[test]
    fn parses_github_with_rev_and_subdir() {
        let p = ParsedUri::parse("github:acme/repo#v1.2.0?subdir=labels").unwrap();
        assert_eq!(p.scheme, "github");
        assert_eq!(p.body, "acme/repo");
        assert_eq!(p.rev.as_deref(), Some("v1.2.0"));
        assert_eq!(p.subdir.as_deref(), Some("labels"));
    }

    #[test]
    fn parses_https_with_authority() {
        let p = ParsedUri::parse("https://example.com/path.tar.gz#main").unwrap();
        assert_eq!(p.scheme, "https");
        assert_eq!(p.body, "//example.com/path.tar.gz");
        assert_eq!(p.rev.as_deref(), Some("main"));
    }

    #[test]
    fn parses_git_ssh_uri() {
        let p = ParsedUri::parse("git+ssh://git@host/path.git#v1").unwrap();
        assert_eq!(p.scheme, "git+ssh");
        assert_eq!(p.body, "//git@host/path.git");
        assert_eq!(p.rev.as_deref(), Some("v1"));
    }

    #[test]
    fn lowercases_scheme() {
        let p = ParsedUri::parse("GITHUB:acme/repo").unwrap();
        assert_eq!(p.scheme, "github");
    }

    #[test]
    fn rejects_no_scheme() {
        let err = ParsedUri::parse("acme-labels").unwrap_err();
        assert!(matches!(err, UriParseError::NoScheme));
    }

    #[test]
    fn rejects_empty_scheme() {
        let err = ParsedUri::parse(":acme/repo").unwrap_err();
        assert!(matches!(err, UriParseError::EmptyScheme));
    }

    #[test]
    fn rejects_multiple_fragments() {
        let err = ParsedUri::parse("github:acme/repo#a#b").unwrap_err();
        assert!(matches!(err, UriParseError::MultipleFragments));
    }

    #[test]
    fn rejects_query_before_fragment() {
        let err = ParsedUri::parse("github:acme/repo?subdir=x#rev").unwrap_err();
        assert!(matches!(err, UriParseError::QueryBeforeFragment));
    }

    #[test]
    fn rejects_unknown_query_key() {
        let err = ParsedUri::parse("github:acme/repo?otherkey=x").unwrap_err();
        assert!(matches!(err, UriParseError::UnknownQueryKey { .. }));
    }

    #[test]
    fn rejects_query_without_value() {
        let err = ParsedUri::parse("github:acme/repo?subdir").unwrap_err();
        assert!(matches!(err, UriParseError::QueryParamMissingValue));
    }

    #[test]
    fn accepts_subdir_and_via_together() {
        let p = ParsedUri::parse("github:acme/repo#v1?subdir=labels&via=git").unwrap();
        assert_eq!(p.rev.as_deref(), Some("v1"));
        assert_eq!(p.subdir.as_deref(), Some("labels"));
        assert_eq!(p.via.as_deref(), Some("git"));
    }

    #[test]
    fn accepts_via_alone() {
        let p = ParsedUri::parse("github:acme/repo?via=git").unwrap();
        assert_eq!(p.via.as_deref(), Some("git"));
        assert_eq!(p.subdir, None);
        // `?via=git` is a recognised key, not the empty-query syntactic
        // marker — so `has_query()` is `true` (URI carried a `?`) but
        // subdir didn't get the empty-string sentinel.
        assert!(p.has_query());
    }

    #[test]
    fn rejects_multi_query_with_unknown_key() {
        let err = ParsedUri::parse("github:acme/repo#v1?subdir=a&otherkey=b").unwrap_err();
        assert!(matches!(err, UriParseError::UnknownQueryKey { key } if key == "otherkey"));
    }

    #[test]
    fn rejects_duplicate_subdir_query_key() {
        let err = ParsedUri::parse("github:acme/repo?subdir=a&subdir=b").unwrap_err();
        assert!(matches!(err, UriParseError::DuplicateQueryKey { key } if key == "subdir"));
    }

    #[test]
    fn rejects_duplicate_via_query_key() {
        let err = ParsedUri::parse("github:acme/repo?via=git&via=https").unwrap_err();
        assert!(matches!(err, UriParseError::DuplicateQueryKey { key } if key == "via"));
    }

    #[test]
    fn empty_query_after_question_is_some_empty_not_none() {
        // `uri#rev?` — the trailing `?` parses to `Some("")` so
        // `has_query()` correctly reports the syntactic presence of
        // the query separator. The `path:` resolver relies on this
        // to reject `path:dir?` (see
        // `path_dir_with_empty_question_is_rejected_by_path_resolver`).
        let p = ParsedUri::parse("github:acme/repo#v1?").unwrap();
        assert_eq!(p.rev.as_deref(), Some("v1"));
        assert_eq!(p.subdir.as_deref(), Some(""));
        assert!(p.has_query());
    }

    #[test]
    fn fragment_without_rev_value_is_some_empty() {
        // `uri#` with empty fragment — parses to Some(""). The
        // path-scheme handler treats this as "URI has fragment" and
        // rejects appropriately; remote schemes will too if the
        // empty rev confuses them.
        let p = ParsedUri::parse("github:acme/repo#").unwrap();
        assert_eq!(p.rev.as_deref(), Some(""));
        assert!(p.has_fragment());
    }
}