use crate::msg::entry_api::EntryInfo;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub enum QuotaType {
#[default]
NONE = 0,
FIFO = 1,
}
impl From<i32> for QuotaType {
fn from(value: i32) -> Self {
match value {
0 => QuotaType::NONE,
1 => QuotaType::FIFO,
_ => QuotaType::NONE,
}
}
}
impl FromStr for QuotaType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"NONE" => Ok(QuotaType::NONE),
"FIFO" => Ok(QuotaType::FIFO),
_ => Err(()),
}
}
}
impl Display for QuotaType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
QuotaType::NONE => write!(f, "NONE"),
QuotaType::FIFO => write!(f, "FIFO"),
}
}
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct BucketSettings {
pub quota_type: Option<QuotaType>,
pub quota_size: Option<u64>,
pub max_block_size: Option<u64>,
pub max_block_records: Option<u64>,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct BucketInfo {
pub name: String,
pub entry_count: u64,
pub size: u64,
pub oldest_record: u64,
pub latest_record: u64,
pub is_provisioned: bool,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct FullBucketInfo {
pub info: BucketInfo,
pub settings: BucketSettings,
pub entries: Vec<EntryInfo>,
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
fn test_enum_as_string() {
let settings = BucketSettings {
quota_type: Some(QuotaType::FIFO),
quota_size: Some(100),
max_block_size: Some(100),
max_block_records: Some(100),
};
let serialized = serde_json::to_string(&settings).unwrap();
assert_eq!(
serialized,
r#"{"quota_type":"FIFO","quota_size":100,"max_block_size":100,"max_block_records":100}"#
);
}
}