diff --git a/README.md b/README.md index d410968d..01e90b3e 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,39 @@ Currently implements an altruistic bitswap strategy over mdns. ## Getting started ```rust #![feature(async_await, await_macro, futures_api)] -use ipfs::{Block, Ipfs, IpfsOptions, Types}; +use ipfs::{Ipfs, IpfsOptions, Ipld, IpldPath, Types}; +use futures::join; fn main() { let options = IpfsOptions::new(); env_logger::Builder::new().parse(&options.ipfs_log).init(); let mut ipfs = Ipfs::::new(options); - let block = Block::from("hello block2\n"); - let cid = Block::from("hello block\n").cid(); tokio::run_async(async move { + // Start daemon and initialize repo tokio::spawn_async(ipfs.start_daemon()); await!(ipfs.init_repo()).unwrap(); await!(ipfs.open_repo()).unwrap(); - await!(ipfs.put_block(block)).unwrap(); - let block = await!(ipfs.get_block(cid.clone())).unwrap(); - println!("Received block with contents: {:?}", - String::from_utf8_lossy(&block.data())); + // Create a DAG + let block1: Ipld = "block1".to_string().into(); + let block2: Ipld = "block2".to_string().into(); + let f1 = ipfs.put_dag(block1); + let f2 = ipfs.put_dag(block2); + let (res1, res2) = join!(f1, f2); + let root: Ipld = vec![res1.unwrap(), res2.unwrap()].into(); + let root_cid = await!(ipfs.put_dag(root)).unwrap(); + // Query the DAG + let path1 = IpldPath::from(root_cid.clone(), "0").unwrap(); + let path2 = IpldPath::from(root_cid, "1").unwrap(); + let f1 = ipfs.get_dag(path1); + let f2 = ipfs.get_dag(path2); + let (res1, res2) = join!(f1, f2); + println!("Received block with contents: {:?}", res1.unwrap()); + println!("Received block with contents: {:?}", res2.unwrap()); + + // Exit ipfs.exit_daemon(); }); } diff --git a/examples/readme.rs b/examples/readme.rs new file mode 100644 index 00000000..19870029 --- /dev/null +++ b/examples/readme.rs @@ -0,0 +1,37 @@ +#![feature(async_await, await_macro, futures_api)] +use ipfs::{Ipfs, IpfsOptions, Ipld, IpldPath, Types}; +use futures::join; + +fn main() { + let options = IpfsOptions::new(); + env_logger::Builder::new().parse(&options.ipfs_log).init(); + let mut ipfs = Ipfs::::new(options); + + tokio::run_async(async move { + // Start daemon and initialize repo + tokio::spawn_async(ipfs.start_daemon()); + await!(ipfs.init_repo()).unwrap(); + await!(ipfs.open_repo()).unwrap(); + + // Create a DAG + let block1: Ipld = "block1".to_string().into(); + let block2: Ipld = "block2".to_string().into(); + let f1 = ipfs.put_dag(block1); + let f2 = ipfs.put_dag(block2); + let (res1, res2) = join!(f1, f2); + let root: Ipld = vec![res1.unwrap(), res2.unwrap()].into(); + let root_cid = await!(ipfs.put_dag(root)).unwrap(); + + // Query the DAG + let path1 = IpldPath::from(root_cid.clone(), "0").unwrap(); + let path2 = IpldPath::from(root_cid, "1").unwrap(); + let f1 = ipfs.get_dag(path1); + let f2 = ipfs.get_dag(path2); + let (res1, res2) = join!(f1, f2); + println!("Received block with contents: {:?}", res1.unwrap()); + println!("Received block with contents: {:?}", res2.unwrap()); + + // Exit + ipfs.exit_daemon(); + }); +}