use core::{fmt, num::NonZeroI32, str::FromStr};
use crate::{CompositeFilterMarker, CompositeMarker, DefaultMarker, ScalarFilter, ToGraphqlInput};
use mediatime as mt;
use smol_bytes::Utf8Bytes;
use crate::graphql::filter::opt_accessor;
#[cfg(feature = "sqlx")]
mod comparison;
#[cfg(feature = "async-graphql")]
mod graphql;
mod storage;
#[cfg(test)]
mod tests;
#[cfg(feature = "sqlx")]
pub use comparison::{SqlTimebaseComparison, TimebaseCrossingError};
pub use storage::TimebaseColumnError;
#[cfg(feature = "sqlx")]
pub(crate) use storage::{DEN as TIMEBASE_DEN, NUM as TIMEBASE_NUM};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Timebase(mt::Timebase);
impl Timebase {
#[inline]
pub const fn new(timebase: mt::Timebase) -> Self {
Self(timebase)
}
#[inline]
pub const fn get(&self) -> mt::Timebase {
self.0
}
#[inline]
pub const fn reduced(&self) -> (i32, NonZeroI32) {
let num = self.0.num();
let den = self.0.den().get();
let divisor = super::rational::gcd(num, den);
match NonZeroI32::new(den / divisor) {
Some(den) => (num / divisor, den),
None => unreachable!(),
}
}
}
impl From<mt::Timebase> for Timebase {
#[inline]
fn from(timebase: mt::Timebase) -> Self {
Self(timebase)
}
}
impl From<Timebase> for mt::Timebase {
#[inline]
fn from(timebase: Timebase) -> Self {
timebase.0
}
}
impl fmt::Display for Timebase {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for Timebase {
type Err = ParseTimebaseError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let (num, den) = text
.split_once('/')
.ok_or(ParseTimebaseError::MissingSeparator)?;
let num = num
.parse::<i32>()
.map_err(|_| ParseTimebaseError::Numerator)?;
let den = den
.parse::<i32>()
.map_err(|_| ParseTimebaseError::Denominator)?;
let den = NonZeroI32::new(den).ok_or(ParseTimebaseError::ZeroDenominator)?;
mt::Timebase::try_new(num, den)
.map(Self)
.ok_or(ParseTimebaseError::NegativeHalf)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ParseTimebaseError {
MissingSeparator,
Numerator,
Denominator,
ZeroDenominator,
NegativeHalf,
}
impl fmt::Display for ParseTimebaseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let reason = match self {
Self::MissingSeparator => "a timebase is spelled num/den",
Self::Numerator => "the numerator is not a 32-bit integer",
Self::Denominator => "the denominator is not a 32-bit integer",
Self::ZeroDenominator => "the denominator must not be zero",
Self::NegativeHalf => "neither half may be negative",
};
f.write_str(reason)
}
}
impl core::error::Error for ParseTimebaseError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimebaseOperand(Timebase);
impl TimebaseOperand {
#[inline]
#[must_use]
pub const fn new(timebase: Timebase) -> Self {
Self(timebase)
}
#[inline]
#[must_use]
pub const fn get(&self) -> Timebase {
self.0
}
}
impl From<Timebase> for TimebaseOperand {
#[inline]
fn from(timebase: Timebase) -> Self {
Self(timebase)
}
}
impl From<TimebaseOperand> for Timebase {
#[inline]
fn from(operand: TimebaseOperand) -> Self {
operand.0
}
}
impl ToGraphqlInput for Timebase {
type Input = TimebaseOperand;
type Error = core::convert::Infallible;
#[inline]
fn cross(input: Self::Input) -> Result<Self, Self::Error> {
Ok(input.into())
}
#[inline]
fn carry(operand: &Self) -> Self::Input {
TimebaseOperand::new(*operand)
}
}
impl CompositeFilterMarker for Timebase {
type Filter = TimebaseFilter;
type ColumnFilter = TimebaseFilter;
}
impl DefaultMarker for Timebase {
type Marker = CompositeMarker<Timebase>;
}
#[derive(Debug, Clone, PartialEq, ingraph::Filter)]
#[ingraph(composed)]
pub struct TimebaseFilter {
eq: Option<Timebase>,
ne: Option<Timebase>,
in_: Option<Box<[Timebase]>>,
not_in: Option<Box<[Timebase]>>,
starts_with: Option<Utf8Bytes>,
ends_with: Option<Utf8Bytes>,
contains: Option<Utf8Bytes>,
numerator: Option<ScalarFilter<i32>>,
denominator: Option<ScalarFilter<NonZeroI32>>,
}
impl TimebaseFilter {
#[inline]
pub const fn new() -> Self {
Self {
eq: None,
ne: None,
in_: None,
not_in: None,
starts_with: None,
ends_with: None,
contains: None,
numerator: None,
denominator: None,
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.eq.is_none()
&& self.ne.is_none()
&& self.in_.is_none()
&& self.not_in.is_none()
&& self.starts_with.is_none()
&& self.ends_with.is_none()
&& self.contains.is_none()
&& match &self.numerator {
Some(numerator) => numerator.is_empty(),
None => true,
}
&& match &self.denominator {
Some(denominator) => denominator.is_empty(),
None => true,
}
}
#[inline]
pub const fn numerator(&self) -> Option<&ScalarFilter<i32>> {
self.numerator.as_ref()
}
#[inline]
pub const fn denominator(&self) -> Option<&ScalarFilter<NonZeroI32>> {
self.denominator.as_ref()
}
opt_accessor!(eq: Timebase => with_eq, set_eq, maybe_eq, update_eq);
opt_accessor!(ne: Timebase => with_ne, set_ne, maybe_ne, update_ne);
opt_accessor!(in_: [Timebase] => with_in_, set_in_, maybe_in_, update_in_);
opt_accessor!(not_in: [Timebase] => with_not_in, set_not_in, maybe_not_in, update_not_in);
opt_accessor!(
starts_with: Utf8Bytes => with_starts_with, set_starts_with, maybe_starts_with,
update_starts_with
);
opt_accessor!(
ends_with: Utf8Bytes => with_ends_with, set_ends_with, maybe_ends_with, update_ends_with
);
opt_accessor!(contains: Utf8Bytes => with_contains, set_contains, maybe_contains, update_contains);
#[must_use]
#[inline]
pub fn with_numerator(mut self, numerator: ScalarFilter<i32>) -> Self {
self.set_numerator(numerator);
self
}
#[inline]
pub fn set_numerator(&mut self, numerator: ScalarFilter<i32>) -> &mut Self {
self.numerator = Some(numerator);
self
}
#[must_use]
#[inline]
pub fn with_denominator(mut self, denominator: ScalarFilter<NonZeroI32>) -> Self {
self.set_denominator(denominator);
self
}
#[inline]
pub fn set_denominator(&mut self, denominator: ScalarFilter<NonZeroI32>) -> &mut Self {
self.denominator = Some(denominator);
self
}
}
impl Default for TimebaseFilter {
#[inline]
fn default() -> Self {
Self::new()
}
}