use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::neo_fs::types::{Attributes, ContainerId, OwnerId, PlacementPolicy};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version {
pub major: u32,
pub minor: u32,
}
impl Default for Version {
fn default() -> Self {
Self { major: 1, minor: 0 }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Container {
pub id: Option<ContainerId>,
pub owner_id: OwnerId,
pub basic_acl: u32,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(with = "chrono::serde::ts_seconds_option")]
pub creation: Option<DateTime<Utc>>,
pub version: Option<Version>,
pub attributes: Attributes,
pub placement_policy: PlacementPolicy,
}
impl Container {
pub fn new(id: ContainerId, owner_id: OwnerId) -> Self {
Self {
id: Some(id),
owner_id,
basic_acl: 0,
name: String::new(),
creation: None,
version: Some(Version::default()),
attributes: Attributes::new(),
placement_policy: Default::default(),
}
}
pub fn with_basic_acl(mut self, acl: u32) -> Self {
self.basic_acl = acl;
self
}
pub fn with_name(mut self, name: String) -> Self {
self.name = name;
self
}
pub fn with_creation(mut self, creation: DateTime<Utc>) -> Self {
self.creation = Some(creation);
self
}
pub fn with_version(mut self, version: Version) -> Self {
self.version = Some(version);
self
}
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.add(key, value);
self
}
}
#[derive(Debug, Clone)]
pub struct BasicACL {
pub put_allowed: bool,
pub get_allowed: bool,
pub head_allowed: bool,
pub delete_allowed: bool,
pub list_allowed: bool,
}
impl BasicACL {
pub fn full_access() -> Self {
Self {
put_allowed: true,
get_allowed: true,
head_allowed: true,
delete_allowed: true,
list_allowed: true,
}
}
pub fn read_only() -> Self {
Self {
put_allowed: false,
get_allowed: true,
head_allowed: true,
delete_allowed: false,
list_allowed: true,
}
}
pub fn to_bitmask(&self) -> u32 {
let mut bitmask = 0;
if self.put_allowed {
bitmask |= 0b00001;
}
if self.get_allowed {
bitmask |= 0b00010;
}
if self.head_allowed {
bitmask |= 0b00100;
}
if self.delete_allowed {
bitmask |= 0b01000;
}
if self.list_allowed {
bitmask |= 0b10000;
}
bitmask
}
pub fn from_bitmask(bitmask: u32) -> Self {
Self {
put_allowed: (bitmask & 0b00001) != 0,
get_allowed: (bitmask & 0b00010) != 0,
head_allowed: (bitmask & 0b00100) != 0,
delete_allowed: (bitmask & 0b01000) != 0,
list_allowed: (bitmask & 0b10000) != 0,
}
}
}