use super::*;
use crate::models::prelude::*;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
impl Response for AllClaimableBalancesResponse {
fn from_json(json: String) -> Result<Self, String> {
let response = serde_json::from_str(&json).map_err(|e| e.to_string())?;
Ok(response)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct AllClaimableBalancesResponse {
#[serde(rename = "_links")]
pub links: ResponseLinks,
#[serde(rename = "_embedded")]
pub embedded: Embedded<ClaimableBalance>,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct ClaimableBalance {
#[serde(rename = "_links")]
pub links: ClaimableBalanceLinks,
pub id: String,
pub asset: String,
pub amount: String,
pub sponsor: String,
#[serde(rename = "last_modified_ledger")]
pub last_modified_ledger: i64,
#[serde(rename = "last_modified_time")]
pub last_modified_time: String,
pub claimants: Vec<Claimant>,
pub flags: ClaimableBalanceFlag,
#[serde(rename = "paging_token")]
pub paging_token: String,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct ClaimableBalanceLinks {
#[serde(rename = "self")]
pub self_field: Link,
pub transactions: TemplateLink,
pub operations: TemplateLink,
}
#[derive(Default, Debug, Clone, Serialize, PartialEq, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Claimant {
pub destination: String,
pub predicate: Predicate,
}
#[derive(Default, Debug, Clone, Serialize, PartialEq, Deserialize, Getters)]
pub struct ClaimableBalanceFlag {
pub clawback_enabled: bool,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Predicate {
pub unconditional: Option<bool>,
pub and: Option<Vec<And>>,
pub or: Option<Vec<Or>>,
pub not: Option<Not>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct And {
pub not: Option<Not>,
#[serde(rename = "abs_before")]
pub abs_before: Option<String>,
#[serde(rename = "abs_before_epoch")]
pub abs_before_epoch: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Not {
#[serde(rename = "abs_before")]
pub abs_before: String,
#[serde(rename = "abs_before_epoch")]
pub abs_before_epoch: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Or {
#[serde(rename = "abs_before")]
pub abs_before: Option<String>,
#[serde(rename = "abs_before_epoch")]
pub abs_before_epoch: Option<String>,
pub not: Option<Not>,
}
#[allow(dead_code)]
impl Predicate {
pub(crate) fn is_valid(&self, date: DateTime<Utc>) -> bool {
match self {
Predicate {
unconditional: Some(true),
..
} => true,
Predicate {
and: Some(ands), ..
} => ands.iter().all(|cond| cond.is_valid(date)),
Predicate { or: Some(ors), .. } => ors.iter().any(|cond| cond.is_valid(date)),
Predicate { not: Some(not), .. } => !not.is_valid(date),
_ => false,
}
}
}
impl And {
pub(crate) fn is_valid(&self, date: DateTime<Utc>) -> bool {
if let Some(not) = &self.not {
if not.is_valid(date) {
return false;
}
}
self.abs_before_epoch
.as_ref()
.map(|d| date < parse_epoch(d))
.unwrap_or(true)
}
}
impl Or {
pub(crate) fn is_valid(&self, date: DateTime<Utc>) -> bool {
if let Some(not) = &self.not {
if not.is_valid(date) {
return true;
}
}
self.abs_before_epoch
.as_ref()
.map(|d| date < parse_epoch(d))
.unwrap_or(false)
}
}
impl Not {
pub(crate) fn is_valid(&self, date: DateTime<Utc>) -> bool {
date <= parse_epoch(&self.abs_before_epoch)
}
}
impl Response for ClaimableBalance {
fn from_json(json: String) -> Result<Self, String> {
let response = serde_json::from_str(&json).map_err(|e| e.to_string())?;
Ok(response)
}
}