use std::{fmt::Display, str::FromStr};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::ByteSize;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum HistoryKind {
Build,
Collection,
Hook,
Quota,
Recovery,
}
impl Display for HistoryKind {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Build => "build",
Self::Collection => "collection",
Self::Hook => "hook",
Self::Quota => "quota",
Self::Recovery => "recovery",
})
}
}
impl FromStr for HistoryKind {
type Err = ParseHistoryKindError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"build" => Ok(Self::Build),
"collection" => Ok(Self::Collection),
"hook" => Ok(Self::Hook),
"quota" => Ok(Self::Quota),
"recovery" => Ok(Self::Recovery),
_ => Err(ParseHistoryKindError(value.to_owned())),
}
}
}
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("unknown history kind `{0}`; expected build, collection, hook, quota, or recovery")]
pub struct ParseHistoryKindError(String);
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct HistoryPolicy {
pub enabled: bool,
pub max_receipts: u64,
pub max_bytes: ByteSize,
}
impl Default for HistoryPolicy {
fn default() -> Self {
Self {
enabled: true,
max_receipts: 10_000,
max_bytes: ByteSize::from_bytes(64 * 1_024 * 1_024),
}
}
}