swap to Option<Thing>

This commit is contained in:
Elijah McMorris 2023-12-14 04:25:39 -08:00
parent e0d2cef391
commit 08d8d2d63b
Signed by: NexVeridian
SSH key fingerprint: SHA256:bsA1SKZxuEcEVHAy3gY1HUeM5ykRJl0U0kQHQn0hMg8
3 changed files with 27 additions and 9 deletions

View file

@ -33,13 +33,17 @@ async fn main() -> Result<(), Error> {
let json: Value = from_str(&line)?;
let data = Entity::from_json(json).expect("Failed to parse JSON");
let (id, claims, data) = EntityMini::from_entity(data);
let (mut claims, mut data) = EntityMini::from_entity(data);
let id = data.id.clone().expect("No ID");
data.id = None;
let _: Option<EntityMini> = db.delete(&id).await?;
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
let _: Option<Claims> = db.delete(&claims.0).await?;
let _: Option<Claims> = db.create(&claims.0).content(claims.1).await?;
let id = claims.id.clone().expect("No ID");
claims.id = None;
let _: Option<Claims> = db.delete(&id).await?;
let _: Option<Claims> = db.create(&id).content(claims).await?;
}
Ok(())

View file

@ -31,6 +31,7 @@ impl ClaimData {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Claims {
pub id: Option<Thing>,
pub claims: Vec<Claim>,
}
@ -42,25 +43,26 @@ pub struct Claim {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityMini {
pub id: Option<Thing>,
pub label: String,
pub claims: Thing,
pub description: String,
}
impl EntityMini {
pub fn from_entity(entity: Entity) -> (Thing, (Thing, Claims), Self) {
pub fn from_entity(entity: Entity) -> (Claims, Self) {
let thing_claim = Thing {
id: get_id_entity(&entity).id,
tb: "Claims".to_string(),
};
(
get_id_entity(&entity),
(
thing_claim.clone(),
Self::flatten_claims(entity.claims.clone()),
),
Claims {
id: Some(thing_claim.clone()),
..Self::flatten_claims(entity.claims.clone())
},
Self {
id: Some(get_id_entity(&entity)),
label: get_name(&entity),
claims: thing_claim,
description: get_description(&entity).unwrap_or("".to_string()),
@ -70,6 +72,7 @@ impl EntityMini {
fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Claims {
Claims {
id: None,
claims: claims
.iter()
.flat_map(|(pid, claim_value)| {