use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::error::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WireError {
pub kind: String,
pub message: String,
}
impl From<&Error> for WireError {
fn from(e: &Error) -> Self {
let kind = match e {
Error::Timeout => "Timeout",
Error::Network(_) => "Network",
Error::HttpStatus(_) => "HttpStatus",
Error::Parse(_) => "Parse",
Error::HttpClient(_) => "HttpClient",
Error::NoResults { .. } => "NoResults",
Error::LocationDetection => "LocationDetection",
Error::Dbus(_) => "Dbus",
};
Self {
kind: kind.to_string(),
message: e.to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnvelopeError {
pub kind: String,
pub message: String,
pub at: DateTime<Utc>,
}
impl EnvelopeError {
pub fn new(error: &Error, at: DateTime<Utc>) -> Self {
let wire = WireError::from(error);
Self {
kind: wire.kind,
message: wire.message,
at,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Envelope<T> {
pub data: Option<T>,
pub fetched_at: Option<DateTime<Utc>>,
pub error: Option<EnvelopeError>,
}