use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Address {
pub formatted: Option<String>,
pub street: Option<String>,
pub city: Option<String>,
pub region: Option<String>,
pub postal_code: Option<String>,
pub country_code: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Condition {
pub code: u64,
pub label: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoDailyBar {
pub date: NaiveDate,
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub currency: Currency,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoDay {
pub date: NaiveDate,
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub quote_volume: Option<f64>,
pub trades: Option<u64>,
pub currency: Currency,
pub previous_close: Option<f64>,
pub change: Option<f64>,
pub change_pct: Option<f64>,
pub partial: bool,
pub through: DateTime<Utc>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoPriceHistory {
pub from: NaiveDate,
pub to: NaiveDate,
#[serde(default)]
pub bars: Vec<CryptoDailyBar>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoPriceOnDate {
pub requested_date: NaiveDate,
pub date: NaiveDate,
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub quote_volume: Option<f64>,
pub trades: Option<u64>,
pub currency: Currency,
pub previous_close: Option<f64>,
pub change: Option<f64>,
pub change_pct: Option<f64>,
pub partial: bool,
pub through: DateTime<Utc>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoProperties {
pub name: String,
pub symbol: String,
pub currency: Currency,
pub stale_hours: Option<u64>,
pub latest: Option<CryptoDay>,
pub range_52w: Option<PriceRange>,
pub quote: Option<CryptoQuote>,
pub chart: Option<PriceChart>,
pub price_on_date: Option<CryptoPriceOnDate>,
pub price_history: Option<CryptoPriceHistory>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CryptoQuote {
pub at: DateTime<Utc>,
pub price: f64,
pub currency: Currency,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Currency {
#[serde(rename = "USD")]
Usd,
#[serde(rename = "USDT")]
Usdt,
#[serde(rename = "EUR")]
Eur,
#[serde(rename = "GBP")]
Gbp,
#[serde(rename = "JPY")]
Jpy,
#[serde(rename = "CNY")]
Cny,
#[serde(rename = "CHF")]
Chf,
#[serde(rename = "CAD")]
Cad,
#[serde(rename = "AUD")]
Aud,
#[serde(rename = "NZD")]
Nzd,
#[serde(rename = "HKD")]
Hkd,
#[serde(rename = "SGD")]
Sgd,
#[serde(rename = "KRW")]
Krw,
#[serde(rename = "INR")]
Inr,
#[serde(rename = "BRL")]
Brl,
#[serde(rename = "MXN")]
Mxn,
#[serde(rename = "ZAR")]
Zar,
#[serde(rename = "SEK")]
Sek,
#[serde(rename = "NOK")]
Nok,
#[serde(rename = "DKK")]
Dkk,
#[serde(rename = "PLN")]
Pln,
#[serde(rename = "CZK")]
Czk,
#[serde(rename = "HUF")]
Huf,
#[serde(rename = "RON")]
Ron,
#[serde(rename = "TRY")]
Try,
#[serde(rename = "ILS")]
Ils,
#[serde(rename = "IDR")]
Idr,
#[serde(rename = "MYR")]
Myr,
#[serde(rename = "PHP")]
Php,
#[serde(rename = "THB")]
Thb,
#[serde(rename = "TWD")]
Twd,
#[serde(rename = "LKR")]
Lkr,
#[serde(rename = "ISK")]
Isk,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Education {
pub school: Option<EntityRef>,
pub degree: Option<String>,
pub field_of_study: Option<String>,
pub start_date: Option<PartialDate>,
pub end_date: Option<PartialDate>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct EntityRef {
pub id: Option<String>,
pub name: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Experience {
pub title: Option<String>,
pub organization: Option<EntityRef>,
pub start_date: Option<PartialDate>,
pub end_date: Option<PartialDate>,
pub is_current: Option<bool>,
pub location: Option<String>,
pub description: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FundingRound {
pub round_type: Option<FundingRoundType>,
pub announced_on: Option<NaiveDate>,
pub amount: Option<f64>,
pub currency: Option<Currency>,
#[serde(default)]
pub investors: Vec<EntityRef>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum FundingRoundType {
#[serde(rename = "pre_seed")]
PreSeed,
#[serde(rename = "seed")]
Seed,
#[serde(rename = "angel")]
Angel,
#[serde(rename = "series_a")]
SeriesA,
#[serde(rename = "series_b")]
SeriesB,
#[serde(rename = "series_c")]
SeriesC,
#[serde(rename = "series_d")]
SeriesD,
#[serde(rename = "series_e")]
SeriesE,
#[serde(rename = "series_f")]
SeriesF,
#[serde(rename = "series_g")]
SeriesG,
#[serde(rename = "series_h")]
SeriesH,
#[serde(rename = "series_i")]
SeriesI,
#[serde(rename = "series_j")]
SeriesJ,
#[serde(rename = "series_unknown")]
SeriesUnknown,
#[serde(rename = "grant")]
Grant,
#[serde(rename = "debt_financing")]
DebtFinancing,
#[serde(rename = "convertible_note")]
ConvertibleNote,
#[serde(rename = "equity_crowdfunding")]
EquityCrowdfunding,
#[serde(rename = "product_crowdfunding")]
ProductCrowdfunding,
#[serde(rename = "private_equity")]
PrivateEquity,
#[serde(rename = "corporate_round")]
CorporateRound,
#[serde(rename = "post_ipo_equity")]
PostIpoEquity,
#[serde(rename = "post_ipo_debt")]
PostIpoDebt,
#[serde(rename = "post_ipo_secondary")]
PostIpoSecondary,
#[serde(rename = "secondary_market")]
SecondaryMarket,
#[serde(rename = "non_equity_assistance")]
NonEquityAssistance,
#[serde(rename = "initial_coin_offering")]
InitialCoinOffering,
#[serde(rename = "undisclosed")]
Undisclosed,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GeoPoint {
pub lat: f64,
pub lon: f64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum LanguageProficiency {
#[serde(rename = "elementary")]
Elementary,
#[serde(rename = "limited_working")]
LimitedWorking,
#[serde(rename = "professional_working")]
ProfessionalWorking,
#[serde(rename = "full_professional")]
FullProfessional,
#[serde(rename = "native_or_bilingual")]
NativeOrBilingual,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct OrganizationProperties {
pub name: String,
pub legal_name: Option<String>,
pub description: Option<String>,
pub tagline: Option<String>,
pub domain: Option<String>,
pub industry: Option<String>,
#[serde(default)]
pub specialties: Vec<String>,
pub founded_year: Option<u64>,
pub employee_count: Option<u64>,
pub headquarters: Option<Address>,
pub revenue_range: Option<RevenueRange>,
pub ticker: Option<String>,
pub exchange: Option<String>,
pub latest_funding_round: Option<FundingRound>,
#[serde(default)]
pub funding_rounds: Vec<FundingRound>,
}
pub type PartialDate = String;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PersonLanguage {
pub name: String,
pub proficiency: Option<LanguageProficiency>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PersonLocation {
pub formatted: Option<String>,
pub country_code: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PersonPost {
pub text: String,
pub posted_on: NaiveDate,
pub like_count: Option<u64>,
pub comment_count: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PersonProperties {
pub name: String,
pub headline: Option<String>,
pub summary: Option<String>,
pub industry: Option<String>,
pub location: Option<PersonLocation>,
pub follower_count: Option<u64>,
#[serde(default)]
pub experience: Vec<Experience>,
#[serde(default)]
pub education: Vec<Education>,
#[serde(default)]
pub skills: Vec<String>,
#[serde(default)]
pub languages: Vec<PersonLanguage>,
#[serde(default)]
pub posts: Vec<PersonPost>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlaceOpeningHours {
pub day: Weekday,
pub hours: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlaceProperties {
pub name: String,
pub category: Option<String>,
#[serde(default)]
pub categories: Vec<String>,
pub description: Option<String>,
#[serde(default)]
pub summary: Vec<String>,
pub address: Option<Address>,
pub location: Option<GeoPoint>,
pub phone: Option<String>,
pub website: Option<String>,
#[serde(default)]
pub opening_hours: Vec<PlaceOpeningHours>,
pub rating: Option<PlaceRating>,
pub status: PlaceStatus,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlaceRating {
pub score: Option<f64>,
pub review_count: Option<u64>,
#[serde(default)]
pub distribution: Vec<PlaceStarCount>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlaceStarCount {
pub stars: u64,
pub count: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PlaceState {
#[serde(rename = "open")]
Open,
#[serde(rename = "temporarily_closed")]
TemporarilyClosed,
#[serde(rename = "closed")]
Closed,
#[serde(rename = "removed")]
Removed,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlaceStatus {
pub state: PlaceState,
pub detected_at: Option<DateTime<Utc>>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Precipitation {
pub mm: f64,
pub inch: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PriceChart {
#[serde(default)]
pub prices: Vec<PricePoint>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PricePoint {
pub time: DateTime<Utc>,
pub price: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PriceRange {
pub low: f64,
pub high: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RevenueRange {
pub min_usd: f64,
pub max_usd: Option<f64>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Temperature {
pub c: f64,
pub f: f64,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherCurrent {
pub observed_at: DateTime<Utc>,
pub temperature: Temperature,
pub model_estimate: bool,
pub humidity_pct: Option<f64>,
pub wind: Option<Wind>,
pub condition: Option<Condition>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherDay {
pub date: NaiveDate,
pub status: Option<WeatherStatus>,
pub model_estimate: bool,
pub high: Option<Temperature>,
pub low: Option<Temperature>,
pub precipitation: Option<Precipitation>,
pub hourly: Option<Vec<WeatherReading>>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherForecast {
pub issued_at: DateTime<Utc>,
#[serde(default)]
pub hourly: Vec<WeatherForecastStep>,
#[serde(default)]
pub daily: Vec<WeatherForecastDay>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherForecastDay {
pub date: NaiveDate,
pub high: Temperature,
pub low: Temperature,
pub precipitation: Precipitation,
pub condition: Option<Condition>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherForecastStep {
pub time: DateTime<Utc>,
pub temperature: Temperature,
pub precipitation: Precipitation,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherProperties {
pub name: String,
pub country_code: String,
pub region: Option<String>,
pub location: GeoPoint,
pub timezone: String,
pub current: Option<WeatherCurrent>,
pub today: Option<WeatherDay>,
pub yesterday: Option<WeatherDay>,
pub forecast: Option<WeatherForecast>,
pub days: Option<Vec<WeatherDay>>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WeatherReading {
pub time: DateTime<Utc>,
pub temperature: Temperature,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum WeatherStatus {
#[serde(rename = "preliminary")]
Preliminary,
#[serde(rename = "final")]
Final,
#[serde(rename = "not_reported")]
NotReported,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Weekday {
#[serde(rename = "monday")]
Monday,
#[serde(rename = "tuesday")]
Tuesday,
#[serde(rename = "wednesday")]
Wednesday,
#[serde(rename = "thursday")]
Thursday,
#[serde(rename = "friday")]
Friday,
#[serde(rename = "saturday")]
Saturday,
#[serde(rename = "sunday")]
Sunday,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Wind {
pub speed_kmh: f64,
pub speed_mph: f64,
pub direction_deg: Option<u64>,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum TypedEntity {
Organization(Box<OrganizationProperties>),
Person(Box<PersonProperties>),
Crypto(Box<CryptoProperties>),
Weather(Box<WeatherProperties>),
Place(Box<PlaceProperties>),
Unknown {
kind: String,
schema_version: u32,
properties: serde_json::Value,
},
}
impl TypedEntity {
pub(crate) fn read(
kind: &str,
schema_version: u32,
properties: &serde_json::Value,
) -> Result<TypedEntity, serde_json::Error> {
Ok(match (kind, schema_version) {
("organization", 1) => TypedEntity::Organization(boxed(properties)?),
("person", 1) => TypedEntity::Person(boxed(properties)?),
("crypto", 1) => TypedEntity::Crypto(boxed(properties)?),
("weather", 1) => TypedEntity::Weather(boxed(properties)?),
("place", 1) => TypedEntity::Place(boxed(properties)?),
_ => TypedEntity::Unknown {
kind: kind.to_owned(),
schema_version,
properties: properties.clone(),
},
})
}
}
fn boxed<'de, T: Deserialize<'de>>(
properties: &'de serde_json::Value,
) -> Result<Box<T>, serde_json::Error> {
T::deserialize(properties).map(Box::new)
}