Skip to content

Commit 185db77

Browse files
committed
refactor(component)!: turn ComponentPart's fields into named ones
1 parent 6b1aa7d commit 185db77

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/dist/component/components.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,31 @@ pub(crate) struct ComponentBuilder<'a> {
102102

103103
impl<'a> ComponentBuilder<'a> {
104104
pub(crate) fn copy_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
105-
self.parts
106-
.push(ComponentPart("file".to_owned(), path.clone()));
105+
self.parts.push(ComponentPart {
106+
kind: "file".to_owned(),
107+
path: path.clone(),
108+
});
107109
self.tx.copy_file(&self.name, path, src)
108110
}
109111
pub(crate) fn copy_dir(&mut self, path: PathBuf, src: &Path) -> Result<()> {
110-
self.parts
111-
.push(ComponentPart("dir".to_owned(), path.clone()));
112+
self.parts.push(ComponentPart {
113+
kind: "dir".to_owned(),
114+
path: path.clone(),
115+
});
112116
self.tx.copy_dir(&self.name, path, src)
113117
}
114118
pub(crate) fn move_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
115-
self.parts
116-
.push(ComponentPart("file".to_owned(), path.clone()));
119+
self.parts.push(ComponentPart {
120+
kind: "file".to_owned(),
121+
path: path.clone(),
122+
});
117123
self.tx.move_file(&self.name, path, src)
118124
}
119125
pub(crate) fn move_dir(&mut self, path: PathBuf, src: &Path) -> Result<()> {
120-
self.parts
121-
.push(ComponentPart("dir".to_owned(), path.clone()));
126+
self.parts.push(ComponentPart {
127+
kind: "dir".to_owned(),
128+
path: path.clone(),
129+
});
122130
self.tx.move_dir(&self.name, path, src)
123131
}
124132
pub(crate) fn finish(mut self) -> Result<Transaction<'a>> {
@@ -146,7 +154,13 @@ impl<'a> ComponentBuilder<'a> {
146154
}
147155

148156
#[derive(Debug)]
149-
pub struct ComponentPart(pub String, pub PathBuf);
157+
pub struct ComponentPart {
158+
/// Kind of the [`ComponentPart`], such as `"file"` or `"dir"`.
159+
pub kind: String,
160+
/// Relative path of the [`ComponentPart`],
161+
/// with components separated by the system's main path separator.
162+
pub path: PathBuf,
163+
}
150164

151165
impl ComponentPart {
152166
const PATH_SEP_MANIFEST: &str = "/";
@@ -155,11 +169,11 @@ impl ComponentPart {
155169
pub(crate) fn encode(&self) -> String {
156170
// Lossy conversion is safe here because we assume that `path` comes from
157171
// `ComponentPart::decode()`, i.e. from calling `Path::from()` on a `&str`.
158-
let mut path = self.1.to_string_lossy();
172+
let mut path = self.path.to_string_lossy();
159173
if Self::PATH_SEP_MAIN != Self::PATH_SEP_MANIFEST {
160174
path = Cow::Owned(path.replace(Self::PATH_SEP_MAIN, Self::PATH_SEP_MANIFEST));
161175
};
162-
format!("{}:{path}", self.0)
176+
format!("{}:{path}", self.kind)
163177
}
164178

165179
pub(crate) fn decode(line: &str) -> Option<Self> {
@@ -169,7 +183,10 @@ impl ComponentPart {
169183
path_str =
170184
Cow::Owned(path_str.replace(Self::PATH_SEP_MANIFEST, Self::PATH_SEP_MAIN));
171185
};
172-
Self(line[0..pos].to_owned(), PathBuf::from(path_str.as_ref()))
186+
Self {
187+
kind: line[0..pos].to_owned(),
188+
path: PathBuf::from(path_str.as_ref()),
189+
}
173190
})
174191
}
175192
}
@@ -322,12 +339,12 @@ impl Component {
322339
prefix: self.components.prefix.abs_path(""),
323340
};
324341
for part in self.parts()?.into_iter().rev() {
325-
match &*part.0 {
326-
"file" => tx.remove_file(&self.name, part.1.clone())?,
327-
"dir" => tx.remove_dir(&self.name, part.1.clone())?,
342+
match &*part.kind {
343+
"file" => tx.remove_file(&self.name, part.path.clone())?,
344+
"dir" => tx.remove_dir(&self.name, part.path.clone())?,
328345
_ => return Err(RustupError::CorruptComponent(self.name.clone()).into()),
329346
}
330-
pset.seen(part.1);
347+
pset.seen(part.path);
331348
}
332349
for empty_dir in pset {
333350
tx.remove_dir(&self.name, empty_dir)?;
@@ -346,20 +363,20 @@ mod tests {
346363

347364
#[test]
348365
fn decode_component_part() {
349-
let ComponentPart(kind, path) = ComponentPart::decode("dir:share/doc/rust/html").unwrap();
350-
assert_eq!(kind, "dir");
366+
let part = ComponentPart::decode("dir:share/doc/rust/html").unwrap();
367+
assert_eq!(part.kind, "dir");
351368
assert_eq!(
352-
path,
369+
part.path,
353370
Path::new(&"share/doc/rust/html".replace("/", ComponentPart::PATH_SEP_MAIN))
354371
);
355372
}
356373

357374
#[test]
358375
fn encode_component_part() {
359-
let part = ComponentPart(
360-
"dir".to_owned(),
361-
["share", "doc", "rust", "html"].into_iter().collect(),
362-
);
376+
let part = ComponentPart {
377+
kind: "dir".to_owned(),
378+
path: ["share", "doc", "rust", "html"].into_iter().collect(),
379+
};
363380
assert_eq!(part.encode(), "dir:share/doc/rust/html");
364381
}
365382
}

src/dist/component/package.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ impl Package for DirectoryPackage {
103103
let part = ComponentPart::decode(l)
104104
.ok_or_else(|| RustupError::CorruptComponent(name.to_owned()))?;
105105

106-
let path = part.1;
106+
let path = part.path;
107107
let src_path = root.join(&path);
108108

109-
match &*part.0 {
109+
match &*part.kind {
110110
"file" => {
111111
if self.copy {
112112
builder.copy_file(path.clone(), &src_path)?

0 commit comments

Comments
 (0)