-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathpath.rs
48 lines (45 loc) · 1.64 KB
/
path.rs
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
// SPDX-FileCopyrightText: 2023 Joep Meindertsma <[email protected]>
//
// SPDX-License-Identifier: MIT
use crate::{
print::{get_serialization, print_resource},
Context,
};
use atomic_lib::{agents::ForAgent, errors::AtomicResult, serialize, storelike, Atom, Storelike};
use serialize::Format;
/// Resolves an Atomic Path query
pub fn get_path(context: &mut Context) -> AtomicResult<()> {
let subcommand_matches = context.matches.subcommand_matches("get").unwrap();
let path_vec: Vec<String> = subcommand_matches
.get_many::<String>("path")
.expect("Add a URL, shortname or path")
.map(|s| s.to_string())
.collect();
let path_string: String = path_vec.join(" ");
let serialization: Format = get_serialization(subcommand_matches)?;
// Returns a URL or Value
let store = &mut context.store;
let path = store.get_path(
&path_string,
Some(&context.mapping.lock().unwrap()),
&ForAgent::Sudo,
)?;
let out = match path {
storelike::PathReturn::Subject(subject) => {
let resource = store.get_resource_extended(&subject, false, &ForAgent::Sudo)?;
print_resource(context, &resource, subcommand_matches)?;
return Ok(());
}
storelike::PathReturn::Atom(atom) => match serialization {
Format::JsonLd | Format::Json | Format::JsonAd | Format::Pretty => {
atom.value.to_string()
}
Format::NTriples => {
let atoms: Vec<Atom> = vec![*atom];
serialize::atoms_to_ntriples(atoms, store)?
}
},
};
println!("{}", out);
Ok(())
}