Skip to content

Commit

Permalink
Finish rkyv validation additions
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Dec 9, 2023
1 parent 2088cd9 commit 538496f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ macro_rules! decl_newtype_prefs {
$(
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
#[cfg_attr(feature = "rkyv", derive(rkyv::CheckBytes))]
#[repr(transparent)]
pub struct $name(pub $ty);

Expand Down
47 changes: 22 additions & 25 deletions src/models/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,29 @@ mod tests {
}
}

/// Half of a user relationship
#[rustfmt::skip]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[derive(enum_primitive_derive::Primitive)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[repr(i8)] // two of these fit together to form a full i16 relationship in the database
pub enum UserRelationship {
#[default]
None = 0,

Friend = 1,

//
// reserve some space for future relationships
//

/// Normal user blocking
Blocked = 100,

/// Blocking + hide messages from the blocked user
BlockedDangerous = 101,
common::enum_codes! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[derive(enum_primitive_derive::Primitive)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum UserRelationship: i8 = None {
#[default]
None = 0,

Friend = 1,

//
// reserve some space for future relationships
//

/// Normal user blocking
Blocked = 100,

/// Blocking + hide messages from the blocked user
BlockedDangerous = 101,
}
}

common::impl_rkyv_for_pod!(UserRelationship);
common::impl_sql_for_enum_primitive!(UserRelationship);

/*
UserA UserB
Expand Down Expand Up @@ -316,6 +312,7 @@ BlockedDangerous None UserA has blocked UserB and reported the
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct Relationship {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<SmolStr>,
Expand Down
60 changes: 30 additions & 30 deletions src/models/user/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,43 @@ use serde_json::Value;

use super::*;

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[allow(non_camel_case_types)]
#[repr(u16)]
pub enum Locale {
#[default]
enUS = 0,
common::enum_codes! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[allow(non_camel_case_types)]
pub enum Locale: u16 = enUS {
#[default]
enUS = 0,
}
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[repr(u16)]
pub enum Font {
#[default]
SansSerif = 0,
Serif = 1,
Monospace = 2,
Cursive = 3,
ComicSans = 4,
common::enum_codes! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
pub enum Font: u16 = SansSerif {
#[default]
SansSerif = 0,
Serif = 1,
Monospace = 2,
Cursive = 3,
ComicSans = 4,

// third-party fonts
OpenDyslexic = 30,
// third-party fonts
OpenDyslexic = 30,

AtkinsonHyperlegible = 31,
AtkinsonHyperlegible = 31,
}
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
#[repr(u8)]
pub enum FriendAddability {
#[default]
None = 0,
FriendsOfFriends = 10,
ServerMembers = 20,
Anyone = 30,
common::enum_codes! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
pub enum FriendAddability: u8 = None {
#[default]
None = 0,
FriendsOfFriends = 10,
ServerMembers = 20,
Anyone = 30,
}
}

common::impl_rkyv_for_pod!(Locale);
common::impl_rkyv_for_pod!(Font);
common::impl_rkyv_for_pod!(FriendAddability);

bitflags::bitflags! {
pub struct UserPrefsFlags: i32 {
/// Reduce movement and animations in the UI
Expand Down Expand Up @@ -131,6 +130,7 @@ pub mod preferences {

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct UserPreferences {
#[serde(default, skip_serializing_if = "is_default", alias = "locale")]
pub l: Locale,
Expand Down

0 comments on commit 538496f

Please sign in to comment.