From b11542eead3839e8b9165f12b959343f3c4ab5f2 Mon Sep 17 00:00:00 2001 From: Dr Alexander Mikhalev Date: Sat, 23 Nov 2024 19:02:08 +0000 Subject: [PATCH] Create try_query.rs Tiny example of how to use query API --- lib/examples/try_query.rs | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/examples/try_query.rs diff --git a/lib/examples/try_query.rs b/lib/examples/try_query.rs new file mode 100644 index 00000000..c0292421 --- /dev/null +++ b/lib/examples/try_query.rs @@ -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(()) +}