Skip to main content

ora/
common.rs

1//! Common types and utilities.
2
3use std::time::SystemTime;
4
5use serde::{Deserialize, Serialize};
6
7/// A label associated with an entity.
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9pub struct Label {
10    /// The key of the label.
11    pub key: String,
12    /// The value of the label.
13    pub value: String,
14}
15
16/// A filter for labels.
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct LabelFilter {
19    /// The key of the label to filter by.
20    pub key: String,
21    /// An optional value of the label to filter by,
22    /// if not specified, entities with
23    /// the given key and any value will match.
24    pub value: Option<String>,
25    /// An optional set of values of the label to filter by,
26    /// entities with any of the values will match.
27    ///
28    /// Takes precedence over `value`, an empty set matches nothing.
29    #[serde(default)]
30    pub value_in: Option<Vec<String>>,
31}
32
33/// A time range that can be open-ended on either side.
34///
35/// The end of the range is exclusive.
36#[derive(Debug, Default, Clone, Serialize, Deserialize)]
37pub struct TimeRange {
38    /// The start of the time range.
39    ///
40    /// If `None`, the range is open-ended at the start.
41    pub start: Option<SystemTime>,
42    /// The end of the time range.
43    ///
44    /// If `None`, the range is open-ended at the end.
45    pub end: Option<SystemTime>,
46}
47
48/// A type that is returned from `if_not_exists` requests,
49/// a resource was either added or existing ones are returned.
50pub enum AddedOrExisting<T> {
51    /// The resource was added.
52    Added(T),
53    /// The resource already existed.
54    Existing(T),
55}
56
57impl<T> AddedOrExisting<T> {
58    /// Unwrap the inner value.
59    #[must_use]
60    pub fn into_inner(self) -> T {
61        match self {
62            AddedOrExisting::Added(job) | AddedOrExisting::Existing(job) => job,
63        }
64    }
65
66    /// Return whether the job was added or already existed.
67    #[must_use]
68    pub fn is_added(&self) -> bool {
69        matches!(self, AddedOrExisting::Added(_))
70    }
71
72    /// Return whether the job already existed.
73    #[must_use]
74    pub fn is_existing(&self) -> bool {
75        matches!(self, AddedOrExisting::Existing(_))
76    }
77}