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
//! Core library of `cargo-component`.

#![deny(missing_docs)]

use anyhow::Context;
use semver::VersionReq;
use std::str::FromStr;
use warg_protocol::registry::PackageName;

pub mod command;
pub mod lock;
pub mod progress;
pub mod registry;
pub mod terminal;

/// Represents a versioned component package name.
#[derive(Clone)]
pub struct VersionedPackageName {
    /// The package name.
    pub name: PackageName,
    /// The optional package version.
    pub version: Option<VersionReq>,
}

impl FromStr for VersionedPackageName {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.split_once('@') {
            Some((name, version)) => Ok(Self {
                name: name.parse()?,
                version: Some(
                    version
                        .parse()
                        .with_context(|| format!("invalid package version `{version}`"))?,
                ),
            }),
            None => Ok(Self {
                name: s.parse()?,
                version: None,
            }),
        }
    }
}