chore: cargo fmt

This commit is contained in:
Joonas Koivunen 2020-06-10 20:31:27 +03:00
parent 7043fe2543
commit 392840c0ad
9 changed files with 50 additions and 34 deletions

View File

@ -60,7 +60,7 @@ fn main() {
Err(e) => {
eprintln!("Error: {}", e);
exit(1);
},
}
};
// The stream needs to be pinned on the stack to be used with StreamExt::next
pin_mut!(stream);

View File

@ -8,9 +8,9 @@ pub mod dag;
pub mod id;
pub mod pubsub;
pub mod refs;
pub mod root_files;
pub mod swarm;
pub mod version;
pub mod root_files;
pub mod support;
pub use support::recover_as_message_response;

View File

@ -165,7 +165,11 @@ impl fmt::Display for WalkError {
match &self.reason {
Loading(e) => write!(fmt, "loading of {} failed: {}", self.last_cid, e),
Parsing(e) => write!(fmt, "failed to parse {} as IPLD: {}", self.last_cid, e),
DagPb(e) => write!(fmt, "failed to resolve {} over dag-pb: {}", self.last_cid, e),
DagPb(e) => write!(
fmt,
"failed to resolve {} over dag-pb: {}",
self.last_cid, e
),
// this is asserted in the conformance tests and I don't really want to change the
// tests for this
IpldWalking(e) => write!(fmt, "{} under {}", e, self.last_cid),
@ -211,10 +215,7 @@ impl From<path::WalkFailed> for WalkFailed {
impl From<(WalkFailed, Cid)> for WalkError {
fn from((reason, last_cid): (WalkFailed, Cid)) -> Self {
WalkError {
last_cid,
reason,
}
WalkError { last_cid, reason }
}
}
@ -241,9 +242,7 @@ pub async fn walk_path<T: IpfsTypes>(
};
if current.codec() == cid::Codec::DagProtobuf {
let needle = path.next()
.expect("already checked path is not empty");
let needle = path.next().expect("already checked path is not empty");
let mut lookup = match ipfs::unixfs::ll::resolve(&data, &needle, &mut cache) {
Ok(MaybeResolved::NeedToLoadMore(lookup)) => lookup,
@ -254,7 +253,7 @@ pub async fn walk_path<T: IpfsTypes>(
Ok(MaybeResolved::NotFound) => {
let e = WalkFailed::from(path::WalkFailed::UnmatchedNamedLink(needle));
return Err(WalkError::from((e, current)));
},
}
Err(e) => return Err(WalkError::from((WalkFailed::from(e), current))),
};
@ -271,19 +270,23 @@ pub async fn walk_path<T: IpfsTypes>(
Ok(MaybeResolved::Found(cid)) => {
current = cid;
break;
},
}
Ok(MaybeResolved::NotFound) => {
let e = WalkFailed::from(path::WalkFailed::UnmatchedNamedLink(needle));
return Err(WalkError::from((e, next)));
},
Err(e) => return Err(WalkError::from((WalkFailed::from(e.into_resolve_error()), next))),
}
Err(e) => {
return Err(WalkError::from((
WalkFailed::from(e.into_resolve_error()),
next,
)))
}
}
}
continue;
}
let ipld = match decode_ipld(&current, &data) {
Ok(ipld) => ipld,
Err(e) => return Err(WalkError::from((WalkFailed::from(e), current))),

View File

@ -112,7 +112,7 @@ impl IpfsPath {
}
for key in self {
if current.codec() == cid::Codec::DagProtobuf {
return Err(WalkFailed::UnsupportedWalkOnDagPbIpld)
return Err(WalkFailed::UnsupportedWalkOnDagPbIpld);
}
ipld = match ipld {

View File

@ -1,10 +1,10 @@
use std::convert::TryFrom;
use crate::v0::support::unshared::Unshared;
use crate::v0::support::{with_ipfs, StringError, StreamResponse};
use crate::v0::support::{with_ipfs, StreamResponse, StringError};
use ipfs::{Ipfs, IpfsTypes};
use libipld::cid::Codec;
use ipfs::{IpfsTypes, Ipfs};
use serde::Deserialize;
use warp::{path, query, Reply, Rejection, Filter};
use std::convert::TryFrom;
use warp::{path, query, Filter, Rejection, Reply};
#[derive(Debug, Deserialize)]
pub struct CatArgs {
@ -25,14 +25,14 @@ pub fn cat<T: IpfsTypes>(
}
async fn cat_inner<T: IpfsTypes>(ipfs: Ipfs<T>, args: CatArgs) -> Result<impl Reply, Rejection> {
use crate::v0::refs::{IpfsPath, walk_path};
use ipfs::unixfs::{TraversalFailed, ll::file::FileReadFailed};
use crate::v0::refs::{walk_path, IpfsPath};
use ipfs::unixfs::{ll::file::FileReadFailed, TraversalFailed};
let path = IpfsPath::try_from(args.arg.as_str()).map_err(StringError::from)?;
let range = match (args.offset, args.length) {
(Some(start), Some(len)) => Some(start..(start + len)),
(Some(_start), None) => { todo!("need to abstract over the range") },
(Some(_start), None) => todo!("need to abstract over the range"),
(None, Some(len)) => Some(0..len),
(None, None) => None,
};
@ -48,9 +48,11 @@ async fn cat_inner<T: IpfsTypes>(ipfs: Ipfs<T>, args: CatArgs) -> Result<impl Re
// TODO: timeout
let stream = match ipfs::unixfs::cat(ipfs, cid, range).await {
Ok(stream) => stream,
Err(TraversalFailed::Walking(_, FileReadFailed::UnexpectedType(ut))) if ut.is_directory() => {
Err(TraversalFailed::Walking(_, FileReadFailed::UnexpectedType(ut)))
if ut.is_directory() =>
{
return Err(StringError::from("this dag node is a directory").into())
},
}
Err(e) => return Err(StringError::from(e).into()),
};

View File

@ -391,7 +391,10 @@ impl<Types: IpfsTypes> Ipfs<Types> {
&self,
cid: Cid,
range: Option<Range<u64>>,
) -> Result<impl Stream<Item = Result<Vec<u8>, unixfs::TraversalFailed>> + Send + '_, unixfs::TraversalFailed> {
) -> Result<
impl Stream<Item = Result<Vec<u8>, unixfs::TraversalFailed>> + Send + '_,
unixfs::TraversalFailed,
> {
unixfs::cat(self, cid, range).await
}

View File

@ -50,7 +50,7 @@ where
};
(visit, bytes)
},
}
Err(e) => {
return Err(TraversalFailed::Walking(cid, e));
}

View File

@ -553,25 +553,33 @@ impl std::error::Error for LookupError {
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use super::{resolve, MaybeResolved};
use cid::Cid;
use hex_literal::hex;
use super::{resolve, MaybeResolved};
use std::convert::TryFrom;
#[test]
fn resolve_paths() {
let payload = hex!("12330a2212206aad27d7e2fc815cd15bf679535062565dc927a831547281fc0af9e5d7e67c74120b6166726963616e2e747874180812340a221220fd36ac5279964db0cba8f7fa45f8c4c44ef5e2ff55da85936a378c96c9c63204120c616d6572696361732e747874180812360a2212207564c20415869d77a8a40ca68a9158e397dd48bdff1325cdb23c5bcd181acd17120e6175737472616c69616e2e7478741808");
let segments = [
("african.txt", "QmVX54jfjB8eRxLVxyQSod6b1FyDh7mR4mQie9j97i2Qk3"),
("americas.txt","QmfP6D9bRV4FEYDL4EHZtZG58kDwDfnzmyjuyK5d1pvzbM"),
("australian.txt", "QmWEuXAjUGyndgr4MKqMBgzMW36XgPgvitt2jsXgtuc7JE")
(
"african.txt",
"QmVX54jfjB8eRxLVxyQSod6b1FyDh7mR4mQie9j97i2Qk3",
),
(
"americas.txt",
"QmfP6D9bRV4FEYDL4EHZtZG58kDwDfnzmyjuyK5d1pvzbM",
),
(
"australian.txt",
"QmWEuXAjUGyndgr4MKqMBgzMW36XgPgvitt2jsXgtuc7JE",
),
];
let mut cache = None;
for (segment, link) in &segments {
let target = Cid::try_from(*link).unwrap();
let res = resolve(&payload[..], segment, &mut cache);

View File

@ -10,7 +10,7 @@ pub mod file;
/// UnixFS directory support.
pub mod dir;
pub use dir::{resolve, MaybeResolved, ResolveError, LookupError};
pub use dir::{resolve, LookupError, MaybeResolved, ResolveError};
mod pb;
use crate::pb::UnixFsType;