blob: 8c3aa747a4e287a254f8bd708a15142ca82e77d1 [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
// 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 std::{
collections::{HashMap, HashSet},
use crate::target::{Provider, Target};
use crate::{Error, Result};
/// A database of packages known to `qg`.
pub struct Registry {
/// Mapping of package slug to package. Slugs are globally unique.
targets: HashMap<String, Arc<Target>>,
/// All known package providers by name.
providers: HashMap<String, Provider>,
/// Unknown target dependencies.
unresolved_dependencies: HashSet<String>,
impl Registry {
/// Initializes an empty package registry.
pub fn new() -> Self {
Self {
targets: HashMap::new(),
providers: HashMap::new(),
unresolved_dependencies: HashSet::new(),
/// Returns the number of registered targets.
pub fn target_count(&self) -> usize {
/// Returns an iterator over all known targets, in arbitrary order.
pub fn targets(&self) -> impl Iterator<Item = &Target> {
/// Registers a new provider.
pub(crate) fn add_provider(&mut self, provider: Provider) -> Result<()> {
if self.providers.contains_key(& {
// TODO(frolv): Provider names must be unique.
return Err(Error::GenericErrorPlaceholder);
self.providers.insert(, provider);
/// Inserts a target into the registry.
pub(crate) fn add_target(&mut self, target: Target) -> Result<()> {
if !self.providers.contains_key(target.provider()) {
// TODO(frolv): Target must have a known provider.
return Err(Error::GenericErrorPlaceholder);
let target_name = target.full_name();
for dep in target
.filter(|&d| !self.targets.contains_key(d))
let target = Arc::new(target);
self.targets.insert(target_name, target);
impl Default for Registry {
fn default() -> Self {