#[allow(dead_code, clippy::all, clippy::pedantic)]
mod generated {
include!(concat!(env!("OUT_DIR"), "/tessera_generated.rs"));
}
pub use generated::{
DatasetSummary, DatasetsResponse, DayDownload, DayDownloadResponse, DownloadBody,
DownloadResponse, ErrorBody, MonthRange, Partition, PartitionsResponse,
};
use crate::error::TesseraError;
pub fn download_urls(body: DownloadBody) -> Result<Vec<String>, TesseraError> {
match body {
DownloadBody::DownloadResponse(single) => Ok(vec![single.url]),
DownloadBody::DayDownloadResponse(day_backed) => {
let DayDownloadResponse {
asset,
coin,
mut days,
month,
..
} = day_backed;
days.sort_by(|left, right| left.date.cmp(&right.date));
if days.is_empty() {
return Err(TesseraError::UnexpectedResponse(format!(
"download of day-backed partition {asset}/{}/{month} returned no day objects",
coin.as_deref().unwrap_or("-")
)));
}
Ok(days.into_iter().map(|day| day.url).collect())
}
}
}
pub(crate) fn validate_month(month: &str) -> Result<(), TesseraError> {
let digits = |bytes: &[u8]| bytes.iter().all(u8::is_ascii_digit);
let valid = month.len() == 7
&& month.as_bytes()[4] == b'-'
&& digits(&month.as_bytes()[0..4])
&& digits(&month.as_bytes()[5..7]);
if !valid {
return Err(TesseraError::InvalidArgument(format!(
"month must be in YYYY-MM format, got {month:?}"
)));
}
let m: u8 = month[5..7].parse().expect("digits checked above");
if !(1..=12).contains(&m) {
return Err(TesseraError::InvalidArgument(format!(
"month must be in YYYY-MM format, got {month:?}"
)));
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PartitionRef {
pub asset: String,
pub coin: String,
pub month: String,
}
impl PartitionRef {
pub fn new(
asset: impl Into<String>,
coin: impl Into<String>,
month: impl Into<String>,
) -> Result<Self, TesseraError> {
let month = month.into();
validate_month(&month)?;
Ok(Self {
asset: asset.into(),
coin: coin.into(),
month,
})
}
#[must_use]
pub fn object_key(&self) -> String {
format!(
"{}/coin={}/month={}.parquet",
self.asset, self.coin, self.month
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MonthSpan {
pub start: String,
pub end: String,
}
impl MonthSpan {
pub fn new(start: impl Into<String>, end: impl Into<String>) -> Result<Self, TesseraError> {
let start = start.into();
let end = end.into();
validate_month(&start)?;
validate_month(&end)?;
if start > end {
return Err(TesseraError::InvalidArgument(format!(
"MonthSpan start {start:?} is after end {end:?}"
)));
}
Ok(Self { start, end })
}
#[must_use]
pub fn months(&self) -> Vec<String> {
let (mut year, mut month) = parse_ym(&self.start);
let (end_year, end_month) = parse_ym(&self.end);
let mut months = Vec::new();
while (year, month) <= (end_year, end_month) {
months.push(format!("{year:04}-{month:02}"));
month += 1;
if month > 12 {
month = 1;
year += 1;
}
}
months
}
}
impl IntoIterator for MonthSpan {
type Item = String;
type IntoIter = std::vec::IntoIter<String>;
fn into_iter(self) -> Self::IntoIter {
self.months().into_iter()
}
}
fn parse_ym(month: &str) -> (i32, i32) {
(
month[0..4]
.parse()
.expect("validated month has a numeric year"),
month[5..7]
.parse()
.expect("validated month has a numeric month"),
)
}
pub trait IntoCoins {
fn into_coins(self) -> Result<Vec<String>, TesseraError>;
}
pub trait IntoMonths {
fn into_months(self) -> Result<Vec<String>, TesseraError>;
}
impl IntoCoins for &str {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
Ok(vec![self.to_string()])
}
}
impl IntoCoins for String {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
Ok(vec![self])
}
}
impl IntoCoins for &String {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
self.as_str().into_coins()
}
}
impl<C: AsRef<str>> IntoCoins for Vec<C> {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_coins()
}
}
impl<C: AsRef<str>> IntoCoins for &Vec<C> {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_coins()
}
}
impl<C: AsRef<str>, const N: usize> IntoCoins for [C; N] {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_coins()
}
}
impl<C: AsRef<str>, const N: usize> IntoCoins for &[C; N] {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_coins()
}
}
impl IntoMonths for &str {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
validate_month(self)?;
Ok(vec![self.to_string()])
}
}
impl IntoMonths for String {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
validate_month(&self)?;
Ok(vec![self])
}
}
impl IntoMonths for &String {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
self.as_str().into_months()
}
}
impl IntoMonths for MonthSpan {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
Ok(self.months())
}
}
impl<C: AsRef<str>> IntoMonths for &[C] {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
let months: Vec<String> = self.iter().map(|m| m.as_ref().to_string()).collect();
if months.is_empty() {
return Err(TesseraError::InvalidArgument(
"at least one month is required".to_string(),
));
}
for m in &months {
validate_month(m)?;
}
Ok(months)
}
}
impl<C: AsRef<str>> IntoMonths for Vec<C> {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_months()
}
}
impl<C: AsRef<str>> IntoMonths for &Vec<C> {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_months()
}
}
impl<C: AsRef<str>, const N: usize> IntoMonths for [C; N] {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_months()
}
}
impl<C: AsRef<str>, const N: usize> IntoMonths for &[C; N] {
fn into_months(self) -> Result<Vec<String>, TesseraError> {
self.as_slice().into_months()
}
}
impl<C: AsRef<str>> IntoCoins for &[C] {
fn into_coins(self) -> Result<Vec<String>, TesseraError> {
let coins: Vec<String> = self.iter().map(|c| c.as_ref().to_string()).collect();
if coins.is_empty() {
return Err(TesseraError::InvalidArgument(
"at least one coin is required".to_string(),
));
}
Ok(coins)
}
}