Skip to content

Commit e3752c1

Browse files
feat: Split InputManifest::load* into attached/detached forms
Previously, the `load`, `load_async` and `sha256` constructors for `InputManifest` all took a second parameter that was an `Option` that could be a value used to identify the target artifact, such as a `&Path`. In practice, this lead to awkward type inference errors, requiring callers to do things like `None::<&String>`, which we don't really want them to have to do. These 3 constructors have now been split into 3 pairs of constructors, a regular one which takes a non-optional target (which, because it's not optional, means inference should always have enough information to give useful errors), and another which does not take a target and is explicitly for constructing a "detached" manifest. Signed-off-by: Andrew Lilley Brinker <[email protected]>
1 parent 0ea4877 commit e3752c1

File tree

5 files changed

+56
-31
lines changed

5 files changed

+56
-31
lines changed

omnibor-cli/src/cmd/manifest/find.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async fn open_and_match_manifests(
5858
pin_mut!(path_rx);
5959

6060
while let Some(path) = path_rx.next().await {
61-
if let Ok(manifest) = InputManifest::sha256(&path, None) {
61+
if let Ok(manifest) = InputManifest::sha256_detached(&path) {
6262
if manifest.contains_artifact(target_aid).map_err(|source| {
6363
Error::FileFailedToIdDuringSearch {
6464
path: path.to_path_buf(),

omnibor-cli/src/cmd/store/add.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
cli::StoreAddArgs,
44
error::{Error, Result},
55
};
6-
use omnibor::{hash_algorithm::Sha256, storage::Storage, InputManifest};
6+
use omnibor::{storage::Storage, InputManifest};
77

88
/// Run the `store add` subcommand.
99
pub async fn run(app: &App, args: &StoreAddArgs) -> Result<()> {
@@ -21,8 +21,11 @@ pub async fn run(app: &App, args: &StoreAddArgs) -> Result<()> {
2121
None => None,
2222
};
2323

24-
let manifest = InputManifest::<Sha256>::load(&args.manifest, target_aid)
25-
.map_err(Error::UnableToReadManifest)?;
24+
let manifest = match target_aid {
25+
Some(target_aid) => InputManifest::load(&args.manifest, target_aid),
26+
None => InputManifest::load_detached(&args.manifest),
27+
}
28+
.map_err(Error::UnableToReadManifest)?;
2629

2730
storage
2831
.write_manifest(&manifest)

omnibor/src/input_manifest.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,39 @@ where
5555

5656
impl InputManifest<Sha256> {
5757
/// Load the input manifest at the path with the SHA-256 hash function.
58-
pub fn sha256<I>(
59-
source: I,
60-
target: Option<ArtifactId<Sha256>>,
61-
) -> Result<Self, InputManifestError>
58+
pub fn sha256<M, I>(source: M, target: I) -> Result<Self, InputManifestError>
6259
where
63-
I: ManifestSource<Sha256>,
60+
M: ManifestSource<Sha256>,
61+
I: Identify<Sha256>,
6462
{
6563
InputManifest::load(source, target)
6664
}
6765

68-
/// Load the input manifest at the path with the SHA-256 hash function asynchronously.
69-
pub async fn sha256_async<I, A>(
70-
source: I,
71-
target: Option<A>,
72-
) -> Result<Self, InputManifestError>
66+
/// Load the input manifest at the path with the SHA-256 hash function, detached.
67+
pub fn sha256_detached<M>(source: M) -> Result<Self, InputManifestError>
7368
where
74-
I: ManifestSourceAsync<Sha256>,
75-
A: IdentifyAsync<Sha256>,
69+
M: ManifestSource<Sha256>,
7670
{
77-
let target = match target {
78-
Some(t) => Some(t.identify_async().await?),
79-
None => None,
80-
};
71+
InputManifest::load_detached(source)
72+
}
8173

74+
/// Load the input manifest at the path with the SHA-256 hash function asynchronously.
75+
pub async fn sha256_async<M, I>(source: M, target: I) -> Result<Self, InputManifestError>
76+
where
77+
M: ManifestSourceAsync<Sha256>,
78+
I: IdentifyAsync<Sha256>,
79+
{
80+
let target = target.identify_async().await?;
8281
InputManifest::load_async(source, target).await
8382
}
83+
84+
/// Load the input manifest at the path with the SHA-256 hash function asynchronously, detached.
85+
pub async fn sha256_detached_async<M>(source: M) -> Result<Self, InputManifestError>
86+
where
87+
M: ManifestSourceAsync<Sha256>,
88+
{
89+
InputManifest::load_detached_async(source).await
90+
}
8491
}
8592

8693
impl<H: HashAlgorithm> InputManifest<H> {
@@ -151,27 +158,39 @@ impl<H: HashAlgorithm> InputManifest<H> {
151158
}
152159

153160
/// Construct an [`InputManifest`] from a source.
154-
pub fn load<M, I>(source: M, target: Option<I>) -> Result<Self, InputManifestError>
161+
pub fn load<M, I>(source: M, target: I) -> Result<Self, InputManifestError>
155162
where
156163
M: ManifestSource<H>,
157164
I: Identify<H>,
158165
{
159-
let target = target.map(|t| t.identify()).transpose()?;
160-
source.resolve(target)
166+
let target = target.identify()?;
167+
source.resolve(Some(target))
168+
}
169+
170+
/// Construct an [`InputManifest`] from a source, without a target.
171+
pub fn load_detached<M>(source: M) -> Result<Self, InputManifestError>
172+
where
173+
M: ManifestSource<H>,
174+
{
175+
source.resolve(None)
161176
}
162177

163178
/// Construct an [`InputManifest`] from a source, asynchronously.
164-
pub async fn load_async<M, I>(source: M, target: Option<I>) -> Result<Self, InputManifestError>
179+
pub async fn load_async<M, I>(source: M, target: I) -> Result<Self, InputManifestError>
165180
where
166181
M: ManifestSourceAsync<H>,
167182
I: IdentifyAsync<H>,
168183
{
169-
let target = match target {
170-
Some(t) => Some(t.identify_async().await?),
171-
None => None,
172-
};
184+
let target = target.identify_async().await?;
185+
source.resolve_async(Some(target)).await
186+
}
173187

174-
source.resolve_async(target).await
188+
/// Construct an [`InputManifest`] from a source, asynchronously, without a target.
189+
pub async fn load_detached_async<M>(source: M) -> Result<Self, InputManifestError>
190+
where
191+
M: ManifestSourceAsync<H>,
192+
{
193+
source.resolve_async(None).await
175194
}
176195

177196
/// Get the manifest as bytes.

omnibor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
//! 6e87984feca6b64467f9028fd6b76a4ec8d13ee23f0ae3b99b548ca0c2d0230b
161161
//! 726eb0db4f3130fb4caef53ee9d103b6bc2d732e665dd88f53efce4c7b59818b
162162
//! "#;
163-
//! let input_manifest = InputManifest::<Sha256>::load(manifest_txt, None::<&String>)?;
163+
//! let input_manifest = InputManifest::sha256_detached(manifest_txt)?;
164164
//!
165165
//! if input_manifest.contains_artifact("./test/data/c/main.c")? {
166166
//! println!("found a match!");

omnibor/src/storage/file_system_storage.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ struct ManifestsEntry<H: HashAlgorithm> {
309309
impl<H: HashAlgorithm> ManifestsEntry<H> {
310310
/// Load the [`InputManifest`] represented by this entry.
311311
fn manifest(&self) -> Result<InputManifest<H>, InputManifestError> {
312-
InputManifest::load(&self.manifest_path, self.target_aid)
312+
match self.target_aid {
313+
Some(target) => InputManifest::load(&self.manifest_path, target),
314+
None => InputManifest::load_detached(&self.manifest_path),
315+
}
313316
}
314317
}
315318

0 commit comments

Comments
 (0)