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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use errors::Result;
use serde;
use std::collections::HashMap;
use std::fmt;
use std::mem;
use std::result;
use std::slice;
use {AsPackage, RpVersionedPackage};
pub struct Parts<'a> {
iter: slice::Iter<'a, String>,
}
impl<'a> Iterator for Parts<'a> {
type Item = <slice::Iter<'a, String> as Iterator>::Item;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
impl<'a> DoubleEndedIterator for Parts<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a> Parts<'a> {
pub fn as_slice(&self) -> &'a [String] {
self.iter.as_slice()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RpPackage {
parts: Vec<String>,
}
impl AsPackage for RpPackage {
fn try_as_package(&self) -> Result<&RpPackage> {
Ok(self)
}
fn prefix_with(self, prefix: RpPackage) -> Self {
prefix.join_package(self)
}
}
impl RpPackage {
pub fn new(parts: Vec<String>) -> RpPackage {
RpPackage { parts: parts }
}
pub fn parse(input: &str) -> RpPackage {
if input.is_empty() {
return Self::empty();
}
RpPackage::new(input.split('.').map(ToOwned::to_owned).collect())
}
pub fn empty() -> RpPackage {
RpPackage { parts: vec![] }
}
pub fn join_package(mut self, other: RpPackage) -> RpPackage {
self.parts.extend(other.parts);
self
}
pub fn join_versioned(&self, other: RpVersionedPackage) -> RpVersionedPackage {
let mut parts = self.parts.clone();
parts.extend(other.package.parts);
RpVersionedPackage::new(RpPackage::new(parts), other.version)
}
pub fn join(&self, separator: &str) -> String {
self.parts.join(separator)
}
pub fn join_part<S: AsRef<str>>(mut self, other: S) -> RpPackage {
self.parts.push(other.as_ref().to_string());
self
}
pub fn starts_with(&self, other: &RpPackage) -> bool {
if self.parts.len() < other.parts.len() {
return false;
}
self.parts
.iter()
.zip(other.parts.iter())
.all(|(a, b)| a == b)
}
pub fn with_replacements(mut self, keywords: &HashMap<String, String>) -> Self {
for p in self.parts.iter_mut() {
if let Some(keyword) = keywords.get(p.as_str()) {
mem::replace(p, keyword.to_string());
}
}
self
}
pub fn with_naming<N>(mut self, naming: N) -> Self
where
N: Fn(&str) -> String,
{
for p in self.parts.iter_mut() {
let new_name = naming(p);
mem::replace(p, new_name);
}
self
}
pub fn parts(&self) -> Parts {
Parts {
iter: self.parts.iter(),
}
}
}
impl fmt::Display for RpPackage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.parts.join("."))
}
}
impl serde::Serialize for RpPackage {
fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(self)
}
}
impl<'de> serde::Deserialize<'de> for RpPackage {
fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct RpPackageVisitor;
impl<'de> serde::de::Visitor<'de> for RpPackageVisitor {
type Value = RpPackage;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a SemVer version as a string")
}
fn visit_str<E>(self, v: &str) -> result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(RpPackage::parse(v))
}
}
deserializer.deserialize_str(RpPackageVisitor)
}
}