use crate::{
domain::market::{Identifier, Label, unique},
error::{Error, ValidationError},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Currency(String);
impl Currency {
pub fn parse(value: &str) -> Result<Self, Error> {
if value.is_empty()
|| value.len() > 32
|| !value
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
{
return Err(ValidationError::InvalidMarketIdentity.into());
}
Ok(Self(value.to_owned()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for Currency {
type Error = Error;
fn try_from(v: String) -> Result<Self, Error> {
Self::parse(&v)
}
}
impl From<Currency> for String {
fn from(v: Currency) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "Label", into = "Label")]
pub struct SearchQuery(Label);
impl SearchQuery {
pub fn new(value: &str) -> Result<Self, Error> {
if value.len() > 256 {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self(Label::new(value)?))
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl TryFrom<Label> for SearchQuery {
type Error = Error;
fn try_from(v: Label) -> Result<Self, Error> {
Self::new(v.as_str())
}
}
impl From<SearchQuery> for Label {
fn from(v: SearchQuery) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "PricesFields")]
pub struct PricesRequest {
ids: Vec<Identifier>,
currencies: Vec<Currency>,
}
impl PricesRequest {
pub fn new(ids: Vec<Identifier>, currencies: Vec<Currency>) -> Result<Self, Error> {
if !(1..=515).contains(&ids.len())
|| !(1..=20).contains(¤cies.len())
|| !unique(ids.iter())
|| !unique(currencies.iter())
{
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self { ids, currencies })
}
#[must_use]
pub fn ids(&self) -> &[Identifier] {
&self.ids
}
#[must_use]
pub fn currencies(&self) -> &[Currency] {
&self.currencies
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PricesFields {
ids: Vec<Identifier>,
currencies: Vec<Currency>,
}
impl TryFrom<PricesFields> for PricesRequest {
type Error = Error;
fn try_from(v: PricesFields) -> Result<Self, Error> {
Self::new(v.ids, v.currencies)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "MarketsFields")]
pub struct MarketsRequest {
currency: Currency,
page: u32,
count: u16,
ids: Option<Vec<Identifier>>,
}
impl MarketsRequest {
pub fn new(
currency: Currency,
page: u32,
count: u16,
ids: Option<Vec<Identifier>>,
) -> Result<Self, Error> {
if page == 0
|| page > 1_000_000
|| !(1..=250).contains(&count)
|| ids
.as_ref()
.is_some_and(|v| !(1..=250).contains(&v.len()) || !unique(v.iter()))
{
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self {
currency,
page,
count,
ids,
})
}
#[must_use]
pub const fn currency(&self) -> &Currency {
&self.currency
}
#[must_use]
pub const fn page(&self) -> u32 {
self.page
}
#[must_use]
pub const fn count(&self) -> u16 {
self.count
}
#[must_use]
pub fn ids(&self) -> Option<&[Identifier]> {
self.ids.as_deref()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct MarketsFields {
currency: Currency,
page: u32,
count: u16,
#[serde(deserialize_with = "crate::domain::deserialize_optional")]
ids: Option<Vec<Identifier>>,
}
impl TryFrom<MarketsFields> for MarketsRequest {
type Error = Error;
fn try_from(v: MarketsFields) -> Result<Self, Error> {
Self::new(v.currency, v.page, v.count, v.ids)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "HistoryFields")]
pub struct HistoryRequest {
id: Identifier,
currency: Currency,
from_unix_seconds: u64,
to_unix_seconds: u64,
}
impl HistoryRequest {
pub fn new(id: Identifier, currency: Currency, from: u64, to: u64) -> Result<Self, Error> {
if from >= to || to.checked_mul(1000).is_none() {
return Err(ValidationError::InvalidMarketRecord.into());
}
Ok(Self {
id,
currency,
from_unix_seconds: from,
to_unix_seconds: to,
})
}
#[must_use]
pub const fn id(&self) -> &Identifier {
&self.id
}
#[must_use]
pub const fn currency(&self) -> &Currency {
&self.currency
}
#[must_use]
pub const fn from_unix_seconds(&self) -> u64 {
self.from_unix_seconds
}
#[must_use]
pub const fn to_unix_seconds(&self) -> u64 {
self.to_unix_seconds
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HistoryFields {
id: Identifier,
currency: Currency,
from_unix_seconds: u64,
to_unix_seconds: u64,
}
impl TryFrom<HistoryFields> for HistoryRequest {
type Error = Error;
fn try_from(v: HistoryFields) -> Result<Self, Error> {
Self::new(v.id, v.currency, v.from_unix_seconds, v.to_unix_seconds)
}
}