Skip to content

Commit

Permalink
store and retrieve JSON DAG node
Browse files Browse the repository at this point in the history
This is silly, converting to JSON and then parsing it.
So far IPFS HTTP API can't give us CBOR directly:
ipfs/kubo#4313
  • Loading branch information
ec1oud committed Dec 15, 2020
1 parent ea09143 commit a8cf448
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ edition = "2018"
[dependencies]
ipfs-api = "0.9.0"
tokio = { version = "0.2", features = ["full"] }
futures = "0.3"
serde = "1.0"
serde_json = "1.0"
67 changes: 67 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
max_width = 100
chain_width = 100
hard_tabs = true
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
license_template_path = ""
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
merge_imports = false
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2018"
version = "One"
inline_attribute_width = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = true
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
53 changes: 49 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
/****************************************************************************
**
** Copyright (C) 2020 Shawn Rutledge
**
** This file is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation
** and appearing in the file LICENSE included in the packaging
** of this file.
**
** This code is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
****************************************************************************/

use futures::TryStreamExt;
use ipfs_api::IpfsClient;
use serde_json::Value;
use std::any::type_name;
use std::io::Cursor;
use tokio;

fn type_of<T>(_: T) -> &'static str {
type_name::<T>()
}

#[tokio::main]
async fn main() {
let client = IpfsClient::default();
let data = Cursor::new("Hello World!");
let data = r#"{"hello":423453343}"#;
let dag_node = Cursor::new(data);
println!("{}", type_of(data));

let response = client
.dag_put(dag_node)
.await
.expect("error adding dag node");

let cid = response.cid.cid_string;
eprintln!("added: {:?}", cid);

match client.add(data).await {
Ok(res) => println!("{}", res.hash),
Err(e) => eprintln!("error adding file: {}", e)
match client
.dag_get(&cid)
.map_ok(|chunk| chunk.to_vec())
.try_concat()
.await
{
Ok(bytes) => {
println!("{}", String::from_utf8_lossy(&bytes[..]));
let deserialized: Value = serde_json::from_slice(&bytes).unwrap();
println!("deserialized = {:?}", deserialized);
}
Err(e) => {
eprintln!("error reading dag node: {}", e);
}
}
}

0 comments on commit a8cf448

Please sign in to comment.