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::{collections::BTreeMap, num::NonZeroU32, time::Duration};
pub(crate) use crate::runs::INLINE_WAIT_SECONDS as MAX_WAIT_SECONDS;
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,
#[serde(default)]
pub profile_variables: BTreeMap<String, Option<String>>,
}
impl Start {
pub(crate) fn fingerprint(&self) -> String {
let args = (
self.scope.selection(),
self.comment.as_ref(),
self.jobs,
self.speculate,
);
let bytes = if self.profile_variables.is_empty() {
serde_json::to_vec(&args)
} else {
serde_json::to_vec(&(args, &self.profile_variables))
}
.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>,
#[serde(default)]
pub summary: bool,
#[serde(default)]
pub wait_for_action: bool,
}
impl Show {
pub(crate) fn validate(&self) -> Result<(), ArgumentsError> {
if self.wait_for_action && self.seconds.is_some() {
return Err(ArgumentsError::ConflictingWait);
}
Ok(())
}
pub(crate) fn is_summary(&self) -> bool {
self.summary || self.wait_for_action
}
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)))
}
}