luaupm 0.1.0-beta.1

The Luau package manager: dependencies, tools and scripts for Luau and Roblox projects
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
pub mod edit;

use crate::error::Error;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

pub const MANIFEST_FILE: &str = "lpm.toml";

/// Key under [indices] used for dependencies that name no index.
pub const DEFAULT_INDEX_NAME: &str = "default";

#[derive(Serialize, Deserialize, Debug)]
pub struct Manifest {
    pub package: Package,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<Target>,
    #[serde(default, skip_serializing_if = "Config::is_default")]
    pub config: Config,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub indices: BTreeMap<String, String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub dependencies: BTreeMap<String, Dependency>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub tools: BTreeMap<String, Tool>,
    /// Shell commands runnable with `lpm run <name>`.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub scripts: BTreeMap<String, String>,
}

/// Per-environment install locations; each defaults to "packages/<env>".
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Config {
    #[serde(
        rename = "shared-packages-out",
        skip_serializing_if = "Option::is_none"
    )]
    pub shared_packages_out: Option<String>,
    #[serde(
        rename = "server-packages-out",
        skip_serializing_if = "Option::is_none"
    )]
    pub server_packages_out: Option<String>,
    #[serde(rename = "lune-packages-out", skip_serializing_if = "Option::is_none")]
    pub lune_packages_out: Option<String>,
    #[serde(rename = "luau-packages-out", skip_serializing_if = "Option::is_none")]
    pub luau_packages_out: Option<String>,
    #[serde(rename = "lute-packages-out", skip_serializing_if = "Option::is_none")]
    pub lute_packages_out: Option<String>,
}

impl Config {
    fn is_default(&self) -> bool {
        self.shared_packages_out.is_none()
            && self.server_packages_out.is_none()
            && self.lune_packages_out.is_none()
            && self.luau_packages_out.is_none()
            && self.lute_packages_out.is_none()
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Package {
    pub name: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub authors: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,
    /// Files or directories (not globs) that go into the published archive;
    /// everything else is skipped. Empty means "everything sensible".
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub include: Vec<String>,
    /// Paths dropped from the published archive after `include` applies.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exclude: Vec<String>,
}

impl Package {
    /// The `repository` field reduced to a GitHub "owner/repo" slug. Accepts
    /// a bare slug, https:// and host-only URLs, and the git@ ssh remote,
    /// with or without ".git". Other hosts give None.
    ///
    /// TODO(api): publish used this to pick the repo that hosted the release
    /// asset. Kept because the API will likely want to record where a package
    /// comes from; delete it if it doesn't.
    #[allow(dead_code)]
    pub fn repository_slug(&self) -> Option<String> {
        let repository = self.repository.as_deref()?.trim().trim_end_matches('/');

        let rest = if let Some(ssh) = repository.strip_prefix("git@github.com:") {
            ssh
        } else if let Some(after_scheme) = repository
            .strip_prefix("https://")
            .or_else(|| repository.strip_prefix("http://"))
        {
            after_scheme.strip_prefix("github.com/")?
        } else if let Some(hosted) = repository.strip_prefix("github.com/") {
            hosted
        } else {
            repository
        };
        let rest = rest.strip_suffix(".git").unwrap_or(rest);

        // Charset checks (GitHub's, roughly) keep host-shaped strings like
        // "gitlab.com/owner" or "git@host:owner" from passing as bare slugs.
        let (owner, repo) = rest.split_once('/')?;
        let slug_char = |c: char| c.is_ascii_alphanumeric() || c == '-' || c == '_';
        let owner_ok = !owner.is_empty() && owner.chars().all(slug_char);
        let repo_ok = !repo.is_empty() && repo.chars().all(|c| slug_char(c) || c == '.');
        if owner_ok && repo_ok {
            Some(format!("{owner}/{repo}"))
        } else {
            None
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Target {
    pub environment: Environment,
    /// Entry point of the package, e.g. "src/init.luau".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub main: Option<String>,
}

/// Where a package's Luau code runs. Output folders under packages/ use the
/// serialized (lowercase) form.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum Environment {
    /// Luau code that must be run in Roblox
    Shared,
    /// Roblox server-side code only
    Server,
    /// Requires the Lune runtime
    Lune,
    /// Standalone Luau, runnable with the luau CLI
    Luau,
    /// Requires the Lute runtime
    Lute,
}

impl Environment {
    pub const ALL: [Environment; 5] = [
        Environment::Shared,
        Environment::Server,
        Environment::Lune,
        Environment::Luau,
        Environment::Lute,
    ];

    pub fn dir_name(self) -> &'static str {
        match self {
            Environment::Shared => "shared",
            Environment::Server => "server",
            Environment::Lune => "lune",
            Environment::Luau => "luau",
            Environment::Lute => "lute",
        }
    }

    /// Translates a pesde `target.environment` value.
    pub fn from_pesde(environment: &str) -> Result<Self, Error> {
        match environment {
            "roblox" => Ok(Environment::Shared),
            "roblox_server" => Ok(Environment::Server),
            "lune" => Ok(Environment::Lune),
            "luau" => Ok(Environment::Luau),
            "lute" => Ok(Environment::Lute),
            other => Err(Error::UnsupportedEnvironment(other.to_string())),
        }
    }

    /// Translates a wally `realm` value.
    pub fn from_wally_realm(realm: &str) -> Result<Self, Error> {
        match realm {
            "shared" => Ok(Environment::Shared),
            "server" => Ok(Environment::Server),
            other => Err(Error::UnsupportedEnvironment(other.to_string())),
        }
    }

    /// Parses lpm's own environment names ("shared", "lune", ...).
    pub fn from_lpm(environment: &str) -> Result<Self, Error> {
        match environment {
            "shared" => Ok(Environment::Shared),
            "server" => Ok(Environment::Server),
            "lune" => Ok(Environment::Lune),
            "luau" => Ok(Environment::Luau),
            "lute" => Ok(Environment::Lute),
            other => Err(Error::UnsupportedEnvironment(other.to_string())),
        }
    }
}

impl fmt::Display for Environment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.dir_name())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Dependency {
    /// Package identifier in "scope/name" form.
    pub name: String,
    /// Semver requirement; "^" alone means "latest".
    pub version: String,
    /// Key into [indices]; None means the default luaupm index.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<String>,
}

/// A GitHub-released binary tool, written in lpm.toml as the single string
/// "owner/repo@version" under [tools] (key = alias).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tool {
    /// GitHub repository in "owner/repo" form.
    pub repository: String,
    /// Exact release version, without a leading 'v'.
    pub version: String,
}

impl Tool {
    /// Parses an "owner/repo@version" tool spec.
    pub fn parse(spec: &str) -> Result<Self, Error> {
        let invalid = || Error::InvalidToolSpec(spec.to_string());

        let (repository, version) = spec.trim().split_once('@').ok_or_else(invalid)?;
        if version.is_empty() || version.contains('@') {
            return Err(invalid());
        }
        Self::split_repository(repository).map_err(|_| invalid())?;

        Ok(Tool {
            repository: repository.to_string(),
            version: version.to_string(),
        })
    }

    /// Splits an "owner/repo" GitHub repository name. Unlike index package
    /// names (see `split_package_name`), GitHub owners and repos may contain
    /// uppercase letters and dots (e.g. "JohnnyMorganz/StyLua"), so only the
    /// shape is validated: exactly one '/', both halves non-empty.
    pub fn split_repository(repository: &str) -> Result<(&str, &str), Error> {
        match repository.split_once('/') {
            Some((owner, repo)) if !owner.is_empty() && !repo.is_empty() && !repo.contains('/') => {
                Ok((owner, repo))
            }
            _ => Err(Error::InvalidToolSpec(repository.to_string())),
        }
    }
}

impl fmt::Display for Tool {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}@{}", self.repository, self.version)
    }
}

impl Serialize for Tool {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for Tool {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let spec = String::deserialize(deserializer)?;
        Tool::parse(&spec).map_err(serde::de::Error::custom)
    }
}

impl Manifest {
    /// Loads the manifest from the current directory.
    pub fn load() -> Result<Self, Error> {
        Self::load_from(Path::new(MANIFEST_FILE))
    }

    pub fn load_from(path: &Path) -> Result<Self, Error> {
        if !path.exists() {
            return Err(Error::ManifestMissing);
        }
        Ok(toml::from_str(&std::fs::read_to_string(path)?)?)
    }

    /// Folder an environment's packages (and their link files) install to.
    pub fn packages_out(&self, environment: Environment) -> std::path::PathBuf {
        let configured = match environment {
            Environment::Shared => &self.config.shared_packages_out,
            Environment::Server => &self.config.server_packages_out,
            Environment::Lune => &self.config.lune_packages_out,
            Environment::Luau => &self.config.luau_packages_out,
            Environment::Lute => &self.config.lute_packages_out,
        };
        match configured {
            Some(dir) => std::path::PathBuf::from(dir),
            None => std::path::Path::new("packages").join(environment.dir_name()),
        }
    }

    /// Resolves a dependency's `index` key to an index URL.
    ///
    /// TODO(api): with lpm's own index gone, a dependency that names no index
    /// only works when the project defines a `default` one. This is where the
    /// lpm API becomes the fallback again.
    pub fn index_url(&self, index: Option<&str>) -> Result<&str, Error> {
        match index {
            None => self
                .indices
                .get(DEFAULT_INDEX_NAME)
                .map(String::as_str)
                .ok_or(Error::NoDefaultIndex),
            Some(key) => self
                .indices
                .get(key)
                .map(String::as_str)
                .ok_or_else(|| Error::UnknownIndex(key.to_string())),
        }
    }

    /// The command a `[scripts]` entry runs. Editing the manifest goes
    /// through `edit::ManifestDoc`; this is the read side, used by `lpm run`.
    pub fn script(&self, name: &str) -> Result<&str, Error> {
        self.scripts
            .get(name)
            .map(String::as_str)
            .ok_or_else(|| Error::ScriptMissing(name.to_string()))
    }
}

/// Splits a "scope/name" package identifier. Wally allows dashes, so parts
/// accept lowercase letters, digits, and dashes/underscores.
pub fn split_package_name(name: &str) -> Result<(&str, &str), Error> {
    let is_valid_part = |part: &str| {
        !part.is_empty()
            && part
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_')
    };

    match name.split('/').collect::<Vec<_>>().as_slice() {
        [scope, package] if is_valid_part(scope) && is_valid_part(package) => Ok((scope, package)),
        _ => Err(Error::InvalidPackageName(name.to_string())),
    }
}

/// Parses a manifest version requirement. A bare "^" (or "*") means "latest".
pub fn parse_version_req(req: &str) -> Result<semver::VersionReq, Error> {
    let trimmed = req.trim();
    if trimmed == "^" || trimmed == "*" || trimmed.is_empty() {
        Ok(semver::VersionReq::STAR)
    } else {
        Ok(semver::VersionReq::parse(trimmed)?)
    }
}

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

    #[test]
    fn parses_full_manifest() {
        let manifest: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"
            authors = ["Someone"]
            license = "MIT"

            [target]
            environment = "lune"
            main = "src/init.luau"

            [indices]
            wally = "https://github.com/UpliftGames/wally-index"

            [dependencies]
            Chief = { name = "chief/core", version = "^" }
            Other = { name = "user/other_package", version = "^", index = "wally" }

            [tools]
            stylua = "johnnymorganz/stylua@2.0.0"
            StyLua = "JohnnyMorganz/StyLua@2.1.0"

            [scripts]
            build = "rojo build -o game.rbxl"
            "#,
        )
        .unwrap();

        assert_eq!(manifest.package.name, "scope/name");
        assert_eq!(
            manifest.target.as_ref().unwrap().environment,
            Environment::Lune
        );
        assert_eq!(manifest.dependencies["Chief"].index, None);
        assert_eq!(
            manifest.dependencies["Other"].index.as_deref(),
            Some("wally")
        );
        assert_eq!(manifest.tools["stylua"].repository, "johnnymorganz/stylua");
        assert_eq!(manifest.tools["stylua"].version, "2.0.0");
        assert_eq!(manifest.tools["StyLua"].repository, "JohnnyMorganz/StyLua");
        assert_eq!(manifest.tools["StyLua"].version, "2.1.0");
        assert_eq!(manifest.script("build").unwrap(), "rojo build -o game.rbxl");
        assert!(matches!(
            manifest.script("test"),
            Err(Error::ScriptMissing(_))
        ));
    }

    #[test]
    fn resolves_index_urls() {
        let manifest: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"

            [indices]
            wally = "https://example.com/wally-index"
            "#,
        )
        .unwrap();

        // No `default` key and no built-in index left to fall back on.
        assert!(matches!(
            manifest.index_url(None),
            Err(Error::NoDefaultIndex)
        ));
        assert_eq!(
            manifest.index_url(Some("wally")).unwrap(),
            "https://example.com/wally-index"
        );
        assert!(matches!(
            manifest.index_url(Some("missing")),
            Err(Error::UnknownIndex(_))
        ));
    }

    #[test]
    fn default_index_key_resolves_bare_dependencies() {
        let manifest: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"

            [indices]
            default = "https://example.com/my-index"
            "#,
        )
        .unwrap();

        assert_eq!(
            manifest.index_url(None).unwrap(),
            "https://example.com/my-index"
        );
    }

    #[test]
    fn packages_out_defaults_and_overrides() {
        let manifest: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"

            [config]
            shared-packages-out = "src/ReplicatedStorage/Packages"
            "#,
        )
        .unwrap();

        assert_eq!(
            manifest.packages_out(Environment::Shared),
            std::path::PathBuf::from("src/ReplicatedStorage/Packages")
        );
        assert_eq!(
            manifest.packages_out(Environment::Luau),
            std::path::PathBuf::from("packages").join("luau")
        );
        assert_eq!(
            manifest.packages_out(Environment::Lute),
            std::path::PathBuf::from("packages").join("lute")
        );
    }

    #[test]
    fn translates_environments() {
        assert_eq!(
            Environment::from_pesde("roblox").unwrap(),
            Environment::Shared
        );
        assert_eq!(
            Environment::from_pesde("roblox_server").unwrap(),
            Environment::Server
        );
        assert_eq!(Environment::from_pesde("lune").unwrap(), Environment::Lune);
        assert!(Environment::from_pesde("nonsense").is_err());
        assert_eq!(
            Environment::from_wally_realm("shared").unwrap(),
            Environment::Shared
        );
        assert_eq!(
            Environment::from_wally_realm("server").unwrap(),
            Environment::Server
        );
        assert!(Environment::from_wally_realm("lune").is_err());
    }

    #[test]
    fn splits_package_names() {
        assert_eq!(
            split_package_name("evaera/promise").unwrap(),
            ("evaera", "promise")
        );
        assert_eq!(
            split_package_name("scope/pkg-name_2").unwrap(),
            ("scope", "pkg-name_2")
        );
        assert!(split_package_name("noslash").is_err());
        assert!(split_package_name("Upper/case").is_err());
        assert!(split_package_name("a/b/c").is_err());
    }

    #[test]
    fn parses_tool_specs() {
        let tool = Tool::parse("  JohnnyMorganz/StyLua@2.0.0  ").unwrap();
        assert_eq!(tool.repository, "JohnnyMorganz/StyLua");
        assert_eq!(tool.version, "2.0.0");
        assert_eq!(tool.to_string(), "JohnnyMorganz/StyLua@2.0.0");
    }

    #[test]
    fn rejects_invalid_tool_specs() {
        for spec in [
            "norepo@1.0",
            "owner/repo",
            "owner/repo@",
            "a/b/c@1.0",
            "@1.0",
            "owner/@1.0",
            "/repo@1.0",
            "owner/repo@1.0@2.0",
            "",
        ] {
            assert!(
                matches!(Tool::parse(spec), Err(Error::InvalidToolSpec(_))),
                "spec {spec:?} should be rejected"
            );
        }
    }

    #[test]
    fn tool_round_trips_through_toml() {
        #[derive(Serialize, Deserialize)]
        struct Tools {
            stylua: Tool,
        }

        let tools = Tools {
            stylua: Tool {
                repository: "JohnnyMorganz/StyLua".to_string(),
                version: "2.0.0".to_string(),
            },
        };
        let serialized = toml::to_string(&tools).unwrap();
        assert_eq!(
            serialized.trim(),
            r#"stylua = "JohnnyMorganz/StyLua@2.0.0""#
        );
        let parsed: Tools = toml::from_str(&serialized).unwrap();
        assert_eq!(parsed.stylua, tools.stylua);
    }

    fn package_with_repository(repository: Option<&str>) -> Package {
        Package {
            name: "scope/name".to_string(),
            version: "0.1.0".to_string(),
            description: None,
            authors: Vec::new(),
            repository: repository.map(str::to_string),
            license: None,
            include: Vec::new(),
            exclude: Vec::new(),
        }
    }

    #[test]
    fn repository_slug_normalizes_github_shapes() {
        for repository in [
            "owner/repo",
            "https://github.com/owner/repo",
            "https://github.com/owner/repo.git",
            "https://github.com/owner/repo/",
            "http://github.com/owner/repo",
            "github.com/owner/repo",
            "git@github.com:owner/repo",
            "git@github.com:owner/repo.git",
            "  owner/repo  ",
        ] {
            assert_eq!(
                package_with_repository(Some(repository)).repository_slug(),
                Some("owner/repo".to_string()),
                "repository {repository:?} should normalize"
            );
        }
        assert_eq!(
            package_with_repository(Some("JohnnyMorganz/StyLua.git"))
                .repository_slug()
                .as_deref(),
            Some("JohnnyMorganz/StyLua")
        );
    }

    #[test]
    fn repository_slug_rejects_everything_else() {
        for repository in [
            "https://gitlab.com/owner/repo",
            "gitlab.com/owner/repo",
            "git@gitlab.com:owner/repo",
            "https://github.com/owner",
            "https://github.com/owner/repo/tree/main",
            "owner",
            "owner/",
            "/repo",
            "a/b/c",
            "",
            "   ",
        ] {
            assert_eq!(
                package_with_repository(Some(repository)).repository_slug(),
                None,
                "repository {repository:?} should be rejected"
            );
        }
        assert_eq!(package_with_repository(None).repository_slug(), None);
    }

    #[test]
    fn include_and_exclude_round_trip() {
        let manifest: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"
            include = ["src", "lpm.toml", "README.md"]
            exclude = ["src/tests"]
            "#,
        )
        .unwrap();

        assert_eq!(manifest.package.include, ["src", "lpm.toml", "README.md"]);
        assert_eq!(manifest.package.exclude, ["src/tests"]);

        let serialized = toml::to_string(&manifest).unwrap();
        let parsed: Manifest = toml::from_str(&serialized).unwrap();
        assert_eq!(parsed.package.include, manifest.package.include);
        assert_eq!(parsed.package.exclude, manifest.package.exclude);

        // Absent lists stay absent on write.
        let bare: Manifest = toml::from_str(
            r#"
            [package]
            name = "scope/name"
            version = "0.1.0"
            "#,
        )
        .unwrap();
        assert!(bare.package.include.is_empty());
        let serialized = toml::to_string(&bare).unwrap();
        assert!(!serialized.contains("include"));
        assert!(!serialized.contains("exclude"));
    }

    #[test]
    fn parses_version_requirements() {
        let latest = parse_version_req("^").unwrap();
        assert!(latest.matches(&semver::Version::new(99, 0, 0)));
        let caret = parse_version_req("^1.2").unwrap();
        assert!(caret.matches(&semver::Version::new(1, 9, 0)));
        assert!(!caret.matches(&semver::Version::new(2, 0, 0)));
        assert!(parse_version_req("not a version").is_err());
    }
}