forked from OKEAMAH/didkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonld.rs
More file actions
102 lines (95 loc) · 2.95 KB
/
jsonld.rs
File metadata and controls
102 lines (95 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::io::{stdin, stdout, BufReader, Read, Write};
use anyhow::Result;
use clap::{Args, Subcommand};
use didkit::ssi::{
self,
jsonld::{parse_ld_context, StaticLoader},
rdf,
};
use iref::IriBuf;
use json_ld::JsonLdProcessor;
use serde_json::json;
#[derive(Subcommand)]
pub enum JsonldCmd {
/// Convert JSON-LD documents to other formats
#[clap(subcommand)]
To(JsonldToCmd),
}
#[derive(Subcommand)]
pub enum JsonldToCmd {
/// Convert to URDNA2015-canonicalized RDF N-Quads
Rdfurdna(JsonldToRDFURDNAArgs),
}
#[derive(Args)]
pub struct JsonldToRDFURDNAArgs {
/// Base IRI
#[clap(short = 'b', long)]
base: Option<String>,
/// IRI for expandContext option
#[clap(short = 'c', long)]
expand_context: Option<String>,
/// Additional values for JSON-LD @context property.
#[clap(short = 'C', long)]
more_context_json: Option<String>,
}
pub async fn cli(cmd: JsonldCmd) -> Result<()> {
match cmd {
JsonldCmd::To(cmd_to) => to(cmd_to).await?,
};
Ok(())
}
pub async fn to(cmd: JsonldToCmd) -> Result<()> {
match cmd {
JsonldToCmd::Rdfurdna(cmd) => to_rdfurdna(cmd).await?,
};
Ok(())
}
pub async fn to_rdfurdna(args: JsonldToRDFURDNAArgs) -> Result<()> {
let mut loader = StaticLoader;
let expand_context = if let Some(m_c) = args.more_context_json {
if let Some(e_c) = args.expand_context {
Some(
serde_json::to_string(&json!([
e_c,
serde_json::from_str::<serde_json::Value>(&m_c).unwrap()
]))
.unwrap(),
)
} else {
Some(m_c)
}
} else {
args.expand_context
};
let mut reader = BufReader::new(stdin());
let mut json = String::new();
reader.read_to_string(&mut json).unwrap();
let json = ssi::jsonld::syntax::to_value_with(
serde_json::from_str::<serde_json::Value>(&json).unwrap(),
Default::default,
)
.unwrap();
let expand_context = expand_context.map(|c| parse_ld_context(&c).unwrap());
// Implementation of `ssi::jsonld::json_to_dataset`
let options = ssi::jsonld::Options {
base: args.base.map(|b| IriBuf::from_string(b).unwrap()),
expand_context,
..Default::default()
};
let doc = ssi::jsonld::RemoteDocument::new(None, None, json);
let mut generator =
rdf_types::generator::Blank::new_with_prefix("b".to_string()).with_default_metadata();
let mut to_rdf = doc
.to_rdf_using(&mut generator, &mut loader, options)
.await
.map_err(Box::new)
.unwrap();
let dataset: rdf::DataSet = to_rdf
.cloned_quads()
.map(|q| q.map_predicate(|p| p.into_iri().unwrap()))
.collect();
let dataset_normalized = ssi::urdna2015::normalize(dataset.quads().map(Into::into));
let normalized = dataset_normalized.into_nquads();
stdout().write_all(normalized.as_bytes()).unwrap();
Ok(())
}