Skip to main content

reduct_base/msg/
bucket_api.rs

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