hatchet-client 0.1.0

Rust SDK for Hatchet: run workers and trigger tasks
Documentation
//! Declaring tasks and workflows, and mapping them to the registration
//! request the engine expects.

pub(crate) mod condition;
mod idempotency;
mod options;
mod task;
mod workflow;

use std::time::Duration;

pub use condition::{Condition, ConditionSet, OrGroup, any_of};
pub(crate) use condition::{ConditionAction, Gates};
pub use idempotency::{Idempotency, IdempotencyMethod};
pub(crate) use options::saturating_i32;
pub use options::{
    BatchConfig, Concurrency, ConcurrencyLimitStrategy, DesiredLabel, LabelValue, Priority,
    RateLimit, RateLimitDuration, StickyStrategy,
};
pub(crate) use task::{RegisteredAction, TaskDef, TaskOptions, WorkflowOptions};
pub use task::{Task, TaskBuilder};
pub(crate) use workflow::WorkflowDef;
pub use workflow::{Register, Workflow};

use crate::config::namespace;

/// The engine takes timeouts as whole seconds with an `s` suffix.
pub(crate) fn timeout_string(timeout: Duration) -> String {
    format!("{}s", timeout.as_secs())
}

/// The workflow name as the engine knows it: lowercased, then prefixed with
/// the namespace unless it already carries it.
///
/// Lowercasing the whole *registered workflow name* here is an intentional
/// difference from the official Go and Python SDKs, which lowercase only the
/// action id (`workflow:task`) and leave the workflow name's case as
/// declared. This project's MVP acceptance criteria require it. Because of
/// that, triggering a workflow must always go through this same function
/// (or `WorkflowDef::wire_name`, which calls it) rather than the name as
/// declared, or the trigger will not match what was registered.
pub(crate) fn wire_name(namespace: &str, name: &str) -> String {
    namespace::apply(namespace, &name.to_lowercase())
}

/// `lower(<workflow>:<task>)`.
pub(crate) fn action_id(workflow_wire_name: &str, task_name: &str) -> String {
    format!("{workflow_wire_name}:{task_name}").to_lowercase()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn timeouts_are_whole_seconds_with_an_s_suffix() {
        assert_eq!(timeout_string(Duration::from_secs(2 * 3600)), "7200s");
        assert_eq!(timeout_string(Duration::from_millis(1500)), "1s");
        assert_eq!(timeout_string(Duration::ZERO), "0s");
    }
}