use crate::models;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResultStatus {
Pending,
Processing,
Ready,
Failed,
Other(String),
}
impl ResultStatus {
pub fn parse(s: &str) -> Self {
match s {
"pending" => ResultStatus::Pending,
"processing" => ResultStatus::Processing,
"ready" => ResultStatus::Ready,
"failed" => ResultStatus::Failed,
other => ResultStatus::Other(other.to_owned()),
}
}
pub fn as_str(&self) -> &str {
match self {
ResultStatus::Pending => "pending",
ResultStatus::Processing => "processing",
ResultStatus::Ready => "ready",
ResultStatus::Failed => "failed",
ResultStatus::Other(s) => s,
}
}
pub fn is_ready(&self) -> bool {
matches!(self, ResultStatus::Ready)
}
pub fn is_failed(&self) -> bool {
matches!(self, ResultStatus::Failed)
}
}
impl std::fmt::Display for ResultStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for ResultStatus {
fn from(s: &str) -> Self {
ResultStatus::parse(s)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryRunStatus {
Running,
Succeeded,
Failed,
Other(String),
}
impl QueryRunStatus {
pub fn parse(s: &str) -> Self {
match s {
"running" => QueryRunStatus::Running,
"succeeded" => QueryRunStatus::Succeeded,
"failed" => QueryRunStatus::Failed,
other => QueryRunStatus::Other(other.to_owned()),
}
}
pub fn as_str(&self) -> &str {
match self {
QueryRunStatus::Running => "running",
QueryRunStatus::Succeeded => "succeeded",
QueryRunStatus::Failed => "failed",
QueryRunStatus::Other(s) => s,
}
}
pub fn is_terminal(&self) -> bool {
matches!(self, QueryRunStatus::Succeeded | QueryRunStatus::Failed)
}
pub fn is_succeeded(&self) -> bool {
matches!(self, QueryRunStatus::Succeeded)
}
}
impl std::fmt::Display for QueryRunStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for QueryRunStatus {
fn from(s: &str) -> Self {
QueryRunStatus::parse(s)
}
}
pub trait ResultStatusExt {
fn result_status(&self) -> ResultStatus;
}
impl ResultStatusExt for models::GetResultResponse {
fn result_status(&self) -> ResultStatus {
ResultStatus::parse(&self.status)
}
}
impl ResultStatusExt for models::ResultInfo {
fn result_status(&self) -> ResultStatus {
ResultStatus::parse(&self.status)
}
}
pub trait QueryRunStatusExt {
fn run_status(&self) -> QueryRunStatus;
}
impl QueryRunStatusExt for models::QueryRunInfo {
fn run_status(&self) -> QueryRunStatus {
QueryRunStatus::parse(&self.status)
}
}
impl QueryRunStatusExt for models::AsyncQueryResponse {
fn run_status(&self) -> QueryRunStatus {
QueryRunStatus::parse(&self.status)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn result_status_round_trips_known_values() {
for s in ["pending", "processing", "ready", "failed"] {
assert_eq!(ResultStatus::parse(s).as_str(), s);
}
assert!(ResultStatus::parse("ready").is_ready());
assert!(ResultStatus::parse("failed").is_failed());
}
#[test]
fn result_status_preserves_unknown() {
let s = ResultStatus::parse("quantum");
assert_eq!(s, ResultStatus::Other("quantum".to_owned()));
assert_eq!(s.as_str(), "quantum");
assert!(!s.is_ready() && !s.is_failed());
}
#[test]
fn query_run_status_terminality() {
assert!(QueryRunStatus::parse("succeeded").is_terminal());
assert!(QueryRunStatus::parse("failed").is_terminal());
assert!(!QueryRunStatus::parse("running").is_terminal());
assert!(!QueryRunStatus::parse("paused").is_terminal());
assert!(QueryRunStatus::parse("succeeded").is_succeeded());
}
#[test]
fn from_str_impls() {
assert_eq!(ResultStatus::from("ready"), ResultStatus::Ready);
assert_eq!(QueryRunStatus::from("running"), QueryRunStatus::Running);
}
}