mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-06-10 14:49:51 +02:00
168 lines
3.5 KiB
Rust
168 lines
3.5 KiB
Rust
|
use chrono::{Utc, DateTime};
|
||
|
use serde::{Serialize, Deserialize};
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
|
||
|
pub struct Pending<M> {
|
||
|
update_id: u64,
|
||
|
meta: M,
|
||
|
enqueued_at: DateTime<Utc>,
|
||
|
}
|
||
|
|
||
|
impl<M> Pending<M> {
|
||
|
pub fn new(meta: M, update_id: u64) -> Self {
|
||
|
Self {
|
||
|
enqueued_at: Utc::now(),
|
||
|
meta,
|
||
|
update_id,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn processing(self) -> Processing<M> {
|
||
|
Processing {
|
||
|
from: self,
|
||
|
started_processing_at: Utc::now(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn abort(self) -> Aborted<M> {
|
||
|
Aborted {
|
||
|
from: self,
|
||
|
aborted_at: Utc::now(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn meta(&self) -> &M {
|
||
|
&self.meta
|
||
|
}
|
||
|
|
||
|
pub fn id(&self) -> u64 {
|
||
|
self.update_id
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
|
||
|
pub struct Processed<M, N> {
|
||
|
success: N,
|
||
|
processed_at: DateTime<Utc>,
|
||
|
#[serde(flatten)]
|
||
|
from: Processing<M>,
|
||
|
}
|
||
|
|
||
|
impl<M, N> Processed<M, N> {
|
||
|
pub fn id(&self) -> u64 {
|
||
|
self.from.id()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
|
||
|
pub struct Processing<M> {
|
||
|
#[serde(flatten)]
|
||
|
from: Pending<M>,
|
||
|
started_processing_at: DateTime<Utc>,
|
||
|
}
|
||
|
|
||
|
impl<M> Processing<M> {
|
||
|
pub fn id(&self) -> u64 {
|
||
|
self.from.id()
|
||
|
}
|
||
|
|
||
|
pub fn meta(&self) -> &M {
|
||
|
self.from.meta()
|
||
|
}
|
||
|
|
||
|
pub fn process<N>(self, meta: N) -> Processed<M, N> {
|
||
|
Processed {
|
||
|
success: meta,
|
||
|
from: self,
|
||
|
processed_at: Utc::now(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn fail<E>(self, error: E) -> Failed<M, E> {
|
||
|
Failed {
|
||
|
from: self,
|
||
|
error,
|
||
|
failed_at: Utc::now(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
|
||
|
pub struct Aborted<M> {
|
||
|
#[serde(flatten)]
|
||
|
from: Pending<M>,
|
||
|
aborted_at: DateTime<Utc>,
|
||
|
}
|
||
|
|
||
|
impl<M> Aborted<M> {
|
||
|
pub fn id(&self) -> u64 {
|
||
|
self.from.id()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
|
||
|
pub struct Failed<M, E> {
|
||
|
#[serde(flatten)]
|
||
|
from: Processing<M>,
|
||
|
error: E,
|
||
|
failed_at: DateTime<Utc>,
|
||
|
}
|
||
|
|
||
|
impl<M, E> Failed<M, E> {
|
||
|
pub fn id(&self) -> u64 {
|
||
|
self.from.id()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, PartialEq, Eq, Hash, Serialize)]
|
||
|
#[serde(tag = "status")]
|
||
|
pub enum UpdateStatus<M, N, E> {
|
||
|
Processing(Processing<M>),
|
||
|
Pending(Pending<M>),
|
||
|
Processed(Processed<M, N>),
|
||
|
Aborted(Aborted<M>),
|
||
|
Failed(Failed<M, E>),
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> UpdateStatus<M, N, E> {
|
||
|
pub fn id(&self) -> u64 {
|
||
|
match self {
|
||
|
UpdateStatus::Processing(u) => u.id(),
|
||
|
UpdateStatus::Pending(u) => u.id(),
|
||
|
UpdateStatus::Processed(u) => u.id(),
|
||
|
UpdateStatus::Aborted(u) => u.id(),
|
||
|
UpdateStatus::Failed(u) => u.id(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> From<Pending<M>> for UpdateStatus<M, N, E> {
|
||
|
fn from(other: Pending<M>) -> Self {
|
||
|
Self::Pending(other)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> From<Aborted<M>> for UpdateStatus<M, N, E> {
|
||
|
fn from(other: Aborted<M>) -> Self {
|
||
|
Self::Aborted(other)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> From<Processed<M, N>> for UpdateStatus<M, N, E> {
|
||
|
fn from(other: Processed<M, N>) -> Self {
|
||
|
Self::Processed(other)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> From<Processing<M>> for UpdateStatus<M, N, E> {
|
||
|
fn from(other: Processing<M>) -> Self {
|
||
|
Self::Processing(other)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<M, N, E> From<Failed<M, E>> for UpdateStatus<M, N, E> {
|
||
|
fn from(other: Failed<M, E>) -> Self {
|
||
|
Self::Failed(other)
|
||
|
}
|
||
|
}
|