use std::{any::Any, rc::Rc};
use crate::{Instant, Resource};
#[derive(Clone, PartialEq)]
pub enum ApiResponse {
Uninitialized,
Data {
value: Resource<Payload>,
expiry: Option<Instant>,
},
}
impl ApiResponse {
pub fn new(value: Resource<Payload>, expiry: Option<Instant>) -> Self {
Self::Data { value, expiry }
}
pub fn new_loading() -> Self {
ApiResponse::Data {
value: Resource::Loading,
expiry: None,
}
}
pub fn get_value(&self) -> Resource<Payload> {
match self {
Self::Uninitialized => Resource::Loading,
Self::Data { value, expiry: _ } => value.clone(),
}
}
pub fn needs_update(&self) -> bool {
match self {
ApiResponse::Uninitialized => true,
ApiResponse::Data { value: _, expiry } => {
let Some(expiry) = expiry else {
return false;
};
expiry.is_expire()
}
}
}
}
#[derive(Clone)]
pub struct Payload {
value: Rc<dyn Any>,
eq: fn(&dyn Any, &dyn Any) -> bool,
}
impl Payload {
pub fn new<T: PartialEq + 'static>(value: T) -> Self {
Self {
value: Rc::new(value),
eq: eq_as::<T>,
}
}
pub fn downcast<T: 'static>(self) -> Option<Rc<T>> {
self.value.downcast().ok()
}
}
impl PartialEq for Payload {
fn eq(&self, other: &Self) -> bool {
(self.eq)(self.value.as_ref(), other.value.as_ref())
}
}
fn eq_as<T: PartialEq + 'static>(left: &dyn Any, right: &dyn Any) -> bool {
match (left.downcast_ref::<T>(), right.downcast_ref::<T>()) {
(Some(left), Some(right)) => left == right,
_ => false,
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use super::Payload;
#[test]
fn payloads_are_equal_when_their_values_are() {
assert!(Payload::new(5) == Payload::new(5));
assert!(Payload::new(5) != Payload::new(6));
assert!(Payload::new(5_i32) != Payload::new(5_i64));
}
#[test]
fn downcast_hands_out_the_value_itself() {
let payload = Payload::new("text".to_string());
let first = payload.clone().downcast::<String>();
let second = payload.clone().downcast::<String>();
assert_eq!(first.as_deref().map(String::as_str), Some("text"));
assert!(matches!((first, second), (Some(a), Some(b)) if Rc::ptr_eq(&a, &b)));
assert!(payload.downcast::<i32>().is_none());
}
}