Skip to content

Commit

Permalink
Create try_query.rs
Browse files Browse the repository at this point in the history
Tiny example of how to use query API
  • Loading branch information
AlexMikhalev authored and joepio committed Dec 13, 2024
1 parent 9517c11 commit b11542e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/examples/try_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use atomic_lib::errors::AtomicResult;
use atomic_lib::{storelike::Query, Store, Storelike};

fn main() -> AtomicResult<()> {
// Initialize a new store
let store = Store::init()?;
// Populate it with some default data
store.populate()?;

// Create a query for all resources that are instances of the Class class
let mut query = Query::new_class("https://atomicdata.dev/classes/Class");
// Include resources from other servers as well
query.include_external = true;

// Execute the query
let result = store.query(&query)?;

println!("Found {} instances of Class:", result.subjects.len());

// Iterate through all found resources
for subject in result.subjects {
// Get the full resource
match store.get_resource(&subject) {
Ok(resource) => {
// Try to get the shortname and description
let shortname = resource
.get_shortname("shortname", &store)
.map(|v| v.to_string())
.unwrap_or_else(|_| "No shortname".to_string());

let description = resource
.get_shortname("description", &store)
.map(|v| v.to_string())
.unwrap_or_else(|_| "No description".to_string());

println!("\nClass: {}", shortname);
println!("Subject: {}", subject);
println!("Description: {}", description);
}
Err(e) => eprintln!("Error fetching resource {}: {}", subject, e),
}
}

Ok(())
}

0 comments on commit b11542e

Please sign in to comment.