Update README.

This commit is contained in:
David Craven 2019-02-27 19:18:58 +01:00
parent 3b58e24301
commit d12aab80a5
No known key found for this signature in database
GPG Key ID: DF438712EA50DBB1
2 changed files with 58 additions and 7 deletions

View File

@ -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::<Types>::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();
});
}

37
examples/readme.rs Normal file
View File

@ -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::<Types>::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();
});
}