mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-01-18 08:48:32 +08:00
Add custom TaskStatusError for TaskStatus
This commit is contained in:
parent
80c156df3f
commit
0e7e16ae72
@ -94,10 +94,28 @@ pub enum TaskStatus {
|
|||||||
Failed,
|
Failed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for TaskStatus {
|
#[derive(Debug)]
|
||||||
type Err = String;
|
pub struct TaskStatusError {
|
||||||
|
invalid_status: String,
|
||||||
|
}
|
||||||
|
|
||||||
fn from_str(status: &str) -> Result<Self, String> {
|
impl fmt::Display for TaskStatusError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"invalid task status `{}`, expecting one of: \
|
||||||
|
enqueued, processing, succeeded, or failed",
|
||||||
|
self.invalid_status,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for TaskStatusError {}
|
||||||
|
|
||||||
|
impl FromStr for TaskStatus {
|
||||||
|
type Err = TaskStatusError;
|
||||||
|
|
||||||
|
fn from_str(status: &str) -> Result<Self, TaskStatusError> {
|
||||||
if status.eq_ignore_ascii_case("enqueued") {
|
if status.eq_ignore_ascii_case("enqueued") {
|
||||||
Ok(TaskStatus::Enqueued)
|
Ok(TaskStatus::Enqueued)
|
||||||
} else if status.eq_ignore_ascii_case("processing") {
|
} else if status.eq_ignore_ascii_case("processing") {
|
||||||
@ -107,11 +125,9 @@ impl FromStr for TaskStatus {
|
|||||||
} else if status.eq_ignore_ascii_case("failed") {
|
} else if status.eq_ignore_ascii_case("failed") {
|
||||||
Ok(TaskStatus::Failed)
|
Ok(TaskStatus::Failed)
|
||||||
} else {
|
} else {
|
||||||
Err(format!(
|
Err(TaskStatusError {
|
||||||
"invalid task status `{}`, expecting one of: \
|
invalid_status: status.to_string(),
|
||||||
enqueued, processing, succeeded, or failed",
|
})
|
||||||
status,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user