use super::{Run, Scope, decode};
use crate::{
types::{RequestKey, RunComment, RunId, Sha256Digest},
verify_tools::{error::ArgumentsError, tool::VerifyTool},
};
use serde::Deserialize;
use serde_json::{Map, Value};
use std::{num::NonZeroU32, time::Duration};
pub(crate) const MAX_WAIT_SECONDS: u64 = 40;
pub(crate) enum Verify {
Start(Start),
Cancel(RunId),
}
impl Verify {
pub(super) fn parse(
tool: VerifyTool,
arguments: Map<String, Value>,
) -> Result<Self, ArgumentsError> {
Ok(match tool {
VerifyTool::Start => Self::Start(decode(arguments)?),
VerifyTool::Cancel => Self::Cancel(decode::<Run>(arguments)?.run),
})
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Start {
pub scope: Scope,
pub request_key: Option<RequestKey>,
pub comment: Option<RunComment>,
pub jobs: Option<NonZeroU32>,
#[serde(default)]
pub speculate: bool,
}
impl Start {
pub(crate) fn fingerprint(&self) -> String {
let bytes = serde_json::to_vec(&(
self.scope.selection(),
self.comment.as_ref(),
self.jobs,
self.speculate,
))
.expect("typed Verify arguments serialize");
Sha256Digest::of(&bytes).to_string()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Show {
pub run: RunId,
seconds: Option<WaitBound>,
}
impl Show {
pub(crate) fn duration(&self) -> Duration {
self.seconds
.as_ref()
.map_or(Duration::ZERO, |bound| bound.0)
}
}
#[derive(Deserialize)]
#[serde(try_from = "u64")]
struct WaitBound(Duration);
impl TryFrom<u64> for WaitBound {
type Error = String;
fn try_from(seconds: u64) -> Result<Self, Self::Error> {
if seconds > MAX_WAIT_SECONDS {
return Err(format!(
"seconds must be an integer from 0 through {MAX_WAIT_SECONDS}."
));
}
Ok(Self(Duration::from_secs(seconds)))
}
}