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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::fmt;

pub trait DependencyExt: fmt::Display {
    fn version(&self, version: &str) -> Dependency {
        Dependency::version(&self.to_string(), version)
    }

    fn optional(&self) -> Dependency {
        Dependency::new(&self.to_string(), DependencyType::Crate(Crate { version: None }))
                   .optional(true)
                   .build()
    }

    fn path(&self, path: &str) -> Dependency {
        Dependency::new(&self.to_string(), DependencyType::Path(path.to_string()))
    }
}

impl<T: fmt::Display> DependencyExt for T {}

#[derive(Debug, Clone, PartialEq)]
pub struct Dependency {
    pub label: String,
    pub dep_type: DependencyType,
    pub optional: Option<bool>,
    pub default_features: Option<bool>,
}

impl Dependency {
    pub fn new(label: &str, dep_type: DependencyType) -> Dependency {
        Dependency {
            label: label.to_string(),
            dep_type: dep_type,
            optional: None,
            default_features: None, 
        }
    }

    pub fn is_inline(&self) -> bool {
        // types of dependencies that look ok when rendered inline
        //
        // e.g.
        //
        // foo = "1.0.0"
        // bar = { path = "baz" }
        // qux = { git = "https://github.com/foo/bar" }
        let inline_type = match self.dep_type {
            DependencyType::Crate(..) => true,
            DependencyType::Repo(Repo { url: _, repo_type: None }) => true,
            DependencyType::Path(..) => true,
            _ => false
        };
        // adding `optional = true` inline looks ok, but not `default_features` (imo)
        match (inline_type, self.default_features) {
            (true, None) => return true,
            _ => return false,
        }
    }

    pub fn optional(&mut self, optional: bool) -> &mut Self {
        self.optional = Some(optional);
        self
    }

    pub fn default_features(&mut self, default_features: bool) -> &mut Self {
        self.default_features = Some(default_features);
        self
    }

    /* Constructors */
    pub fn repo(label: &str, repo: &str) -> Dependency {
        Dependency::new(label, DependencyType::Repo(Repo { url: repo.into(), repo_type: None}))
    }

    pub fn version(label: &str, version: &str) -> Dependency {
        Dependency::new(label, DependencyType::Crate(Crate { version: Some(version.into())}))
    }

    pub fn path(label: &str, path: &str) -> Dependency {
        Dependency::new(label, DependencyType::Path(path.into()))
    }

    pub fn rev(label: &str, repo: &str, rev: &str) -> Dependency {
        Dependency::new(label, DependencyType::Repo(Repo { url: repo.into(), repo_type: Some(RepoType::Rev(rev.into()))}))
    }

    pub fn tag(label: &str, repo: &str, tag: &str) -> Dependency {
        Dependency::new(label, DependencyType::Repo(Repo { url: repo.into(), repo_type: Some(RepoType::Tag(tag.into()))}))
    }

    pub fn branch(label: &str, repo: &str, branch: &str) -> Dependency {
        Dependency::new(label, DependencyType::Repo(Repo { url: repo.into(), repo_type: Some(RepoType::Branch(branch.into()))}))
    }

    pub fn build(&self) -> Self {
        self.clone()
    }
}

impl<'a> From<&'a str> for Dependency {
    fn from(s: &'a str) -> Dependency {
        Dependency::new(s, DependencyType::Crate(Crate { version: None}))
    }
}

impl From<String> for Dependency {
    fn from(s: String) -> Dependency {
        Dependency::from(s.as_str())
    }
}

impl<'a> From<&'a Dependency> for Dependency {
    fn from(d: &Dependency) -> Dependency {
        d.clone()
    }
}

impl<'a> From<&'a mut Dependency> for Dependency {
    fn from(d: &'a mut Dependency) -> Dependency {
        d.clone()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum DependencyType {
    Crate(Crate),
    Repo(Repo),
    Path(String),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Crate {
    pub version: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Repo {
    pub url: String,
    pub repo_type: Option<RepoType>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RepoType {
    Rev(String),
    Tag(String),
    Branch(String),
}