reduct-base 1.19.9

Base crate for ReductStore
Documentation
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0
use crate::msg::entry_api::EntryInfo;
use crate::msg::status::ResourceStatus;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;

/// Quota type
///
/// NONE: No quota
/// FIFO: When quota_size is reached, the oldest records are deleted
/// HARD: When quota_size is reached, no more records can be added
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub enum QuotaType {
    #[default]
    NONE = 0,
    FIFO = 1,
    HARD = 2,
}

impl From<i32> for QuotaType {
    fn from(value: i32) -> Self {
        match value {
            0 => QuotaType::NONE,
            1 => QuotaType::FIFO,
            2 => QuotaType::HARD,
            _ => 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),
            "HARD" => Ok(QuotaType::HARD),
            _ => 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"),
            QuotaType::HARD => write!(f, "HARD"),
        }
    }
}

/// Bucket settings
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct BucketSettings {
    /// Quota type see QuotaType
    pub quota_type: Option<QuotaType>,
    /// Quota size in bytes
    pub quota_size: Option<u64>,
    /// Max size of a block in bytes to start a new one
    pub max_block_size: Option<u64>,
    /// Max records in a block to start a new block one
    pub max_block_records: Option<u64>,
}

/// Bucket information
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct BucketInfo {
    /// Unique bucket name
    pub name: String,
    /// Number of records in bucket
    pub entry_count: u64,
    /// Total size of bucket in bytes
    pub size: u64,
    /// Oldest record in bucket
    pub oldest_record: u64,
    /// Latest record in bucket
    pub latest_record: u64,
    /// Provisioned
    pub is_provisioned: bool,
    /// Status of the bucket
    #[serde(default)]
    pub status: ResourceStatus,
}

/// Full bucket information
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct FullBucketInfo {
    /// Bucket information
    pub info: BucketInfo,
    /// Bucket settings
    pub settings: BucketSettings,
    /// Entries in bucket
    pub entries: Vec<EntryInfo>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::rstest;

    #[rstest]
    #[case(QuotaType::NONE, "NONE")]
    #[case(QuotaType::FIFO, "FIFO")]
    #[case(QuotaType::HARD, "HARD")]
    fn test_enum_as_string(#[case] quota_type: QuotaType, #[case] expected: &str) {
        let settings = BucketSettings {
            quota_type: Some(quota_type),
            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,
            format!("{{\"quota_type\":\"{}\",\"quota_size\":100,\"max_block_size\":100,\"max_block_records\":100}}", expected)
        );
    }

    #[rstest]
    #[case(QuotaType::NONE, "NONE")]
    #[case(QuotaType::FIFO, "FIFO")]
    #[case(QuotaType::HARD, "HARD")]
    fn test_quota_from_string(#[case] expected: QuotaType, #[case] as_string: &str) {
        assert_eq!(QuotaType::from_str(as_string).unwrap(), expected);
    }
}

#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct RenameBucket {
    /// New bucket name
    pub new_name: String,
}