JMAP Registry API implementation - part 1

This commit is contained in:
mdecimus
2026-02-22 19:41:56 +01:00
parent f2c2ad6748
commit ec148c5550
21 changed files with 733 additions and 220 deletions

View File

@@ -0,0 +1,60 @@
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use crate::{
schema::prelude::Property,
types::{error::PatchError, string::StringValidator},
};
use jmap_tools::{JsonPointer, Value};
use std::fmt::Debug;
use types::{blob::BlobId, id::Id};
pub mod patch;
pub mod properties;
pub mod ser;
pub type JmapValue<'x> = Value<'x, Property, RegistryValue>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RegistryValue {
Id(Id),
BlobId(BlobId),
IdReference(String),
}
#[derive(Debug, Clone)]
pub struct JsonPointerPatch<'x> {
ptr: &'x JsonPointer<Property>,
pos: usize,
validators: &'x [StringValidator],
}
pub trait RegistryJsonPatch: Debug + Default {
fn patch(
&mut self,
pointer: JsonPointerPatch<'_>,
value: JmapValue<'_>,
) -> Result<(), PatchError>;
}
pub trait RegistryJsonPropertyPatch: Debug + Default {
fn patch_property(
&mut self,
pointer: JsonPointerPatch<'_>,
value: JmapValue<'_>,
) -> Result<(), PatchError>;
}
pub trait RegistryJsonEnumPatch: Debug {
fn patch(
&mut self,
pointer: JsonPointerPatch<'_>,
value: JmapValue<'_>,
) -> Result<(), PatchError>;
}
pub trait IntoValue {
fn into_value(self) -> JmapValue<'static>;
}

View File

@@ -5,6 +5,10 @@
*/
use crate::{
jmap::{
JsonPointerPatch, RegistryJsonEnumPatch, RegistryJsonPatch, RegistryJsonPropertyPatch,
RegistryValue,
},
schema::prelude::Property,
types::{
EnumImpl,
@@ -13,47 +17,9 @@ use crate::{
},
};
use jmap_tools::{JsonPointer, JsonPointerItem, Key, Value};
use std::{borrow::Cow, fmt::Debug, str::FromStr};
use types::{blob::BlobId, id::Id};
use std::fmt::Debug;
use utils::map::vec_map::VecMap;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RegistryValue {
Id(Id),
BlobId(BlobId),
IdReference(String),
}
#[derive(Debug, Clone)]
pub struct JsonPointerPatch<'x> {
ptr: &'x JsonPointer<Property>,
pos: usize,
validators: &'x [StringValidator],
}
pub trait RegistryJsonPatch: Debug + Default {
fn patch(
&mut self,
pointer: JsonPointerPatch<'_>,
value: Value<'_, Property, RegistryValue>,
) -> Result<(), PatchError>;
}
pub trait RegistryJsonPropertyPatch: Debug + Default {
fn patch_property(
&mut self,
pointer: JsonPointerPatch<'_>,
value: Value<'_, Property, RegistryValue>,
) -> Result<(), PatchError>;
}
pub trait RegistryJsonEnumPatch: Debug {
fn patch(
&mut self,
pointer: JsonPointerPatch<'_>,
value: Value<'_, Property, RegistryValue>,
) -> Result<(), PatchError>;
}
impl<'x> JsonPointerPatch<'x> {
pub fn new(ptr: &'x JsonPointer<Property>) -> Self {
Self {
@@ -107,86 +73,6 @@ impl<'x> JsonPointerPatch<'x> {
}
}
impl jmap_tools::Property for Property {
fn try_parse(_: Option<&Key<'_, Self>>, value: &str) -> Option<Self> {
Property::parse(value)
}
fn to_cow(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
impl FromStr for Property {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Property::parse(s).ok_or(())
}
}
impl jmap_tools::Element for RegistryValue {
type Property = Property;
fn try_parse<P>(key: &Key<'_, Self::Property>, value: &str) -> Option<Self> {
if let Key::Property(prop) = key {
match prop {
Property::Id
| Property::MemberGroupIds
| Property::MemberTenantId
| Property::RoleIds
| Property::DnsServerId
| Property::DirectoryId
| Property::DomainId
| Property::AccountId
| Property::DefaultDomainId
| Property::DefaultUserRoleIds
| Property::DefaultGroupRoleIds
| Property::DefaultTenantRoleIds
| Property::QueueId
| Property::ModelId
| Property::AcmeProviderId => {
if let Some(reference) = value.strip_prefix('#') {
Some(RegistryValue::IdReference(reference.to_string()))
} else {
Id::from_str(value).map(RegistryValue::Id).ok()
}
}
Property::BlobId => {
if let Some(reference) = value.strip_prefix('#') {
Some(RegistryValue::IdReference(reference.to_string()))
} else {
BlobId::from_str(value).map(RegistryValue::BlobId).ok()
}
}
_ => None,
}
} else {
None
}
}
fn to_cow(&self) -> Cow<'static, str> {
match self {
RegistryValue::Id(id) => id.to_string().into(),
RegistryValue::BlobId(blob_id) => blob_id.to_string().into(),
RegistryValue::IdReference(r) => format!("#{r}").into(),
}
}
}
impl From<Id> for RegistryValue {
fn from(id: Id) -> Self {
RegistryValue::Id(id)
}
}
impl From<BlobId> for RegistryValue {
fn from(id: BlobId) -> Self {
RegistryValue::BlobId(id)
}
}
impl<T: RegistryJsonPatch> RegistryJsonPatch for Option<T> {
fn patch(
&mut self,

View File

@@ -0,0 +1,90 @@
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use crate::{jmap::RegistryValue, schema::prelude::Property, types::EnumImpl};
use jmap_tools::Key;
use std::{borrow::Cow, str::FromStr};
use types::{blob::BlobId, id::Id};
impl jmap_tools::Property for Property {
fn try_parse(_: Option<&Key<'_, Self>>, value: &str) -> Option<Self> {
Property::parse(value)
}
fn to_cow(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
impl FromStr for Property {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Property::parse(s).ok_or(())
}
}
impl jmap_tools::Element for RegistryValue {
type Property = Property;
fn try_parse<P>(key: &Key<'_, Self::Property>, value: &str) -> Option<Self> {
if let Key::Property(prop) = key {
match prop {
Property::Id
| Property::MemberGroupIds
| Property::MemberTenantId
| Property::RoleIds
| Property::DnsServerId
| Property::DirectoryId
| Property::DomainId
| Property::AccountId
| Property::DefaultDomainId
| Property::DefaultUserRoleIds
| Property::DefaultGroupRoleIds
| Property::DefaultTenantRoleIds
| Property::QueueId
| Property::ModelId
| Property::AcmeProviderId => {
if let Some(reference) = value.strip_prefix('#') {
Some(RegistryValue::IdReference(reference.to_string()))
} else {
Id::from_str(value).map(RegistryValue::Id).ok()
}
}
Property::BlobId => {
if let Some(reference) = value.strip_prefix('#') {
Some(RegistryValue::IdReference(reference.to_string()))
} else {
BlobId::from_str(value).map(RegistryValue::BlobId).ok()
}
}
_ => None,
}
} else {
None
}
}
fn to_cow(&self) -> Cow<'static, str> {
match self {
RegistryValue::Id(id) => id.to_string().into(),
RegistryValue::BlobId(blob_id) => blob_id.to_string().into(),
RegistryValue::IdReference(r) => format!("#{r}").into(),
}
}
}
impl From<Id> for RegistryValue {
fn from(id: Id) -> Self {
RegistryValue::Id(id)
}
}
impl From<BlobId> for RegistryValue {
fn from(id: BlobId) -> Self {
RegistryValue::BlobId(id)
}
}

View File

@@ -0,0 +1,102 @@
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use crate::{
jmap::{IntoValue, JmapValue},
schema::prelude::Property,
types::EnumImpl,
};
use jmap_tools::Key;
use std::fmt::Debug;
use utils::map::vec_map::VecMap;
impl<T: IntoValue> IntoValue for Option<T> {
fn into_value(self) -> JmapValue<'static> {
match self {
Some(value) => value.into_value(),
None => JmapValue::Null,
}
}
}
impl IntoValue for String {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.into())
}
}
impl IntoValue for bool {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Bool(self)
}
}
impl IntoValue for u64 {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Number(self.into())
}
}
impl IntoValue for i64 {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Number(self.into())
}
}
impl IntoValue for f64 {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Number(self.into())
}
}
impl<T: EnumImpl> IntoValue for T {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.as_str().into())
}
}
trait MapKey: Sized + PartialEq + Eq + Debug {
fn to_key(self) -> Key<'static, Property>;
}
impl MapKey for String {
fn to_key(self) -> Key<'static, Property> {
Key::Owned(self)
}
}
impl MapKey for u32 {
fn to_key(self) -> Key<'static, Property> {
Key::Owned(self.to_string())
}
}
impl<T: EnumImpl> MapKey for T {
fn to_key(self) -> Key<'static, Property> {
Key::Borrowed(self.as_str())
}
}
impl<K: MapKey, V: IntoValue> IntoValue for VecMap<K, V> {
fn into_value(self) -> JmapValue<'static> {
let mut map = jmap_tools::Map::with_capacity(self.len());
for (k, v) in self {
map.insert_unchecked(k.to_key(), v.into_value());
}
JmapValue::Object(map)
}
}
impl<V: IntoValue> IntoValue for Vec<V> {
fn into_value(self) -> JmapValue<'static> {
let mut array = Vec::with_capacity(self.len());
for v in self {
array.push(v.into_value());
}
JmapValue::Array(array)
}
}

View File

@@ -5,15 +5,14 @@
*/
use crate::{
pickle::Pickle,
schema::{
enums::{TracingLevel, TracingLevelOpt},
prelude::{
Account, Duration, GroupAccount, HashedObject, HttpAuth, NodeRange, Object,
ObjectInner, ObjectType, Property, UserAccount,
Account, Duration, GroupAccount, HttpAuth, NodeRange, Object, ObjectInner, Property,
UserAccount,
},
},
types::{EnumImpl, ObjectImpl},
types::EnumImpl,
};
use std::{cmp::Ordering, fmt::Display};
use trc::TOTAL_EVENT_COUNT;
@@ -239,52 +238,8 @@ impl<T: Into<ObjectInner>> From<T> for Object {
}
}
impl<T: ObjectImpl + From<Object>> From<Object> for HashedObject<T> {
fn from(value: Object) -> Self {
HashedObject {
revision: value.revision,
object: T::from(value),
}
}
}
impl<T: ObjectImpl> ObjectImpl for HashedObject<T> {
const FLAGS: u64 = T::FLAGS;
const OBJECT: ObjectType = T::OBJECT;
fn validate(&self, errors: &mut Vec<prelude::ValidationError>) -> bool {
self.object.validate(errors)
}
fn index<'x>(&'x self, builder: &mut prelude::IndexBuilder<'x>) {
self.object.index(builder)
}
}
impl<T: ObjectImpl> Pickle for HashedObject<T> {
fn pickle(&self, out: &mut Vec<u8>) {
T::OBJECT.pickle(out);
self.object.pickle(out);
(xxhash_rust::xxh3::xxh3_64(out) as u32).pickle(out);
}
fn unpickle(stream: &mut crate::pickle::PickledStream<'_>) -> Option<Self> {
let _ = u16::unpickle(stream)?;
Some(Self {
object: T::unpickle(stream)?,
revision: u32::unpickle(stream)?,
})
}
}
impl<'de, T: ObjectImpl> serde::Deserialize<'de> for HashedObject<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
T::deserialize(deserializer).map(|object| Self {
object,
revision: 0,
})
impl Object {
pub fn new(inner: ObjectInner) -> Self {
Object { inner, revision: 0 }
}
}

View File

@@ -3,9 +3,11 @@
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
pub use crate::jmap::IntoValue;
pub use crate::jmap::JmapValue;
pub use crate::jmap::{
JsonPointerPatch, RegistryJsonEnumPatch, RegistryJsonPatch, RegistryJsonPropertyPatch,
object_type,
patch::object_type,
};
pub use crate::pickle::Pickle;
pub use crate::schema::enums::*;
@@ -32,12 +34,6 @@ pub struct Object {
pub revision: u32,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct HashedObject<T: ObjectImpl> {
pub object: T,
pub revision: u32,
}
#[derive(Debug)]
pub struct ExpressionContext<'x> {
pub expr: &'x Expression,

View File

@@ -5,7 +5,7 @@
*/
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch},
pickle::{Pickle, PickledStream},
types::error::PatchError,
};
@@ -270,7 +270,7 @@ impl RegistryJsonPatch for UTCDateTime {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Str(value), None) => {
@@ -292,6 +292,12 @@ impl RegistryJsonPatch for UTCDateTime {
}
}
impl IntoValue for UTCDateTime {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.to_string().into())
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;

View File

@@ -5,7 +5,7 @@
*/
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch},
pickle::{Pickle, PickledStream},
types::error::PatchError,
};
@@ -141,7 +141,7 @@ impl RegistryJsonPatch for Duration {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Number(value), None) => {
@@ -156,3 +156,9 @@ impl RegistryJsonPatch for Duration {
}
}
}
impl IntoValue for Duration {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Number((self.0.as_millis() as u64).into())
}
}

View File

@@ -5,7 +5,7 @@
*/
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch, RegistryValue},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch, RegistryValue},
pickle::{Pickle, PickledStream},
schema::prelude::ObjectType,
types::{EnumImpl, error::PatchError},
@@ -80,7 +80,7 @@ impl RegistryJsonPatch for Id {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Element(RegistryValue::Id(value)), None) => {
@@ -107,7 +107,7 @@ impl RegistryJsonPatch for BlobId {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Element(RegistryValue::BlobId(value)), None) => {
@@ -132,3 +132,15 @@ impl RegistryJsonPatch for BlobId {
}
}
}
impl IntoValue for Id {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Element(RegistryValue::Id(self))
}
}
impl IntoValue for BlobId {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Element(RegistryValue::BlobId(self))
}
}

View File

@@ -7,7 +7,7 @@
use std::{fmt::Display, net::Ipv4Addr, str::FromStr};
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch},
pickle::{Pickle, PickledStream},
types::error::PatchError,
};
@@ -122,7 +122,7 @@ impl RegistryJsonPatch for IpAddr {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Str(value), None) => {
@@ -143,3 +143,9 @@ impl RegistryJsonPatch for IpAddr {
}
}
}
impl IntoValue for IpAddr {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.to_string().into())
}
}

View File

@@ -5,7 +5,7 @@
*/
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch},
pickle::{Pickle, PickledStream},
types::error::PatchError,
};
@@ -251,7 +251,7 @@ impl RegistryJsonPatch for IpAddrOrMask {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Str(value), None) => {
@@ -273,6 +273,12 @@ impl RegistryJsonPatch for IpAddrOrMask {
}
}
impl IntoValue for IpAddrOrMask {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.to_string().into())
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -7,7 +7,7 @@
use std::{fmt::Display, str::FromStr};
use crate::{
jmap::{JsonPointerPatch, RegistryJsonPatch},
jmap::{IntoValue, JmapValue, JsonPointerPatch, RegistryJsonPatch},
pickle::{Pickle, PickledStream},
types::error::PatchError,
};
@@ -91,7 +91,7 @@ impl RegistryJsonPatch for SocketAddr {
fn patch(
&mut self,
mut pointer: JsonPointerPatch<'_>,
value: jmap_tools::Value<'_, crate::schema::prelude::Property, crate::jmap::RegistryValue>,
value: JmapValue<'_>,
) -> Result<(), PatchError> {
match (value, pointer.next()) {
(jmap_tools::Value::Str(value), None) => {
@@ -112,3 +112,9 @@ impl RegistryJsonPatch for SocketAddr {
}
}
}
impl IntoValue for SocketAddr {
fn into_value(self) -> JmapValue<'static> {
JmapValue::Str(self.to_string().into())
}
}