better lib structure/docs

This commit is contained in:
Smitty 2021-05-28 10:11:49 -04:00
parent dab2888135
commit 15b8542c97
3 changed files with 67 additions and 6 deletions

View file

@ -6,43 +6,72 @@ use serde::{Deserialize, Serialize};
/// 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, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entity { pub struct Entity {
/// All of the claims on the entity.
pub claims: Vec<(Pid, ClaimValue)>, pub claims: Vec<(Pid, ClaimValue)>,
/// The type of the entity.
pub entity_type: EntityType, pub entity_type: EntityType,
pub description: Text, /// All of the descriptions in all known languages.
pub description: Vec<Text>,
/// All of the labels in all known languages.
pub labels: Vec<Text>,
} }
/// 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]
pub enum EntityType { pub enum EntityType {
/// An entity with a Qid.
Entity, Entity,
/// An entity with a Pid.
Property, Property,
/// An entity with a Lid.
Lexeme, Lexeme,
} }
/// Data relating to a claim value. /// Data relating to a claim value.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClaimValueData { pub enum ClaimValueData {
/// The ID of a file on Wikimedia Commons.
CommonsMedia(String), CommonsMedia(String),
/// Coordinates on some globe.
GlobeCoordinate { GlobeCoordinate {
//supported /// Latitude.
lat: f64, lat: f64,
/// Longitude.
lon: f64, lon: f64,
/// How many degrees of distance of precision there are.
precision: f64, precision: f64,
/// The globe the coordnaties are on, usually usually Q2 for Earth.
globe: Qid, globe: Qid,
}, },
/// A Wikidata item.
Item(Qid), Item(Qid),
/// A Wikidata property.
Property(Pid), Property(Pid),
/// A language-less string of text.
Stringg(String), Stringg(String),
/// Text with a language.
MonolingualText(Text), MonolingualText(Text),
/// The same text, translated across multiple languages.
MultilingualText(Vec<Text>), MultilingualText(Vec<Text>),
/// An external identifier.
ExternalID(String), ExternalID(String),
/// Some numeric quantity of something.
Quantity { Quantity {
/// How much.
amount: f64, // technically it could exceed the bound, but meh amount: f64, // technically it could exceed the bound, but meh
/// The lowest possible value. If this isn't present then it is exactly the amount.
lower_bound: Option<f64>, lower_bound: Option<f64>,
/// The highest possible value. If this isn't present then it is exactly the amount.
upper_bound: Option<f64>, upper_bound: Option<f64>,
/// The units used.
unit: Option<Qid>, // *could* be any IRI but in practice almost all are Wikidata entity IRIs unit: Option<Qid>, // *could* be any IRI but in practice almost all are Wikidata entity IRIs
}, },
/// A point in time time.
DateTime { DateTime {
/// The time as a Chrono DateTime.
date_time: DateTime<chrono::offset::Utc>, date_time: DateTime<chrono::offset::Utc>,
/// The precision of the date:
/// 0 - billion years /// 0 - billion years
/// 1 - 100 million years /// 1 - 100 million years
/// 2 - 10 million years /// 2 - 10 million years
@ -60,15 +89,25 @@ pub enum ClaimValueData {
/// 14 - second (deprecated) /// 14 - second (deprecated)
precision: u8, precision: u8,
}, },
/// A URL.
Url(String), Url(String),
/// A LaTeX math expression.
MathExpr(String), MathExpr(String),
/// A geometric shape. The value of the string is currently unspecified.
GeoShape(String), GeoShape(String),
/// LilyPond musical notation.
MusicNotation(String), MusicNotation(String),
/// ID of a file with tabular data on Wikimedia commons.
TabularData(String), TabularData(String),
/// A lexeme ID on Wikidata.
Lexeme(Lid), Lexeme(Lid),
/// A form ID on Wikidata.
Form(Fid), Form(Fid),
/// A sense ID on Wikidata.
Sense(Sid), Sense(Sid),
/// No value.
NoValue, NoValue,
/// Unknown value.
UnknownValue, UnknownValue,
} }
@ -95,20 +134,24 @@ impl Default for Rank {
/// A group of claims that make up a single reference. /// A group of claims that make up a single reference.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReferenceGroup { pub struct ReferenceGroup {
/// All of the claims.
pub claims: Vec<(Pid, ClaimValueData)>, pub claims: Vec<(Pid, ClaimValueData)>,
} }
/// A claim value. /// A claim value.
#[derive(Debug, Clone, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ClaimValue { pub struct ClaimValue {
/// The data of the claim.
pub data: ClaimValueData, pub data: ClaimValueData,
pub rank: Rank, pub rank: Rank,
/// The globally unique claim ID.
pub id: String, pub id: String,
pub qualifiers: Vec<(Pid, ClaimValueData)>, pub qualifiers: Vec<(Pid, ClaimValueData)>,
pub references: Vec<ReferenceGroup>, pub references: Vec<ReferenceGroup>,
} }
impl Entity { impl Entity {
/// All of the values of "instance of" on the entity.
pub fn instances(&self) -> Vec<Qid> { pub fn instances(&self) -> Vec<Qid> {
let mut instances = Vec::with_capacity(1); let mut instances = Vec::with_capacity(1);
for (pid, claim) in &self.claims { for (pid, claim) in &self.claims {
@ -122,6 +165,7 @@ impl Entity {
instances instances
} }
/// When the entity started existing.
pub fn start_time(&self) -> Option<DateTime<chrono::offset::Utc>> { pub fn start_time(&self) -> Option<DateTime<chrono::offset::Utc>> {
for (pid, claim) in &self.claims { for (pid, claim) in &self.claims {
if *pid == consts::DATE_OF_BIRTH { if *pid == consts::DATE_OF_BIRTH {
@ -133,6 +177,7 @@ impl Entity {
None None
} }
/// When the entity stopped existing.
pub fn end_time(&self) -> Option<DateTime<chrono::offset::Utc>> { pub fn end_time(&self) -> Option<DateTime<chrono::offset::Utc>> {
for (pid, claim) in &self.claims { for (pid, claim) in &self.claims {
if *pid == consts::DATE_OF_DEATH { if *pid == consts::DATE_OF_DEATH {
@ -145,6 +190,7 @@ impl Entity {
} }
} }
/// An error related to entity parsing/creation.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive] #[non_exhaustive]
pub enum EntityError { pub enum EntityError {
@ -369,6 +415,7 @@ impl ClaimValueData {
} }
impl ClaimValue { impl ClaimValue {
/// Try to parse a JSON claim to a claim value.
#[must_use] #[must_use]
pub fn get_prop_from_snak(mut claim: json::JsonValue, skip_id: bool) -> Option<ClaimValue> { pub fn get_prop_from_snak(mut claim: json::JsonValue, skip_id: bool) -> Option<ClaimValue> {
let claim_str = take_prop("rank", &mut claim) let claim_str = take_prop("rank", &mut claim)

View file

@ -3,9 +3,12 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{num::ParseIntError, str::FromStr}; use std::{num::ParseIntError, str::FromStr};
/// An error parsing an ID.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum IdParseError { pub enum IdParseError {
/// The number couldn't be parsed.
UnparseableNumber(ParseIntError), UnparseableNumber(ParseIntError),
/// The ID had an invalid prefix letter.
InvalidPrefix, InvalidPrefix,
} }
@ -14,6 +17,8 @@ macro_rules! id_def {
#[derive( #[derive(
Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
)] )]
#[doc = "A Wikidata"]
#[doc = $full_name]
pub struct $name(pub u64); pub struct $name(pub u64);
impl $name { impl $name {
@ -50,6 +55,7 @@ macro_rules! id_def {
} }
} }
impl std::fmt::Display for $name { impl std::fmt::Display for $name {
/// Display the ID as it would be in a URI.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, concat!($letter, "{}"), self.0) write!(f, concat!($letter, "{}"), self.0)
} }
@ -63,15 +69,14 @@ id_def!(Lid, "lexeme ID", "L");
/// A lexeme ID and associated form ID /// A lexeme ID and associated form ID
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
// see id_def! comment about datatype
pub struct Fid(pub Lid, pub u16); pub struct Fid(pub Lid, pub u16);
/// A lexeme ID and assoicated sense ID /// A lexeme ID and assoicated sense ID
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
// see id_def! comment about datatype
pub struct Sid(pub Lid, pub u16); pub struct Sid(pub Lid, pub u16);
impl ToString for Fid { impl ToString for Fid {
/// Display the ID as it would be in a URI.
#[must_use] #[must_use]
fn to_string(&self) -> String { fn to_string(&self) -> String {
match self { match self {
@ -81,6 +86,7 @@ impl ToString for Fid {
} }
impl ToString for Sid { impl ToString for Sid {
/// Display the ID as it would be in a URI.
#[must_use] #[must_use]
fn to_string(&self) -> String { fn to_string(&self) -> String {
match self { match self {
@ -107,7 +113,7 @@ macro_rules! pid_consts (
macro_rules! qid_unit_suffixes { macro_rules! qid_unit_suffixes {
{ $($key:ident => $value:expr),+, } => { { $($key:ident => $value:expr),+, } => {
#[must_use] #[must_use]
pub fn unit_suffix(qid: Qid) -> Option<&'static str> { pub(super) fn unit_suffix(qid: Qid) -> Option<&'static str> {
$( $(
if qid == $key { if qid == $key {
Some($value) Some($value)
@ -120,6 +126,13 @@ macro_rules! qid_unit_suffixes {
}; };
} }
impl Qid {
/// If the Qid is a commonly used unit on Wikidata, get it as a unit suffix.
pub fn unit_suffix(self) -> Option<&'static str> {
consts::unit_suffix(self)
}
}
#[allow(clippy::unreadable_literal)] #[allow(clippy::unreadable_literal)]
/// Various IDs for commonly used entities/properties on Wikidata. /// Various IDs for commonly used entities/properties on Wikidata.
pub mod consts { pub mod consts {

View file

@ -1,9 +1,10 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// A language used in the Wikibase data model. /// A language, as used in the Wikibase data model.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Lang(pub String); pub struct Lang(pub String);
/// Text that is in a certain language.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Text { pub struct Text {
pub text: String, pub text: String,