Skip to content

Commit

Permalink
refactor: remove optional error from UnixfsStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Sep 6, 2024
1 parent dcad6fb commit e55c437
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- chore: Change IpfsOptions visibility, remove UninitializedIpfs::{empty, with_opt}. [PR 294](https://github.com/dariusc93/rust-ipfs/pull/294)
- refactor: Support multiple certificates for wss. [PR 295](https://github.com/dariusc93/rust-ipfs/pull/295)
- refactor: Remove redundant static lifetime. [PR 301](https://github.com/dariusc93/rust-ipfs/pull/301)
- refactor: Remove optional error from `UnixfsStatus`.

# 0.11.21
- chore: Put libp2p-webrtc-websys behind feature.
Expand Down
6 changes: 1 addition & 5 deletions examples/unixfs-add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ async fn main() -> anyhow::Result<()> {
None => println!("failed with {written} stored"),
}

if let Some(error) = error {
anyhow::bail!(error);
} else {
anyhow::bail!("Unknown error while writting to blockstore");
}
anyhow::bail!(error);
}
UnixfsStatus::CompletedStatus { path, written, .. } => {
println!("{written} been stored with path {path}");
Expand Down
6 changes: 1 addition & 5 deletions examples/unixfs_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ async fn main() -> anyhow::Result<()> {
None => println!("failed with {written} written"),
}

if let Some(error) = error {
anyhow::bail!(error);
} else {
anyhow::bail!("Unknown error while writting to disk");
}
anyhow::bail!(error);
}
UnixfsStatus::CompletedStatus { written, .. } => {
let path = dest;
Expand Down
20 changes: 10 additions & 10 deletions src/unixfs/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tracing::{Instrument, Span};

use crate::{Ipfs, IpfsPath};

use super::UnixfsStatus;
use super::{TraversalFailed, UnixfsStatus};

pub enum AddOpt {
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Stream for UnixfsAdd {
}).await {
Ok(s) => s,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size: None, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size: None, error: e.into() };
return;
}
},
Expand All @@ -167,7 +167,7 @@ impl Stream for UnixfsAdd {
let buffer = match buffer {
Ok(buf) => buf,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
Expand All @@ -179,14 +179,14 @@ impl Stream for UnixfsAdd {
let block = match Block::new(cid, block) {
Ok(block) => block,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e.into()) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
let _cid = match repo.put_block(&block).await {
Ok(cid) => cid,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e };
return;
}
};
Expand All @@ -205,14 +205,14 @@ impl Stream for UnixfsAdd {
let block = match Block::new(cid, block) {
Ok(block) => block,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e.into()) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
let _cid = match repo.put_block(&block).await {
Ok(cid) => cid,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e };
return;
}
};
Expand All @@ -222,7 +222,7 @@ impl Stream for UnixfsAdd {
let cid = match last_cid {
Some(cid) => cid,
None => {
yield UnixfsStatus::FailedStatus { written, total_size, error: None };
yield UnixfsStatus::FailedStatus { written, total_size, error: TraversalFailed::Io(std::io::ErrorKind::InvalidData.into()).into() };
return;
}
};
Expand Down Expand Up @@ -263,7 +263,7 @@ impl Stream for UnixfsAdd {
path = match result.await {
Ok(path) => path,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e };
return;
}
};
Expand Down Expand Up @@ -325,7 +325,7 @@ impl std::future::IntoFuture for UnixfsAdd {
match status {
UnixfsStatus::CompletedStatus { path, .. } => return Ok(path),
UnixfsStatus::FailedStatus { error, .. } => {
return Err(error.unwrap_or(anyhow::anyhow!("Unable to add file")));
return Err(error);
}
_ => {}
}
Expand Down
20 changes: 8 additions & 12 deletions src/unixfs/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Stream for UnixfsGet {
.map_err(TraversalFailed::Io) {
Ok(f) => f,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
Expand All @@ -139,7 +139,7 @@ impl Stream for UnixfsGet {
.and_then(|(resolved, _)| resolved.into_unixfs_block().map_err(TraversalFailed::Path)) {
Ok(block) => block,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
Expand All @@ -154,7 +154,7 @@ impl Stream for UnixfsGet {
let block = match repo._get_block(next, &providers, local_only, timeout).await {
Ok(block) => block,
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(e) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
Expand All @@ -180,26 +180,24 @@ impl Stream for UnixfsGet {
let next = &slice[n..];
n += next.len();
if let Err(e) = file.write_all(next).await {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
if let Err(e) = file.sync_all().await {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}

written += n;
yield UnixfsStatus::ProgressStatus { written, total_size };
}

yield UnixfsStatus::ProgressStatus { written, total_size };

if segment.is_last() {
yield UnixfsStatus::ProgressStatus { written, total_size };
}
},
Ok(ContinuedWalk::Directory( .. )) | Ok(ContinuedWalk::RootDirectory( .. )) => {}, //TODO
Ok(ContinuedWalk::Symlink( .. )) => {},
Err(e) => {
yield UnixfsStatus::FailedStatus { written, total_size, error: Some(anyhow::Error::from(e)) };
yield UnixfsStatus::FailedStatus { written, total_size, error: e.into() };
return;
}
};
Expand Down Expand Up @@ -254,8 +252,6 @@ impl std::future::IntoFuture for UnixfsGet {
while let Some(status) = self.next().await {
match status {
UnixfsStatus::FailedStatus { error, .. } => {
let error = error
.unwrap_or(anyhow::anyhow!("Unknown error while writting to disk"));
return Err(error);
}
UnixfsStatus::CompletedStatus { .. } => return Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/unixfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub enum UnixfsStatus {
FailedStatus {
written: usize,
total_size: Option<usize>,
error: Option<anyhow::Error>,
error: Error,
},
}

Expand Down

0 comments on commit e55c437

Please sign in to comment.