Added new field on Entity struct that has the ID on it
Co-authored-by: Smittyvb <me@smitop.com>
This commit is contained in:
parent
5a27fbb387
commit
e9cd90efff
2 changed files with 43 additions and 2 deletions
|
@ -1,6 +1,6 @@
|
||||||
use std::{collections::BTreeMap, str::FromStr};
|
use std::{collections::BTreeMap, str::FromStr};
|
||||||
|
|
||||||
use crate::ids::{consts, Fid, Lid, Pid, Qid, Sid};
|
use crate::ids::{consts, Fid, IdParseError, Lid, Pid, Qid, Sid};
|
||||||
use crate::text::{Lang, Text};
|
use crate::text::{Lang, Text};
|
||||||
use chrono::{DateTime, TimeZone, Utc};
|
use chrono::{DateTime, TimeZone, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -9,6 +9,8 @@ use serde_json::Value;
|
||||||
/// A Wikibase entity: this could be an entity, property, or lexeme.
|
/// A Wikibase entity: this could be an entity, property, or lexeme.
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Entity {
|
pub struct Entity {
|
||||||
|
///Unique identifier
|
||||||
|
pub id: WikiId,
|
||||||
/// All of the claims on the entity.
|
/// All of the claims on the entity.
|
||||||
pub claims: Vec<(Pid, ClaimValue)>,
|
pub claims: Vec<(Pid, ClaimValue)>,
|
||||||
/// The type of the entity.
|
/// The type of the entity.
|
||||||
|
@ -21,6 +23,17 @@ pub struct Entity {
|
||||||
pub aliases: BTreeMap<Lang, Vec<String>>,
|
pub aliases: BTreeMap<Lang, Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Three main types of IDs entities can have.
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
pub enum WikiId {
|
||||||
|
/// A Qid
|
||||||
|
EntityId(Qid),
|
||||||
|
/// A Pid
|
||||||
|
PropertyId(Pid),
|
||||||
|
/// An Lid
|
||||||
|
LexemeId(Lid),
|
||||||
|
}
|
||||||
|
|
||||||
/// The type of entity: normal entity with a Qid, a property with a Pid, or a lexeme with a Lid.
|
/// The type of entity: normal entity with a Qid, a property with a Pid, or a lexeme with a Lid.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
@ -238,6 +251,17 @@ impl Entity {
|
||||||
None => json,
|
None => json,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let raw_id: &str = json
|
||||||
|
.get_mut("id")
|
||||||
|
.ok_or(EntityError::ExpectedObject)?
|
||||||
|
.as_str()
|
||||||
|
.ok_or(EntityError::ExpectedKeyvalTextString)?;
|
||||||
|
|
||||||
|
let id: WikiId = match get_wiki_id(raw_id) {
|
||||||
|
Ok(id) => id,
|
||||||
|
_ => return Err(EntityError::NoId),
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! text_keyval {
|
macro_rules! text_keyval {
|
||||||
($key:literal) => {{
|
($key:literal) => {{
|
||||||
match json.get($key) {
|
match json.get($key) {
|
||||||
|
@ -412,6 +436,7 @@ impl Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
id,
|
||||||
claims,
|
claims,
|
||||||
entity_type,
|
entity_type,
|
||||||
descriptions,
|
descriptions,
|
||||||
|
@ -421,6 +446,16 @@ impl Entity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_wiki_id(id: &str) -> Result<WikiId, IdParseError> {
|
||||||
|
let uid: WikiId = match &id[0..1] {
|
||||||
|
"Q" => WikiId::EntityId(Qid::from_str(id).unwrap()),
|
||||||
|
"P" => WikiId::PropertyId(Pid::from_str(id).unwrap()),
|
||||||
|
"L" => WikiId::LexemeId(Lid::from_str(id).unwrap()),
|
||||||
|
_ => return Err(IdParseError::InvalidPrefix),
|
||||||
|
};
|
||||||
|
Ok(uid)
|
||||||
|
}
|
||||||
|
|
||||||
/// An error related to entity parsing/creation.
|
/// An error related to entity parsing/creation.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
@ -852,6 +887,11 @@ mod test {
|
||||||
assert_eq!(qid, Ok(Qid(1234567)));
|
assert_eq!(qid, Ok(Qid(1234567)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_wiki_id_test() {
|
||||||
|
assert_eq!(get_wiki_id("Q42").unwrap(), WikiId::EntityId(Qid(42)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn number_parsing() {
|
fn number_parsing() {
|
||||||
assert_eq!(parse_wb_number(&serde_json::json!("+5")), Ok(5.));
|
assert_eq!(parse_wb_number(&serde_json::json!("+5")), Ok(5.));
|
||||||
|
|
|
@ -10,7 +10,8 @@ fn simple_item() {
|
||||||
#[test]
|
#[test]
|
||||||
fn douglas_adams() {
|
fn douglas_adams() {
|
||||||
let j: serde_json::Value = serde_json::from_str(include_str!("../items/Q42.json")).unwrap();
|
let j: serde_json::Value = serde_json::from_str(include_str!("../items/Q42.json")).unwrap();
|
||||||
Entity::from_json(j).unwrap();
|
let e = Entity::from_json(j).unwrap();
|
||||||
|
assert_eq!(e.id, WikiId::EntityId(Qid(42)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue