chore(types): unbloat and clean up dependencies

This commit is contained in:
oxalica 2024-10-18 10:15:15 -04:00
parent ccd5401411
commit d7ac77a1d9
5 changed files with 59 additions and 237 deletions

View file

@ -4,9 +4,11 @@ version = "0.0.0"
edition = "2021"
[features]
default = []
unsafe_use_mock_instant_for_testing = ["dep:mock_instant"]
default = ["ed25519-dalek/default"]
schemars = ["dep:schemars"]
rusqlite = ["dep:rusqlite"]
unsafe_use_mock_instant_for_testing = ["dep:mock_instant"]
[[bench]]
name = "crypto_ops"
@ -14,25 +16,29 @@ harness = false
[dependencies]
bitflags = "2"
bitflags_serde_shim = "0.2"
ed25519-dalek = "2"
ed25519-dalek = { version = "2", default-features = false }
hex = { version = "0.4", features = ["serde"] }
html-escape = "0.2"
mock_instant = { version = "0.5", optional = true }
rand = "0.8"
rusqlite = { version = "0.32", optional = true }
serde = { version = "1", features = ["derive"] }
serde_jcs = "0.1"
serde_json = "1"
serde_with = "3"
thiserror = "1"
url = { version = "2", features = ["serde"] }
[dependencies.rusqlite]
version = "0.32"
optional = true
[dependencies.schemars]
version = "0.8"
optional = true
features = ["url"]
[dependencies.mock_instant]
version = "0.5"
optional = true
[dev-dependencies]
criterion = "0.5"
ed25519-dalek = { version = "2", features = ["rand_core"] }

View file

@ -14,7 +14,7 @@ macro_rules! impl_json_schema_as {
// Workaround: https://github.com/GREsau/schemars/issues/267
#[cfg(feature = "schemars")]
macro_rules! impl_json_schema_as {
($ty:ident => $as_ty:ty) => {
($ty:ty => $as_ty:ty) => {
impl schemars::JsonSchema for $ty {
fn schema_name() -> String {
stringify!($ty).into()

View file

@ -3,9 +3,7 @@ use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;
use bitflags_serde_shim::impl_serde_for_bitflags;
use serde::{de, ser, Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use url::Url;
use crate::identity::IdUrl;
@ -13,13 +11,26 @@ use crate::{PubKey, Signed};
/// An opaque server-specific ID for rooms, messages, and etc.
/// It's currently serialized as a string for JavaScript's convenience.
#[serde_as]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Id(#[serde_as(as = "DisplayFromStr")] pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(pub i64);
impl_json_schema_as!(Id => String);
impl Serialize for Id {
fn serialize<S: ser::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
// TODO: No alloc?
self.0.to_string().serialize(ser)
}
}
impl<'de> Deserialize<'de> for Id {
fn deserialize<D: de::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
let s = <&str>::deserialize(de)?;
s.parse()
.map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &"a stringified integer"))
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
@ -455,13 +466,27 @@ bitflags::bitflags! {
}
}
impl_serde_for_bitflags!(ServerPermission);
impl_serde_for_bitflags!(MemberPermission);
impl_serde_for_bitflags!(RoomAttrs);
macro_rules! impl_for_bitflags {
($($ty:ty),* $(,)?) => {
$(
impl Serialize for $ty {
fn serialize<S: ser::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
self.bits().serialize(ser)
}
}
impl_json_schema_as!(ServerPermission => i32);
impl_json_schema_as!(MemberPermission => i32);
impl_json_schema_as!(RoomAttrs => i32);
impl<'de> Deserialize<'de> for $ty {
fn deserialize<D: de::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
<_>::deserialize(de).map(Self::from_bits_retain)
}
}
impl_json_schema_as!($ty => <$ty as bitflags::Flags>::Bits);
)*
};
}
impl_for_bitflags!(ServerPermission, MemberPermission, RoomAttrs);
#[cfg(test)]
mod tests {