use serde::{Deserialize, Serialize};
pub const MIB: u64 = 1024 * 1024;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContentPolicy {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_bytes: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub member_bytes: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hourly_bytes: Option<u64>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct KeptLimits {
pub file_bytes: Option<u64>,
pub member_bytes: Option<u64>,
pub hourly_bytes: Option<u64>,
pub workspace_bytes: Option<u64>,
}
impl KeptLimits {
#[must_use]
pub fn of(policy: &ContentPolicy, workspace_bytes: Option<u64>) -> Self {
Self {
file_bytes: policy.file_bytes,
member_bytes: policy.member_bytes,
hourly_bytes: policy.hourly_bytes,
workspace_bytes,
}
}
#[must_use]
pub fn allowed(&self, limit: ContentLimit) -> Option<u64> {
match limit {
ContentLimit::File => self.file_bytes,
ContentLimit::Member => self.member_bytes,
ContentLimit::Hourly => self.hourly_bytes,
ContentLimit::Workspace => self.workspace_bytes,
}
}
#[must_use]
pub fn allows(&self, limit: ContentLimit, bytes: u64) -> bool {
self.allowed(limit).is_none_or(|allowed| bytes <= allowed)
}
#[must_use]
pub fn refuses(&self, limit: ContentLimit, bytes: u64) -> bool {
match limit {
ContentLimit::File => !self.allows(limit, bytes),
ContentLimit::Member | ContentLimit::Hourly | ContentLimit::Workspace => true,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContentLimit {
File,
Member,
Hourly,
Workspace,
}
impl ContentLimit {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::File => "file size",
Self::Member => "a member's share",
Self::Hourly => "a member's hourly intake",
Self::Workspace => "the workspace's kept content",
}
}
#[must_use]
pub fn parse(name: &str) -> Option<Self> {
match name {
"file" => Some(Self::File),
"member" => Some(Self::Member),
"hourly" => Some(Self::Hourly),
"workspace" => Some(Self::Workspace),
_ => None,
}
}
#[must_use]
pub const fn key(self) -> &'static str {
match self {
Self::File => "file",
Self::Member => "member",
Self::Hourly => "hourly",
Self::Workspace => "workspace",
}
}
#[must_use]
pub const fn raises(self) -> Raises {
match self {
Self::File | Self::Member | Self::Hourly => Raises::Owners,
Self::Workspace => Raises::Host,
}
}
#[must_use]
pub const fn raise(self) -> &'static str {
self.raises().who()
}
#[must_use]
pub fn refusal(self, allowed: u64, bytes: u64) -> String {
format!(
"{bytes} bytes is over the {name} of {allowed} bytes: {raise}",
name = self.name(),
raise = self.raise(),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Raises {
Owners,
Host,
}
impl Raises {
#[must_use]
pub const fn who(self) -> &'static str {
match self {
Self::Owners => "the workspace's owners, in its content policy",
Self::Host => "the host's person, in its server's settings",
}
}
#[must_use]
pub const fn key(self) -> &'static str {
match self {
Self::Owners => "owners",
Self::Host => "host",
}
}
#[must_use]
pub fn parse(name: &str) -> Option<Self> {
match name {
"owners" => Some(Self::Owners),
"host" => Some(Self::Host),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_limit_is_set_by_default_and_none_of_them_bounds_any_size() {
let limits = KeptLimits::default();
assert_eq!(limits.file_bytes, None);
assert_eq!(limits.member_bytes, None);
assert_eq!(limits.hourly_bytes, None);
assert_eq!(limits.workspace_bytes, None);
for limit in [
ContentLimit::File,
ContentLimit::Member,
ContentLimit::Hourly,
ContentLimit::Workspace,
] {
assert_eq!(limits.allowed(limit), None, "{limit:?}");
assert!(limits.allows(limit, 3 * 1024 * MIB), "{limit:?}");
}
}
#[test]
fn a_policy_written_before_a_limit_was_optional_takes_no_limit_for_the_ones_it_left_out() {
let stored: ContentPolicy = serde_json::from_str(r#"{"file_bytes": 1024}"#).unwrap();
assert_eq!(stored.file_bytes, Some(1024));
assert_eq!(stored.member_bytes, None);
assert_eq!(stored.hourly_bytes, None);
}
#[test]
fn a_limit_the_owners_set_still_refuses_content_past_it() {
let limits = KeptLimits::of(
&ContentPolicy {
file_bytes: Some(1024),
..ContentPolicy::default()
},
Some(2048),
);
assert!(!limits.allows(ContentLimit::File, 2048));
assert!(limits.allows(ContentLimit::File, 1024));
assert!(limits.allows(ContentLimit::Member, u64::MAX / 2));
assert!(!limits.allows(ContentLimit::Workspace, 2049));
}
#[test]
fn a_refusal_stands_while_what_the_record_alone_says_refuses_it() {
let limits = KeptLimits::of(
&ContentPolicy {
file_bytes: Some(1_024),
member_bytes: Some(2_048),
hourly_bytes: Some(4_096),
},
None,
);
assert!(limits.refuses(ContentLimit::File, 2_048));
assert!(!limits.refuses(ContentLimit::File, 1_024));
assert!(limits.refuses(ContentLimit::Member, 1));
assert!(limits.refuses(ContentLimit::Hourly, 1));
assert!(limits.refuses(ContentLimit::Workspace, 1));
}
#[test]
fn a_refusal_names_the_limit_what_it_allowed_and_who_raises_it() {
let refusal = ContentLimit::File.refusal(1024, 2048);
assert!(refusal.contains("file size"), "{refusal}");
assert!(refusal.contains("1024"), "{refusal}");
assert!(refusal.contains("2048"), "{refusal}");
assert!(refusal.contains("workspace's owners"), "{refusal}");
assert!(
ContentLimit::Workspace
.refusal(1, 2)
.contains("host's person")
);
}
}