swap to surrealdb thing

This commit is contained in:
Elijah McMorris 2023-12-13 06:34:11 +00:00
parent f4babefdba
commit b838a5c326
Signed by: NexVeridian
SSH key fingerprint: SHA256:bsA1SKZxuEcEVHAy3gY1HUeM5ykRJl0U0kQHQn0hMg8
2 changed files with 15 additions and 26 deletions

View file

@ -23,8 +23,8 @@ async fn main() -> Result<(), Error> {
db.use_ns("wikidata").use_db("wikidata").await?;
let _: Option<EntityMini> = db.delete(id.to_string()).await?;
let _: Option<EntityMini> = db.create(id.to_string()).content(data.clone()).await?;
let _: Option<EntityMini> = db.delete(&id).await?;
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
// println!("{:#?}", data);
Ok(())

View file

@ -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<Entity, Error> {
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 {