blob: 44bf754fb25590412acf98b34174332b70d531ec [file] [log] [blame]
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
use crate::project::manifest;
/// An installable `qg` package.
#[derive(Debug)]
pub struct Package {
name: String,
provider: String,
metadata: Metadata,
}
#[derive(Debug)]
enum Metadata {
Cipd(manifest::CipdPackage),
}
impl From<manifest::PackageType> for Metadata {
fn from(pt: manifest::PackageType) -> Self {
match pt {
manifest::PackageType::Cipd(data) => Self::Cipd(data),
}
}
}
impl Package {
pub(crate) fn from_manifest(name: &str, provider: &str, package: manifest::Package) -> Self {
Self {
name: name.to_owned(),
provider: provider.to_owned(),
metadata: package.desc.into(),
}
}
/// Returns the name of the package.
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
/// Returns the name of the package's provider.
#[must_use]
pub fn provider(&self) -> &str {
&self.provider
}
/// Returns the type of the package as a string.
#[must_use]
pub fn type_string(&self) -> &str {
match self.metadata {
Metadata::Cipd(_) => "cipd",
}
}
/// Returns the globally-unique identifier for the package.
#[must_use]
pub fn canonical_slug(&self) -> String {
if self.provider.is_empty() {
self.name.clone()
} else {
format!("{}:{}", self.provider, self.name)
}
}
}