nix-uri 0.2.0

Parse and manipulate the nix-uri scheme to and from flakerefs.
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use std::fmt::Display;

use serde::{Deserialize, Serialize};
use winnow::{
    ModalResult, Parser,
    combinator::{alt, cut_err, opt, preceded, separated_pair, terminated},
    error::{ContextError, ErrMode, StrContext, StrContextValue},
    token::take_till,
};

use crate::{
    error::{NixUriError, NixUriResult, tag},
    flakeref::{
        RefLocation,
        validators::{looks_like_rev, validate_ref_name},
    },
};

/// Which git-forge scheme a `GitForge` reference uses. Spelled in the URL as
/// the leading `github:`, `gitlab:`, or `sourcehut:` token; also drives the
/// canonical-domain lookup in [`super::ForgeIdentity`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub enum GitForgePlatform {
    GitHub,
    GitLab,
    SourceHut,
}

/// A reference into a git forge (`github:`, `gitlab:`, `sourcehut:`).
///
/// Ref and rev are stored as separate typed slots; the parser splits a
/// path-component value (`github:owner/repo/<x>`) into `rev` if `<x>` is
/// 40-hex, otherwise into `ref_`. The
/// `location` field records where the value would be rendered on `Display`,
/// so round-trips preserve `?ref=` vs `/ref` form. The "no ref and no rev"
/// state is encoded by both fields being `None`; `location` still defaults
/// to `PathComponent` for that case.
///
/// `#[non_exhaustive]` reserves room for future fields (e.g. `host`,
/// `submodules`) to land here without breaking match arms downstream.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct GitForge {
    pub platform: GitForgePlatform,
    pub owner: String,
    pub repo: String,
    pub ref_: Option<String>,
    pub rev: Option<String>,
    pub location: RefLocation,
}

impl GitForgePlatform {
    /// Parses the gitforge platform token: `<github|gitlab|sourcehut>`.
    #[allow(dead_code)]
    pub(crate) fn parse(input: &mut &str) -> ModalResult<Self> {
        alt((
            tag("github").value(Self::GitHub),
            tag("gitlab").value(Self::GitLab),
            tag("sourcehut").value(Self::SourceHut),
        ))
        .parse_next(input)
    }
    #[allow(dead_code)]
    pub(crate) fn parse_terminated(input: &mut &str) -> ModalResult<Self> {
        terminated(
            Self::parse,
            ':'.context(StrContext::Expected(StrContextValue::CharLiteral(':'))),
        )
        .parse_next(input)
    }
}

impl GitForge {
    /// `<owner>/<repo>[/?#]`
    fn parse_owner_repo<'i>(input: &mut &'i str) -> ModalResult<(&'i str, &'i str)> {
        cut_err(separated_pair(
            take_till(1.., |c: char| c == '/')
                .context(StrContext::Label("TakeTill1"))
                .context(StrContext::Label("owner")),
            '/'.context(StrContext::Expected(StrContextValue::CharLiteral('/'))),
            take_till(1.., |c: char| c == '/' || c == '?' || c == '#')
                .context(StrContext::Label("TakeTill1"))
                .context(StrContext::Label("repo")),
        ))
        .context(StrContext::Label("owner and repo"))
        .parse_next(input)
    }

    /// `/[foobar]<?#>...` -> `Option<foobar>`; consumes the leading `/` and
    /// optionally the trailing token before `?` / `#`.
    fn parse_rev_ref<'i>(input: &mut &'i str) -> ModalResult<Option<&'i str>> {
        preceded(
            '/'.context(StrContext::Expected(StrContextValue::CharLiteral('/'))),
            opt(take_till(1.., |c: char| c == '?' || c == '#')
                .context(StrContext::Label("TakeTill1"))),
        )
        .parse_next(input)
    }

    /// `<owner>/<repo>[/[value]] -> (owner, repo, value)`. The trailing
    /// `value`, when present, is classified by the caller into either
    /// `ref_` or `rev` via [`super::validators::looks_like_rev`].
    #[allow(dead_code)]
    pub(crate) fn parse_owner_repo_ref<'i>(
        input: &mut &'i str,
    ) -> ModalResult<(&'i str, &'i str, Option<&'i str>)> {
        let (owner, repo) = Self::parse_owner_repo(input)?;
        let maybe_refrev = opt(Self::parse_rev_ref).parse_next(input)?;
        Ok((owner, repo, maybe_refrev.flatten()))
    }

    #[allow(dead_code)]
    pub(crate) fn parse(input: &mut &str) -> ModalResult<Self> {
        let platform = terminated(
            GitForgePlatform::parse,
            ':'.context(StrContext::Expected(StrContextValue::CharLiteral(':'))),
        )
        .parse_next(input)?;
        let (owner, repo, maybe_value) = Self::parse_owner_repo_ref(input)?;
        let (ref_, rev) = match maybe_value {
            Some(v) if looks_like_rev(v) => (None, Some(v.to_string())),
            Some(v) if validate_ref_name(v) => (Some(v.to_string()), None),
            Some(_) => return Err(ErrMode::Cut(ContextError::new())),
            None => (None, None),
        };
        Ok(Self {
            platform,
            owner: owner.to_string(),
            repo: repo.to_string(),
            ref_,
            rev,
            location: RefLocation::PathComponent,
        })
    }
}

/// `[a-zA-Z0-9._-]` is the strictest common alphabet across `github:`,
/// `gitlab:`, and `sourcehut:`. `SourceHut` additionally permits a leading
/// `~` on owner (e.g. `~misterio/nix-colors`); that is the only platform-
/// specific carve-out.
fn is_owner_repo_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')
}

/// Reject owner/repo strings that upstream Nix would accept syntactically
/// but that no real forge would resolve. Lets parse-time errors stand in
/// for the fetch-time failure a downstream consumer would otherwise see.
///
/// `owner` and `repo` are the post-percent-decode values the parser pulled
/// out of the URL path; see [`super::fr_type::FlakeRefType::parse_type`]'s
/// `GitForge` arm. A `gitlab:` owner is allowed to carry decoded `/` so
/// nested-subgroup forms like `gitlab:veloren/dev/rfcs` (encoded
/// `gitlab:veloren%2Fdev/rfcs` on the wire) round-trip; GitHub and
/// `SourceHut` do not have a subgroup concept upstream, so the same input
/// still rejects there.
///
/// The `field` name is `"owner"` or `"repo"` so consumers can pattern-match
/// on which segment failed.
pub(crate) fn validate_owner_repo(
    platform: &GitForgePlatform,
    owner: &str,
    repo: &str,
) -> NixUriResult<()> {
    let owner_body = if matches!(platform, GitForgePlatform::SourceHut) {
        owner.strip_prefix('~').unwrap_or(owner)
    } else {
        owner
    };
    if owner_body.is_empty() {
        return Err(NixUriError::InvalidValue {
            field: "owner",
            reason: "owner must not be empty".to_string(),
        });
    }
    if owner_body.contains('/') {
        if !matches!(platform, GitForgePlatform::GitLab) {
            return Err(NixUriError::InvalidValue {
                field: "owner",
                reason: "only gitlab owners may contain a '/' (subgroup form)".to_string(),
            });
        }
        for segment in owner_body.split('/') {
            if segment.is_empty() {
                return Err(NixUriError::InvalidValue {
                    field: "owner",
                    reason: "subgroup owner must not have empty segments or leading/trailing '/'"
                        .to_string(),
                });
            }
            let first = segment.chars().next().unwrap();
            if first == '-' || first == '.' {
                return Err(NixUriError::InvalidValue {
                    field: "owner",
                    reason: "owner segment must not start with '-' or '.'".to_string(),
                });
            }
            if !segment.chars().all(is_owner_repo_char) {
                return Err(NixUriError::InvalidValue {
                    field: "owner",
                    reason: "owner segment contains a character outside [a-zA-Z0-9._-]".to_string(),
                });
            }
        }
    } else {
        let owner_first = owner_body.chars().next().unwrap();
        if owner_first == '-' || owner_first == '.' {
            return Err(NixUriError::InvalidValue {
                field: "owner",
                reason: "owner must not start with '-' or '.'".to_string(),
            });
        }
        if !owner_body.chars().all(is_owner_repo_char) {
            return Err(NixUriError::InvalidValue {
                field: "owner",
                reason: "owner contains a character outside [a-zA-Z0-9._-]".to_string(),
            });
        }
    }
    if repo.is_empty() {
        return Err(NixUriError::InvalidValue {
            field: "repo",
            reason: "repo must not be empty".to_string(),
        });
    }
    if repo.starts_with('.') {
        return Err(NixUriError::InvalidValue {
            field: "repo",
            reason: "repo must not start with '.'".to_string(),
        });
    }
    if !repo.chars().all(is_owner_repo_char) {
        return Err(NixUriError::InvalidValue {
            field: "repo",
            reason: "repo contains a character outside [a-zA-Z0-9._-]".to_string(),
        });
    }
    Ok(())
}

impl Display for GitForgePlatform {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::GitHub => "github",
                Self::GitLab => "gitlab",
                Self::SourceHut => "sourcehut",
            }
        )
    }
}

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

    #[test]
    fn platform() {
        let remain = ":nixos/nixpkgs";

        let uri = "github:nixos/nixpkgs";

        let (rest, platform) = GitForgePlatform::parse.parse_peek(uri).unwrap();
        assert_eq!(rest, remain);
        assert_eq!(platform, GitForgePlatform::GitHub);

        let (rest, platform) = GitForgePlatform::parse_terminated.parse_peek(uri).unwrap();
        assert_eq!(rest, &remain[1..]);
        assert_eq!(platform, GitForgePlatform::GitHub);

        let uri = "gitlab:nixos/nixpkgs";

        let (rest, platform) = GitForgePlatform::parse.parse_peek(uri).unwrap();
        assert_eq!(rest, remain);
        assert_eq!(platform, GitForgePlatform::GitLab);

        let uri = "sourcehut:nixos/nixpkgs";

        let (rest, platform) = GitForgePlatform::parse.parse_peek(uri).unwrap();
        assert_eq!(rest, remain);
        assert_eq!(platform, GitForgePlatform::SourceHut);
        // TODO?: fuzz test where `:` is preceded by bad string
    }
}

#[cfg(test)]
mod err_msgs {
    use cool_asserts::assert_matches;

    #[test]
    fn just_owner_public_surface() {
        use crate::{NixUriError, ParseExpected, parser::parse_nix_uri};

        assert_matches!(
            parse_nix_uri("github:owner"),
            Err(NixUriError::Parse {
                position: 12,
                expected: ParseExpected::Char('/'),
            })
        );
    }

    #[test]
    fn whitespace_in_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:bad owner/repo"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn whitespace_in_repo_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:owner/bad repo"),
            Err(NixUriError::InvalidValue { field: "repo", .. })
        );
    }

    #[test]
    fn leading_dot_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:.dotted/repo"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn leading_dash_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:-dashed/repo"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn leading_dot_repo_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:owner/.dotrepo"),
            Err(NixUriError::InvalidValue { field: "repo", .. })
        );
    }

    #[test]
    fn special_char_in_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:bad!owner/repo"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn tilde_in_github_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:~tilde/repo"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn tilde_only_in_sourcehut_owner() {
        use crate::parser::parse_nix_uri;
        // SourceHut owner permits a leading `~` (e.g. `~misterio`).
        parse_nix_uri("sourcehut:~owner/repo").expect("sourcehut owner with `~` is valid");
        // GitLab and GitHub do not.
        assert!(parse_nix_uri("gitlab:~owner/repo").is_err());
        assert!(parse_nix_uri("github:~owner/repo").is_err());
    }

    #[test]
    fn valid_forms_still_accepted() {
        use crate::parser::parse_nix_uri;
        for uri in [
            "github:nixos/nixpkgs",
            "github:nix.os/nix-pkgs",
            "github:n_ix/r_epo",
            "github:o-1/r.2",
            "gitlab:owner/repo",
            "sourcehut:nixos/nixpkgs",
            "sourcehut:~misterio/nix-colors",
        ] {
            parse_nix_uri(uri).unwrap_or_else(|e| panic!("expected {uri:?} to parse, got {e}"));
        }
    }

    /// GitLab nested subgroups are written as
    /// `gitlab:veloren%2Fdev/rfcs` in Nix. The parser percent-decodes
    /// each path segment after splitting on the literal `/`, so `%2F`
    /// survives as a literal `/` inside the owner without colliding
    /// with the owner-vs-repo boundary.
    #[test]
    fn gitlab_subgroup_percent_decode() {
        use crate::{FlakeRef, FlakeRefType, GitForge, GitForgePlatform, parser::parse_nix_uri};
        let uri = "gitlab:veloren%2Fdev/rfcs";
        let parsed: FlakeRef = parse_nix_uri(uri).expect("subgroup form should parse");
        match parsed.kind() {
            FlakeRefType::GitForge(GitForge {
                platform: GitForgePlatform::GitLab,
                owner,
                repo,
                ..
            }) => {
                assert_eq!(owner, "veloren/dev");
                assert_eq!(repo, "rfcs");
            }
            other => panic!("expected GitLab GitForge, got {other:?}"),
        }
        // Display re-encodes the `/` in owner so the wire form matches the input
        // and the round-trip stays byte-stable.
        assert_eq!(parsed.to_string(), uri);
    }

    #[test]
    fn gitlab_deep_subgroup() {
        use crate::{FlakeRefType, parser::parse_nix_uri};
        let uri = "gitlab:o%2Fp%2Fq/r";
        let parsed = parse_nix_uri(uri).expect("deep subgroup should parse");
        match parsed.kind() {
            FlakeRefType::GitForge(g) => {
                assert_eq!(g.owner, "o/p/q");
                assert_eq!(g.repo, "r");
            }
            other => panic!("expected GitForge, got {other:?}"),
        }
        assert_eq!(parsed.to_string(), uri);
    }

    #[test]
    fn gitlab_subgroup_with_ref_query() {
        use crate::{FlakeRefType, parser::parse_nix_uri};
        let parsed =
            parse_nix_uri("gitlab:o%2Fp/r?ref=main").expect("ref-query subgroup should parse");
        match parsed.kind() {
            FlakeRefType::GitForge(g) => {
                assert_eq!(g.owner, "o/p");
                assert_eq!(g.repo, "r");
                assert_eq!(g.ref_.as_deref(), Some("main"));
            }
            other => panic!("expected GitForge, got {other:?}"),
        }
    }

    #[test]
    fn gitlab_subgroup_with_path_rev() {
        use crate::{FlakeRefType, parser::parse_nix_uri};
        let rev = "0123456789abcdef0123456789abcdef01234567";
        let parsed = parse_nix_uri(&format!("gitlab:o%2Fp/r/{rev}"))
            .expect("path-rev subgroup should parse");
        match parsed.kind() {
            FlakeRefType::GitForge(g) => {
                assert_eq!(g.owner, "o/p");
                assert_eq!(g.rev.as_deref(), Some(rev));
                assert_eq!(g.ref_, None);
            }
            other => panic!("expected GitForge, got {other:?}"),
        }
    }

    #[test]
    fn gitlab_leading_slash_in_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("gitlab:%2Fp/r"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn gitlab_trailing_slash_in_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("gitlab:p%2F/r"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn gitlab_empty_subgroup_segment_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("gitlab:p%2F%2Fq/r"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    /// GitHub has no subgroup concept and `validate_owner_repo` is the
    /// parse-time stand-in for the fetch-time failure a downstream
    /// consumer would otherwise see, so reject the subgroup form here
    /// even though Nix accepts it syntactically (Nix applies the same
    /// path-segment decode regardless of forge); the platform gate is a
    /// nix-uri-only safety.
    #[test]
    fn github_subgroup_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("github:o%2Fp/r"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }

    #[test]
    fn sourcehut_subgroup_owner_rejected() {
        use crate::{NixUriError, parser::parse_nix_uri};
        assert_matches!(
            parse_nix_uri("sourcehut:o%2Fp/r"),
            Err(NixUriError::InvalidValue { field: "owner", .. })
        );
    }
}

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

    #[test]
    fn plain() {
        let input = "owner/repo";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", None);
        assert_eq!(rest, "");
        assert_eq!(expected, res);
    }

    #[test]
    fn param_terminated() {
        let input = "owner/repo?🤡";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", None);
        assert_eq!(rest, "?🤡");
        assert_eq!(expected, res);
        assert_eq!(rest, "?🤡");

        let input = "owner/repo#🤡";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", None);
        assert_eq!(expected, res);
        assert_eq!(rest, "#🤡");

        let input = "owner/repo?#🤡";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", None);
        assert_eq!(expected, res);
        assert_eq!(rest, "?#🤡");
    }

    #[test]
    fn attr_terminated() {
        let input = "owner/repo#fizz.bar";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", None);
        assert_eq!(rest, "#fizz.bar");
        assert_eq!(expected, res);
    }

    #[test]
    fn rev_param_terminated() {
        let input = "owner/repo/rev?foo=bar";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", Some("rev"));
        assert_eq!(rest, "?foo=bar");
        assert_eq!(expected, res);
    }

    #[test]
    fn rev_attr_terminated() {
        let input = "owner/repo/rev#fizz.bar";
        let (rest, res) = GitForge::parse_owner_repo_ref.parse_peek(input).unwrap();
        let expected = ("owner", "repo", Some("rev"));
        assert_eq!(rest, "#fizz.bar");
        assert_eq!(expected, res);
    }
}