use genius_core_client::{
client::{Client, Protocol},
types::entity::HSMLEntity,
CollisionStrategy,
};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let genius_db_url = env::var("GENIUS_DB_URL").unwrap();
let genius_db_port = env::var("GENIUS_DB_PORT").unwrap();
let token = env::var("GENIUS_JWT").unwrap();
let mut client =
Client::new_with_oauth2_token(Protocol::HTTPS, genius_db_url, genius_db_port, token, None)
.await?;
let hello_entity_swid = "swid:entity:XdK5KRN6OtUd6-HQEp2-t".to_string();
let mut hello_entity = HSMLEntity::new(hello_entity_swid.clone());
let world_entity_swid = "swid:entity:QhmTxLrXfiqVRgySNUH4T".to_string();
let mut world_entity = HSMLEntity::new(world_entity_swid.clone());
hello_entity.name = Some("hello_example".to_string());
hello_entity.description = Some("Hello".to_string());
world_entity.name = Some("world_example".to_string());
world_entity.description = Some("World!".to_string());
let link_swid = "swid:link:iU66SCBZ4JzzfHNbi0WfR".to_string();
let link = HSMLEntity::new_link(
link_swid.clone(),
&[world_entity_swid.clone()],
&[hello_entity_swid.clone()],
None,
);
client
.upsert(
vec![hello_entity, world_entity, link],
CollisionStrategy::Overwrite,
)
.await?;
let hello_entity = client.query_for_entity("hello_example").await?;
let world_entity = client.query_for_entity("world_example").await?;
println!("Hello Entity:\n{:#?}", hello_entity);
println!("World Entity:\n{:#?}", world_entity);
let links_to_hello = client.query_for_entity_array("<-- hello_example").await?;
for entity in &links_to_hello.entities {
println!("Links to Hello Entity:\n{:#?}", entity);
let destination_swid = entity
.as_link()
.expect("Entity is not a link")
.destination_swid
.as_array()
.expect("Destination swid is not an array")
.first()
.expect("Destination swid array is empty")
.as_str()
.expect("Destination swid is not a string");
let world = client
.query_for_entity(destination_swid.to_string())
.await?;
println!("Destination Entity linked to Hello:\n{:#?}", world);
}
Ok(())
}