From b838a5c326458fc9ed05c3b12905d02fd6dc212e Mon Sep 17 00:00:00 2001 From: NexVeridian Date: Wed, 13 Dec 2023 06:34:11 +0000 Subject: [PATCH] swap to surrealdb thing --- src/main.rs | 4 ++-- src/utils.rs | 37 +++++++++++++------------------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 212364c..6d6e8c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,8 +23,8 @@ async fn main() -> Result<(), Error> { db.use_ns("wikidata").use_db("wikidata").await?; - let _: Option = db.delete(id.to_string()).await?; - let _: Option = db.create(id.to_string()).content(data.clone()).await?; + let _: Option = db.delete(&id).await?; + let _: Option = db.create(&id).content(data.clone()).await?; // println!("{:#?}", data); Ok(()) diff --git a/src/utils.rs b/src/utils.rs index b937a58..47c9719 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_json::from_reader; use serde_json::Value; use std::fs::File; +use surrealdb::sql::Thing; use wikidata::ClaimValueData; use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId}; @@ -14,28 +15,16 @@ pub async fn get_entity(path: &str) -> Result { Ok(data) } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct Id { - pub entity_type: String, - pub id: u64, -} - -impl Id { - pub fn to_string(&self) -> (String, String) { - (self.entity_type.clone(), self.id.to_string()) - } -} - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct EntityMini { // In English pub label: String, - pub claims: Vec<(Id, ClaimValueData)>, + pub claims: Vec<(Thing, ClaimValueData)>, pub description: String, } impl EntityMini { - pub fn from_entity(entity: Entity) -> (Id, Self) { + pub fn from_entity(entity: Entity) -> (Thing, Self) { ( get_id(&entity), Self { @@ -46,14 +35,14 @@ impl EntityMini { ) } - fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Vec<(Id, ClaimValueData)> { + fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Vec<(Thing, ClaimValueData)> { claims .iter() .flat_map(|(pid, claim_value)| { let mut flattened = vec![( - Id { - id: pid.0, - entity_type: "Property".to_string(), + Thing { + id: pid.0.into(), + tb: "Property".to_string(), }, claim_value.data.clone(), )]; @@ -61,9 +50,9 @@ impl EntityMini { flattened.extend(claim_value.qualifiers.iter().map( |(qualifier_pid, qualifier_value)| { ( - Id { - id: qualifier_pid.0, - entity_type: "Property".to_string(), + Thing { + id: qualifier_pid.0.into(), + tb: "Property".to_string(), }, qualifier_value.clone(), ) @@ -75,15 +64,15 @@ impl EntityMini { } } -fn get_id(entity: &Entity) -> Id { - let (id, entity_type) = match entity.id { +fn get_id(entity: &Entity) -> Thing { + let (id, tb) = match entity.id { WikiId::EntityId(qid) => (qid.0, "Entity".to_string()), WikiId::PropertyId(pid) => (pid.0, "Property".to_string()), WikiId::LexemeId(lid) => (lid.0, "Lexeme".to_string()), _ => todo!("Not implemented"), }; - Id { id, entity_type } + Thing { id: id.into(), tb } } fn get_name(entity: &Entity) -> String {