/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL */ use crate::{ Depth, Timeout, responses::DeadPropertyFormat, schema::{ property::{ActiveLock, LockDiscovery, LockEntry, LockScope, LockType, SupportedLock}, request::LockInfo, response::{Href, List}, }, }; use std::fmt::Display; use types::dead_property::DeadProperty; impl Display for SupportedLock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl Display for LockDiscovery { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl Display for ActiveLock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}{}{}", self.lock_scope, self.lock_type, self.depth )?; if let Some(owner) = &self.owner { f.write_str("")?; owner.fmt(f)?; f.write_str("")?; } write!(f, "{}", self.timeout)?; if let Some(lock_token) = &self.lock_token { write!(f, "{}", lock_token)?; } write!( f, "{}", self.lock_root ) } } impl Display for Depth { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Depth::Zero => write!(f, "0"), Depth::One => write!(f, "1"), Depth::Infinity => write!(f, "infinity"), Depth::None => write!(f, ""), } } } impl Display for Timeout { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Timeout::Infinite => write!(f, "Infinite"), Timeout::Second(s) => write!(f, "Second-{}", s), Timeout::None => Ok(()), } } } impl Display for LockInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}{}", self.lock_scope, self.lock_type)?; if let Some(owner) = &self.owner { f.write_str("")?; owner.fmt(f)?; f.write_str("")?; } write!(f, "",) } } impl Display for LockEntry { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}{}", self.lock_scope, self.lock_type ) } } impl Display for LockScope { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { LockScope::Exclusive => write!(f, ""), LockScope::Shared => write!(f, ""), } } } impl Display for LockType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { LockType::Write => write!(f, ""), LockType::Other => write!(f, ""), } } } impl ActiveLock { pub fn new(href: impl Into, lock_scope: LockScope) -> Self { Self { lock_scope, lock_type: LockType::Write, depth: Depth::Infinity, owner: None, timeout: Timeout::Infinite, lock_token: None, lock_root: Href(href.into()), } } pub fn with_depth(mut self, depth: Depth) -> Self { self.depth = depth; self } pub fn with_timeout(mut self, timeout: u64) -> Self { self.timeout = Timeout::Second(timeout); self } pub fn with_owner_opt(mut self, owner: Option) -> Self { self.owner = owner; self } pub fn with_owner(mut self, owner: DeadProperty) -> Self { self.owner = Some(owner); self } pub fn with_lock_token(mut self, token: impl Into) -> Self { self.lock_token = Some(Href(token.into())); self } } impl Default for SupportedLock { fn default() -> Self { Self(List(vec![ LockEntry { lock_scope: LockScope::Exclusive, lock_type: LockType::Write, }, LockEntry { lock_scope: LockScope::Shared, lock_type: LockType::Write, }, ])) } }