Skip to content

Commit e9882d1

Browse files
committed
refactor: indirect Target path via Src enum
This is a trivial preparatory refactor to create a space for adding git repository handling.
1 parent 2b1c70f commit e9882d1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/package.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ pub struct Target<'y> {
6464
resource_id: &'y str,
6565
property: &'static PackageableProperty,
6666
target: &'y mut YamlValue,
67-
path: PathBuf,
67+
src: Src,
68+
}
69+
70+
enum Src {
71+
Local(PathBuf),
6872
}
6973

7074
pub fn targets(template: &mut Template) -> impl Iterator<Item = Target<'_>> + '_ {
@@ -95,7 +99,7 @@ pub fn targets(template: &mut Template) -> impl Iterator<Item = Target<'_>> + '_
9599
resource_id,
96100
property,
97101
target,
98-
path,
102+
src: Src::Local(path),
99103
})
100104
})
101105
}
@@ -129,7 +133,8 @@ pub async fn process(
129133
}
130134

131135
async fn package_zip(target: &Target<'_>) -> Result<File, Error> {
132-
let metadata = match fs::metadata(&target.path).await {
136+
let Src::Local(src) = &target.src;
137+
let metadata = match fs::metadata(src).await {
133138
Ok(metadata) => metadata,
134139
Err(error) => return upload_err(target, error),
135140
};
@@ -145,9 +150,9 @@ async fn package_zip(target: &Target<'_>) -> Result<File, Error> {
145150
let mut writer = ZipFileWriter::new(&mut zip);
146151

147152
let paths = if metadata.is_file() {
148-
vec![Ok(target.path.clone())]
153+
vec![Ok(src.clone())]
149154
} else if metadata.is_dir() {
150-
let path = target.path.clone();
155+
let path = src.clone();
151156
tokio::task::spawn_blocking(move || scandir(&path))
152157
.await
153158
.or_else(|error| upload_err(target, format!("couldn't read: {error}")))?
@@ -159,7 +164,7 @@ async fn package_zip(target: &Target<'_>) -> Result<File, Error> {
159164
let path = path.or_else(|error| upload_err(target, format!("couldn't read: {error}")))?;
160165

161166
let file_name = path
162-
.strip_prefix(&target.path)
167+
.strip_prefix(src)
163168
.expect("file must have file name")
164169
.to_string_lossy()
165170
.into_owned();
@@ -231,9 +236,10 @@ fn scandir(path: &Path) -> Vec<io::Result<PathBuf>> {
231236
}
232237

233238
fn upload_err<T>(target: &Target, error: impl fmt::Display) -> Result<T, Error> {
239+
let Src::Local(src) = &target.src;
234240
Err(Error::other(format!(
235241
"couldn't upload `{}` for `{}`: {error}",
236-
target.path.display(),
242+
src.display(),
237243
target.resource_id
238244
)))
239245
}

0 commit comments

Comments
 (0)