use core::fmt;
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
pub const REQUEST_ID: &str = "request_id";
pub const GOAL_ID: &str = "goal_id";
pub const GOAL_STATUS: &str = "goal_status";
pub const SESSION_ID: &str = "session_id";
pub const SEGMENT_ID: &str = "segment_id";
pub const SEQ: &str = "seq";
pub const FIN: &str = "fin";
pub const FLUSH: &str = "flush";
pub const SCHEMA_HASH: &str = "_schema_hash";
pub const INTERNAL_PREFIX: char = '_';
pub const WELL_KNOWN: &[&str] = &[
REQUEST_ID,
GOAL_ID,
GOAL_STATUS,
SESSION_ID,
SEGMENT_ID,
SEQ,
FIN,
FLUSH,
SCHEMA_HASH,
];
pub const CORRELATION: &[&str] = &[REQUEST_ID, GOAL_ID, GOAL_STATUS];
pub const STREAM: &[&str] = &[SESSION_ID, SEGMENT_ID, SEQ, FIN, FLUSH];
#[must_use]
pub fn is_internal_key(key: &str) -> bool {
key.starts_with(INTERNAL_PREFIX)
}
#[must_use]
pub fn is_correlation_key(key: &str) -> bool {
CORRELATION.contains(&key)
}
#[must_use]
pub fn is_stream_key(key: &str) -> bool {
STREAM.contains(&key)
}
#[must_use]
pub fn is_reserved_key(key: &str) -> bool {
is_internal_key(key) || WELL_KNOWN.contains(&key)
}
#[derive(
Debug,
Clone,
Copy,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
Encode,
Decode,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum GoalStatus {
#[default]
#[oxicode(variant = 0)]
Unknown,
#[oxicode(variant = 1)]
Accepted,
#[oxicode(variant = 2)]
Executing,
#[oxicode(variant = 3)]
Canceling,
#[oxicode(variant = 4)]
Succeeded,
#[oxicode(variant = 5)]
Canceled,
#[oxicode(variant = 6)]
Aborted,
}
impl GoalStatus {
pub const ALL: &'static [Self] = &[
Self::Unknown,
Self::Accepted,
Self::Executing,
Self::Canceling,
Self::Succeeded,
Self::Canceled,
Self::Aborted,
];
#[must_use]
pub const fn as_i64(self) -> i64 {
match self {
Self::Unknown => 0,
Self::Accepted => 1,
Self::Executing => 2,
Self::Canceling => 3,
Self::Succeeded => 4,
Self::Canceled => 5,
Self::Aborted => 6,
}
}
#[must_use]
pub const fn from_i64(code: i64) -> Option<Self> {
match code {
0 => Some(Self::Unknown),
1 => Some(Self::Accepted),
2 => Some(Self::Executing),
3 => Some(Self::Canceling),
4 => Some(Self::Succeeded),
5 => Some(Self::Canceled),
6 => Some(Self::Aborted),
_ => None,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Unknown => "unknown",
Self::Accepted => "accepted",
Self::Executing => "executing",
Self::Canceling => "canceling",
Self::Succeeded => "succeeded",
Self::Canceled => "canceled",
Self::Aborted => "aborted",
}
}
#[must_use]
pub const fn is_terminal(self) -> bool {
matches!(self, Self::Succeeded | Self::Canceled | Self::Aborted)
}
#[must_use]
pub const fn is_active(self) -> bool {
matches!(self, Self::Accepted | Self::Executing | Self::Canceling)
}
#[must_use]
pub const fn can_transition_to(self, next: Self) -> bool {
match self {
Self::Unknown => matches!(next, Self::Accepted),
Self::Accepted => matches!(
next,
Self::Executing
| Self::Canceling
| Self::Succeeded
| Self::Canceled
| Self::Aborted
),
Self::Executing => matches!(
next,
Self::Canceling | Self::Succeeded | Self::Canceled | Self::Aborted
),
Self::Canceling => matches!(next, Self::Canceled | Self::Succeeded | Self::Aborted),
Self::Succeeded | Self::Canceled | Self::Aborted => false,
}
}
}
impl fmt::Display for GoalStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<GoalStatus> for i64 {
fn from(status: GoalStatus) -> Self {
status.as_i64()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::codec::round_trip;
#[test]
fn well_known_keys_match_the_blueprint() {
assert_eq!(
WELL_KNOWN,
&[
"request_id",
"goal_id",
"goal_status",
"session_id",
"segment_id",
"seq",
"fin",
"flush",
"_schema_hash",
]
);
}
#[test]
fn well_known_keys_are_unique() {
let unique: std::collections::BTreeSet<_> = WELL_KNOWN.iter().collect();
assert_eq!(unique.len(), WELL_KNOWN.len());
}
#[test]
fn correlation_and_stream_sets_are_disjoint_subsets() {
for key in CORRELATION {
assert!(WELL_KNOWN.contains(key));
assert!(!STREAM.contains(key));
}
for key in STREAM {
assert!(WELL_KNOWN.contains(key));
assert!(!CORRELATION.contains(key));
}
}
#[test]
fn key_predicates_agree_with_the_sets() {
for key in WELL_KNOWN {
assert!(is_reserved_key(key), "{key}");
assert_eq!(is_correlation_key(key), CORRELATION.contains(key));
assert_eq!(is_stream_key(key), STREAM.contains(key));
}
assert!(is_internal_key(SCHEMA_HASH));
assert!(!is_internal_key(SEQ));
assert!(is_reserved_key("_future_internal_key"));
assert!(!is_reserved_key("user_key"));
assert!(!is_internal_key(""));
}
#[test]
fn goal_status_codes_match_ros2() {
assert_eq!(GoalStatus::Unknown.as_i64(), 0);
assert_eq!(GoalStatus::Accepted.as_i64(), 1);
assert_eq!(GoalStatus::Executing.as_i64(), 2);
assert_eq!(GoalStatus::Canceling.as_i64(), 3);
assert_eq!(GoalStatus::Succeeded.as_i64(), 4);
assert_eq!(GoalStatus::Canceled.as_i64(), 5);
assert_eq!(GoalStatus::Aborted.as_i64(), 6);
}
#[test]
fn numeric_form_round_trips_and_rejects_unknown_codes() {
for &status in GoalStatus::ALL {
assert_eq!(GoalStatus::from_i64(status.as_i64()), Some(status));
assert_eq!(i64::from(status), status.as_i64());
}
for code in [-2i64, -1, 7, 8, i64::MAX, i64::MIN] {
assert_eq!(GoalStatus::from_i64(code), None, "code {code}");
}
}
#[test]
fn terminal_and_active_partition_everything_but_unknown() {
for &status in GoalStatus::ALL {
assert!(!(status.is_terminal() && status.is_active()));
if status != GoalStatus::Unknown {
assert!(status.is_terminal() || status.is_active(), "{status}");
}
}
assert!(!GoalStatus::Unknown.is_terminal());
assert!(!GoalStatus::Unknown.is_active());
}
#[test]
fn terminal_states_have_no_successors() {
for status in GoalStatus::ALL.iter().copied().filter(|s| s.is_terminal()) {
for &next in GoalStatus::ALL {
assert!(
!status.can_transition_to(next),
"{status} must not move to {next}"
);
}
}
}
#[test]
fn the_documented_happy_path_is_legal() {
assert!(GoalStatus::Unknown.can_transition_to(GoalStatus::Accepted));
assert!(GoalStatus::Accepted.can_transition_to(GoalStatus::Executing));
assert!(GoalStatus::Executing.can_transition_to(GoalStatus::Succeeded));
assert!(GoalStatus::Executing.can_transition_to(GoalStatus::Aborted));
assert!(GoalStatus::Executing.can_transition_to(GoalStatus::Canceling));
assert!(GoalStatus::Canceling.can_transition_to(GoalStatus::Canceled));
}
#[test]
fn no_status_can_transition_to_itself() {
for &status in GoalStatus::ALL {
assert!(!status.can_transition_to(status), "{status}");
}
}
#[test]
fn names_are_unique_and_render() {
let mut seen = std::collections::BTreeSet::new();
for &status in GoalStatus::ALL {
assert!(seen.insert(status.as_str()));
assert_eq!(status.to_string(), status.as_str());
}
}
#[test]
fn codec_and_serde_round_trip() {
for &status in GoalStatus::ALL {
assert_eq!(round_trip(&status).unwrap(), status);
let json = serde_json::to_string(&status).unwrap();
assert_eq!(serde_json::from_str::<GoalStatus>(&json).unwrap(), status);
}
assert_eq!(GoalStatus::default(), GoalStatus::Unknown);
}
}