use std::{collections::BTreeSet, fmt, str::FromStr};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::{
domain::ExactDecimal,
error::{Error, ValidationError},
};
use super::{Address, Currency, Hash};
fn invalid_record() -> Error {
ValidationError::InvalidXrplRecord.into()
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Drops(u64);
impl Drops {
pub const MAX: u64 = 100_000_000_000_000_000;
pub const fn new(value: u64) -> Result<Self, Error> {
if value > Self::MAX {
return Err(Error::Validation(ValidationError::InvalidXrplAmount));
}
Ok(Self(value))
}
pub fn parse(text: &str) -> Result<Self, Error> {
if text.is_empty()
|| (text.len() > 1 && text.starts_with('0'))
|| !text.bytes().all(|b| b.is_ascii_digit())
{
return Err(ValidationError::InvalidXrplAmount.into());
}
Self::new(
text.parse()
.map_err(|_| Error::from(ValidationError::InvalidXrplAmount))?,
)
}
#[must_use]
pub const fn raw(self) -> u64 {
self.0
}
}
impl fmt::Display for Drops {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for Drops {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Error> {
Self::parse(text)
}
}
impl Serialize for Drops {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> Deserialize<'de> for Drops {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct IssuedValue(ExactDecimal);
impl IssuedValue {
pub fn parse(text: &str) -> Result<Self, Error> {
let value = ExactDecimal::parse(text)
.map_err(|_| Error::from(ValidationError::InvalidXrplAmount))?;
if !value.is_zero() {
let canonical = value.canonical();
let absolute = canonical.strip_prefix('-').unwrap_or(&canonical);
let decimal_position = absolute.find('.').unwrap_or(absolute.len());
let digits: String = absolute.chars().filter(|c| *c != '.').collect();
let leading = digits.bytes().take_while(|b| *b == b'0').count();
let significant = digits.trim_start_matches('0').trim_end_matches('0').len();
let order = i64::try_from(decimal_position).map_err(|_| invalid_record())?
- i64::try_from(leading).map_err(|_| invalid_record())?
- 1;
if significant > 16 || !(-96..=80).contains(&(order - 15)) {
return Err(ValidationError::InvalidXrplAmount.into());
}
}
Ok(Self(value))
}
#[must_use]
pub const fn value(&self) -> &ExactDecimal {
&self.0
}
}
impl FromStr for IssuedValue {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Error> {
Self::parse(text)
}
}
impl<'de> Deserialize<'de> for IssuedValue {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AccountBalance {
account: Address,
balance: Drops,
sequence: u32,
owner_count: u32,
flags: u32,
}
impl AccountBalance {
#[must_use]
pub const fn new(
account: Address,
balance: Drops,
sequence: u32,
owner_count: u32,
flags: u32,
) -> Self {
Self {
account,
balance,
sequence,
owner_count,
flags,
}
}
#[must_use]
pub const fn account(&self) -> Address {
self.account
}
#[must_use]
pub const fn balance(&self) -> Drops {
self.balance
}
#[must_use]
pub const fn sequence(&self) -> u32 {
self.sequence
}
#[must_use]
pub const fn owner_count(&self) -> u32 {
self.owner_count
}
#[must_use]
pub const fn flags(&self) -> u32 {
self.flags
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(from = "bool", into = "bool")]
pub enum Setting {
#[default]
Disabled,
Enabled,
}
impl From<bool> for Setting {
fn from(value: bool) -> Self {
if value { Self::Enabled } else { Self::Disabled }
}
}
impl From<Setting> for bool {
fn from(value: Setting) -> Self {
matches!(value, Setting::Enabled)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LineFlags {
pub no_ripple: Setting,
pub no_ripple_peer: Setting,
pub authorized: Setting,
pub peer_authorized: Setting,
pub freeze: Setting,
pub freeze_peer: Setting,
pub quality_in: u32,
pub quality_out: u32,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "LineFields")]
pub struct TrustLine {
peer: Address,
currency: Currency,
balance: IssuedValue,
limit: IssuedValue,
limit_peer: IssuedValue,
settings: LineFlags,
}
impl TrustLine {
pub fn new(
peer: Address,
currency: Currency,
balance: IssuedValue,
limit: IssuedValue,
limit_peer: IssuedValue,
settings: LineFlags,
) -> Result<Self, Error> {
if limit.value().is_negative() || limit_peer.value().is_negative() {
return Err(invalid_record());
}
Ok(Self {
peer,
currency,
balance,
limit,
limit_peer,
settings,
})
}
#[must_use]
pub const fn peer(&self) -> Address {
self.peer
}
#[must_use]
pub const fn currency(&self) -> Currency {
self.currency
}
#[must_use]
pub const fn balance(&self) -> &IssuedValue {
&self.balance
}
#[must_use]
pub const fn limit(&self) -> &IssuedValue {
&self.limit
}
#[must_use]
pub const fn limit_peer(&self) -> &IssuedValue {
&self.limit_peer
}
#[must_use]
pub const fn settings(&self) -> LineFlags {
self.settings
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct LineFields {
peer: Address,
currency: Currency,
balance: IssuedValue,
limit: IssuedValue,
limit_peer: IssuedValue,
settings: LineFlags,
}
impl TryFrom<LineFields> for TrustLine {
type Error = Error;
fn try_from(v: LineFields) -> Result<Self, Error> {
Self::new(
v.peer,
v.currency,
v.balance,
v.limit,
v.limit_peer,
v.settings,
)
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct Marker(String);
impl Marker {
pub fn new(text: impl Into<String>) -> Result<Self, Error> {
let text = text.into();
if text.is_empty() || text.len() > 1024 || !text.bytes().all(|b| b.is_ascii_graphic()) {
return Err(ValidationError::InvalidPageRequest.into());
}
Ok(Self(text))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for Marker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Marker([REDACTED])")
}
}
impl Serialize for Marker {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for Marker {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::new(String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "PageFields")]
pub struct PageRequest {
limit: u16,
ledger_hash: Option<Hash>,
marker: Option<Marker>,
}
impl PageRequest {
pub fn new(
limit: u16,
ledger_hash: Option<Hash>,
marker: Option<Marker>,
) -> Result<Self, Error> {
if !(10..=400).contains(&limit) || (marker.is_some() && ledger_hash.is_none()) {
return Err(ValidationError::InvalidPageRequest.into());
}
Ok(Self {
limit,
ledger_hash,
marker,
})
}
#[must_use]
pub const fn limit(&self) -> u16 {
self.limit
}
#[must_use]
pub const fn ledger_hash(&self) -> Option<Hash> {
self.ledger_hash
}
#[must_use]
pub const fn marker(&self) -> Option<&Marker> {
self.marker.as_ref()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PageFields {
limit: u16,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
ledger_hash: Option<Hash>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
marker: Option<Marker>,
}
impl TryFrom<PageFields> for PageRequest {
type Error = Error;
fn try_from(v: PageFields) -> Result<Self, Error> {
Self::new(v.limit, v.ledger_hash, v.marker)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "PageValueFields")]
pub struct TrustLinePage {
account: Address,
request: PageRequest,
lines: Vec<TrustLine>,
next: Option<Marker>,
}
impl TrustLinePage {
pub fn new(
account: Address,
request: PageRequest,
lines: Vec<TrustLine>,
next: Option<Marker>,
) -> Result<Self, Error> {
let mut identities = BTreeSet::new();
if lines.len() > usize::from(request.limit())
|| lines.iter().any(|line| {
line.peer() == account || !identities.insert((line.peer(), line.currency()))
})
|| next
.as_ref()
.is_some_and(|marker| Some(marker) == request.marker())
{
return Err(invalid_record());
}
Ok(Self {
account,
request,
lines,
next,
})
}
#[must_use]
pub const fn account(&self) -> Address {
self.account
}
#[must_use]
pub const fn request(&self) -> &PageRequest {
&self.request
}
#[must_use]
pub fn lines(&self) -> &[TrustLine] {
&self.lines
}
#[must_use]
pub const fn next_marker(&self) -> Option<&Marker> {
self.next.as_ref()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PageValueFields {
account: Address,
request: PageRequest,
lines: Vec<TrustLine>,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
next: Option<Marker>,
}
impl TryFrom<PageValueFields> for TrustLinePage {
type Error = Error;
fn try_from(v: PageValueFields) -> Result<Self, Error> {
Self::new(v.account, v.request, v.lines, v.next)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeeEstimate {
#[serde(rename = "base_fee")]
base: Drops,
#[serde(rename = "minimum_fee")]
minimum: Drops,
#[serde(rename = "median_fee")]
median: Drops,
#[serde(rename = "open_ledger_fee")]
open_ledger: Drops,
}
impl FeeEstimate {
#[must_use]
pub const fn new(
base_fee: Drops,
minimum_fee: Drops,
median_fee: Drops,
open_ledger_fee: Drops,
) -> Self {
Self {
base: base_fee,
minimum: minimum_fee,
median: median_fee,
open_ledger: open_ledger_fee,
}
}
#[must_use]
pub const fn base_fee(self) -> Drops {
self.base
}
#[must_use]
pub const fn minimum_fee(self) -> Drops {
self.minimum
}
#[must_use]
pub const fn median_fee(self) -> Drops {
self.median
}
#[must_use]
pub const fn open_ledger_fee(self) -> Drops {
self.open_ledger
}
}