cargo_pants/
package.rs

1// Copyright 2019 Glenn Mohre, Sonatype.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Crate metadata as parsed from `Cargo.lock`
16
17use cargo_metadata::Version;
18use serde::{Deserialize, Serialize};
19use std::fmt;
20
21/// A Rust package (i.e. crate) as structured in `Cargo.lock`
22#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
23pub struct Package {
24    /// Name of a crate
25    pub name: String,
26
27    /// Crate version (using `semver`)
28    pub version: Version,
29
30    pub license: Option<String>, // /// Source of the crate
31    // #[serde(default)]
32    // pub source: String,
33
34    // /// Dependencies of this crate
35    // #[serde(default)]
36    // pub dependencies: Vec<String>
37    pub package_id: cargo_metadata::PackageId,
38}
39
40impl Package {
41    pub fn as_purl(&self) -> String {
42        format!("pkg:cargo/{}@{}", self.name, self.version)
43    }
44}
45
46impl fmt::Display for Package {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        write!(f, "{}:{}", self.name, self.version)
49    }
50}
51
52/// Name of a crate
53#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)]
54pub struct PackageName(pub String);
55
56impl PackageName {
57    /// Get string reference to this package name
58    pub fn as_str(&self) -> &str {
59        &self.0
60    }
61}
62
63impl AsRef<PackageName> for PackageName {
64    fn as_ref(&self) -> &PackageName {
65        self
66    }
67}
68
69impl fmt::Display for PackageName {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        self.0.fmt(f)
72    }
73}
74
75impl<'a> From<&'a str> for PackageName {
76    fn from(string: &'a str) -> PackageName {
77        PackageName(string.into())
78    }
79}
80
81impl Into<String> for PackageName {
82    fn into(self) -> String {
83        self.0
84    }
85}