Skip to content

Commit 5477f3b

Browse files
authored
Create try_query.rs
Tiny example of how to use query API
1 parent 121f416 commit 5477f3b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/examples/try_query.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use atomic_lib::errors::AtomicResult;
2+
use atomic_lib::{storelike::Query, Store, Storelike};
3+
4+
fn main() -> AtomicResult<()> {
5+
// Initialize a new store
6+
let store = Store::init()?;
7+
// Populate it with some default data
8+
store.populate()?;
9+
10+
// Create a query for all resources that are instances of the Class class
11+
let mut query = Query::new_class("https://atomicdata.dev/classes/Class");
12+
// Include resources from other servers as well
13+
query.include_external = true;
14+
15+
// Execute the query
16+
let result = store.query(&query)?;
17+
18+
println!("Found {} instances of Class:", result.subjects.len());
19+
20+
// Iterate through all found resources
21+
for subject in result.subjects {
22+
// Get the full resource
23+
match store.get_resource(&subject) {
24+
Ok(resource) => {
25+
// Try to get the shortname and description
26+
let shortname = resource
27+
.get_shortname("shortname", &store)
28+
.map(|v| v.to_string())
29+
.unwrap_or_else(|_| "No shortname".to_string());
30+
31+
let description = resource
32+
.get_shortname("description", &store)
33+
.map(|v| v.to_string())
34+
.unwrap_or_else(|_| "No description".to_string());
35+
36+
println!("\nClass: {}", shortname);
37+
println!("Subject: {}", subject);
38+
println!("Description: {}", description);
39+
}
40+
Err(e) => eprintln!("Error fetching resource {}: {}", subject, e),
41+
}
42+
}
43+
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)