reproto_core/
resolver.rs

1use errors::Result;
2use std::fmt;
3use {RpPackage, RpRequiredPackage, Source, Version};
4
5/// A resolved package.
6#[derive(Debug)]
7pub struct Resolved {
8    /// Version of object found.
9    pub version: Option<Version>,
10    /// Source found.
11    pub source: Source,
12}
13
14/// A resolved package.
15#[derive(Debug)]
16pub struct ResolvedByPrefix {
17    /// Package object belongs to.
18    pub package: RpPackage,
19    /// Source found.
20    pub source: Source,
21}
22
23impl Resolved {
24    /// Build a resolved object from a tuple pair.
25    pub fn from_pair(pair: (Option<Version>, Source)) -> Resolved {
26        let (version, source) = pair;
27
28        Resolved { version, source }
29    }
30}
31
32impl fmt::Display for Resolved {
33    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
34        if let Some(ref version) = self.version {
35            write!(fmt, "{}-{}", self.source, version)
36        } else {
37            self.source.fmt(fmt)
38        }
39    }
40}
41
42/// Trait that translates a required package into a set of versions and objects.
43pub trait Resolver {
44    /// Resolve the specified request.
45    fn resolve(&mut self, package: &RpRequiredPackage) -> Result<Vec<Resolved>>;
46
47    /// Resolve by prefix.
48    fn resolve_by_prefix(&mut self, package: &RpPackage) -> Result<Vec<ResolvedByPrefix>>;
49}
50
51pub struct EmptyResolver;
52
53impl Resolver for EmptyResolver {
54    fn resolve(&mut self, _package: &RpRequiredPackage) -> Result<Vec<Resolved>> {
55        Ok(vec![])
56    }
57
58    fn resolve_by_prefix(&mut self, _package: &RpPackage) -> Result<Vec<ResolvedByPrefix>> {
59        Ok(vec![])
60    }
61}