Skip to main content

reduct_base/msg/
bucket_api.rs

1// Copyright 2021-2026 ReductSoftware UG
2// Licensed under the Apache License, Version 2.0
3use crate::msg::entry_api::EntryInfo;
4use crate::msg::status::ResourceStatus;
5use serde::{Deserialize, Serialize};
6use std::fmt::Display;
7use std::str::FromStr;
8
9/// Quota type
10///
11/// NONE: No quota
12/// FIFO: When quota_size is reached, the oldest records are deleted
13/// HARD: When quota_size is reached, no more records can be added
14#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
15pub enum QuotaType {
16    #[default]
17    NONE = 0,
18    FIFO = 1,
19    HARD = 2,
20}
21
22impl From<i32> for QuotaType {
23    fn from(value: i32) -> Self {
24        match value {
25            0 => QuotaType::NONE,
26            1 => QuotaType::FIFO,
27            2 => QuotaType::HARD,
28            _ => QuotaType::NONE,
29        }
30    }
31}
32
33impl FromStr for QuotaType {
34    type Err = ();
35
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s {
38            "NONE" => Ok(QuotaType::NONE),
39            "FIFO" => Ok(QuotaType::FIFO),
40            "HARD" => Ok(QuotaType::HARD),
41            _ => Err(()),
42        }
43    }
44}
45
46impl Display for QuotaType {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        match self {
49            QuotaType::NONE => write!(f, "NONE"),
50            QuotaType::FIFO => write!(f, "FIFO"),
51            QuotaType::HARD => write!(f, "HARD"),
52        }
53    }
54}
55
56/// Bucket settings
57#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
58pub struct BucketSettings {
59    /// Quota type see QuotaType
60    pub quota_type: Option<QuotaType>,
61    /// Quota size in bytes
62    pub quota_size: Option<u64>,
63    /// Max size of a block in bytes to start a new one
64    pub max_block_size: Option<u64>,
65    /// Max records in a block to start a new block one
66    pub max_block_records: Option<u64>,
67}
68
69/// Bucket information
70#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
71pub struct BucketInfo {
72    /// Unique bucket name
73    pub name: String,
74    /// Number of records in bucket
75    pub entry_count: u64,
76    /// Total size of bucket in bytes
77    pub size: u64,
78    /// Oldest record in bucket
79    pub oldest_record: u64,
80    /// Latest record in bucket
81    pub latest_record: u64,
82    /// Provisioned
83    pub is_provisioned: bool,
84    /// Status of the bucket
85    #[serde(default)]
86    pub status: ResourceStatus,
87}
88
89/// Full bucket information
90#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
91pub struct FullBucketInfo {
92    /// Bucket information
93    pub info: BucketInfo,
94    /// Bucket settings
95    pub settings: BucketSettings,
96    /// Entries in bucket
97    pub entries: Vec<EntryInfo>,
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use rstest::rstest;
104
105    #[rstest]
106    #[case(QuotaType::NONE, "NONE")]
107    #[case(QuotaType::FIFO, "FIFO")]
108    #[case(QuotaType::HARD, "HARD")]
109    fn test_enum_as_string(#[case] quota_type: QuotaType, #[case] expected: &str) {
110        let settings = BucketSettings {
111            quota_type: Some(quota_type),
112            quota_size: Some(100),
113            max_block_size: Some(100),
114            max_block_records: Some(100),
115        };
116        let serialized = serde_json::to_string(&settings).unwrap();
117
118        assert_eq!(
119            serialized,
120            format!("{{\"quota_type\":\"{}\",\"quota_size\":100,\"max_block_size\":100,\"max_block_records\":100}}", expected)
121        );
122    }
123
124    #[rstest]
125    #[case(QuotaType::NONE, "NONE")]
126    #[case(QuotaType::FIFO, "FIFO")]
127    #[case(QuotaType::HARD, "HARD")]
128    fn test_quota_from_string(#[case] expected: QuotaType, #[case] as_string: &str) {
129        assert_eq!(QuotaType::from_str(as_string).unwrap(), expected);
130    }
131}
132
133#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
134pub struct RenameBucket {
135    /// New bucket name
136    pub new_name: String,
137}