diff options
Diffstat (limited to 'ipfs-api/examples/mfs.rs')
-rw-r--r-- | ipfs-api/examples/mfs.rs | 104 |
1 files changed, 54 insertions, 50 deletions
diff --git a/ipfs-api/examples/mfs.rs b/ipfs-api/examples/mfs.rs index 30a9af4..3d6419c 100644 --- a/ipfs-api/examples/mfs.rs +++ b/ipfs-api/examples/mfs.rs @@ -6,78 +6,82 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::{response, IpfsClient}; use std::fs::File; -use tokio::runtime::current_thread::Runtime; fn print_stat(stat: response::FilesStatResponse) { - println!(" type : {}", stat.typ); - println!(" hash : {}", stat.hash); - println!(" size : {}", stat.size); - println!(" cum. size: {}", stat.cumulative_size); - println!(" blocks : {}", stat.blocks); - println!(); + eprintln!(" type : {}", stat.typ); + eprintln!(" hash : {}", stat.hash); + eprintln!(" size : {}", stat.size); + eprintln!(" cum. size: {}", stat.cumulative_size); + eprintln!(" blocks : {}", stat.blocks); + eprintln!(); } // Creates an Ipfs client, and makes some calls to the Mfs Api. // -fn main() { - println!("note: this must be run in the root of the project repository"); - println!("connecting to localhost:5001..."); +#[cfg_attr(feature = "actix", actix_rt::main)] +#[cfg_attr(feature = "hyper", tokio::main)] +async fn main() { + eprintln!("note: this must be run in the root of the project repository"); + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - println!("making /test..."); - println!(); + eprintln!("making /test..."); + eprintln!(); - let mkdir = client.files_mkdir("/test", false); - let mkdir_recursive = client.files_mkdir("/test/does/not/exist/yet", true); + if let Err(e) = client.files_mkdir("/test", false).await { + eprintln!("error making /test: {}", e); + return; + } - let file_stat = client.files_stat("/test/does"); + eprintln!("making dirs /test/does/not/exist/yet..."); + eprintln!(); - let src = File::open(file!()).expect("could not read source file"); - let file_write = client.files_write("/test/mfs.rs", true, true, src); + if let Err(e) = client.files_mkdir("/test/does/not/exist/yet", true).await { + eprintln!("error making /test/does/not/exist/yet: {}", e); + return; + } - let file_write_stat = client.files_stat("/test/mfs.rs"); + eprintln!("getting status of /test/does..."); + eprintln!(); - let file_rm = client.files_rm("/test", true); + match client.files_stat("/test/does").await { + Ok(stat) => print_stat(stat), + Err(e) => { + eprintln!("error getting status of /test/does: {}", e); + return; + } + } - let fut = mkdir - .and_then(|_| { - println!("making dirs /test/does/not/exist/yet..."); - println!(); + eprintln!("writing source file to /test/mfs.rs"); + eprintln!(); - mkdir_recursive - }) - .and_then(|_| { - println!("getting status of /test/does..."); - println!(); + let src = File::open(file!()).expect("could not read source file"); - file_stat - }) - .and_then(|stat| { - print_stat(stat); + if let Err(e) = client.files_write("/test/mfs.rs", true, true, src).await { + eprintln!("error writing source file /test/mfs.rs: {}", e); + return; + } - println!("writing source file to /test/mfs.rs"); - println!(); + eprintln!("getting status of /test/mfs.rs..."); + eprintln!(); - file_write - }) - .and_then(|_| file_write_stat) - .and_then(|stat| { - print_stat(stat); + match client.files_stat("/test/mfs.rs").await { + Ok(stat) => print_stat(stat), + Err(e) => { + eprintln!("error getting status of /test/mfs.rs: {}", e); + return; + } + } - println!("removing /test..."); - println!(); + eprintln!("removing /test..."); + eprintln!(); - file_rm - }) - .map(|_| println!("done!")) - .map_err(|e| eprintln!("{}", e)); + if let Err(e) = client.files_rm("/test", true).await { + eprintln!("error removing /test: {}", e); + } - Runtime::new() - .expect("tokio runtime") - .block_on(fut) - .expect("successful response"); + eprintln!("done!"); } |