Skip to main content

rustvello_proto/status/
concurrency.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Concurrency control strategy for a task.
5///
6/// Determines how concurrent invocations of the same task are handled.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8#[non_exhaustive]
9pub enum ConcurrencyControlType {
10    /// No concurrency restrictions
11    Unlimited,
12    /// Only one invocation per (task_id, key_args) at a time
13    Task,
14    /// Only one invocation per exact argument set
15    Argument,
16    /// No concurrent invocations allowed at all
17    None,
18}
19
20impl Default for ConcurrencyControlType {
21    fn default() -> Self {
22        Self::Unlimited
23    }
24}
25
26impl fmt::Display for ConcurrencyControlType {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::Unlimited => f.write_str("Unlimited"),
30            Self::Task => f.write_str("Task"),
31            Self::Argument => f.write_str("Argument"),
32            Self::None => f.write_str("None"),
33        }
34    }
35}
36
37impl std::str::FromStr for ConcurrencyControlType {
38    type Err = String;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        match s.to_uppercase().as_str() {
42            "UNLIMITED" | "DISABLED" => Ok(Self::Unlimited),
43            "TASK" => Ok(Self::Task),
44            "ARGUMENT" | "ARGUMENTS" | "KEYS" => Ok(Self::Argument),
45            "NONE" => Ok(Self::None),
46            _ => Err(format!("unknown ConcurrencyControlType: {s:?}")),
47        }
48    }
49}