rust-ipfs/examples
2020-01-10 16:28:31 +01:00
..
block.data Get unixfs demo working. 2019-03-03 20:22:10 +01:00
client1.rs Fix compile errors 2020-01-10 16:28:31 +01:00
client2.rs Fix compile errors 2020-01-10 16:28:31 +01:00
ipfs_bitswap_test.rs Fix compile errors 2020-01-10 16:28:31 +01:00
ipfs_ipns_test.rs Fix compile errors 2020-01-10 16:28:31 +01:00
readme.rs Fix compile errors 2020-01-10 16:28:31 +01:00

use ipfs::{UninitializedIpfs, IpfsOptions, Ipld, Types};
use futures::join;
use futures::{FutureExt, TryFutureExt};

fn main() {
    let options = IpfsOptions::<Types>::default();
    env_logger::Builder::new().parse_filters(&options.ipfs_log).init();

    tokio::runtime::current_thread::block_on_all(async move {
        // Start daemon and initialize repo
        let (ipfs, fut) = UninitializedIpfs::new(options).await.start().await.unwrap();
        tokio::spawn(fut.unit_error().boxed().compat());

        // 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 path = ipfs.put_dag(root).await.unwrap();

        // Query the DAG
        let path1 = path.sub_path("0").unwrap();
        let path2 = path.sub_path("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();
    }.unit_error().boxed().compat()).unwrap();
}