diff --git a/README.md b/README.md index cdac65d4..3124e125 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,11 @@ to run the tests for wasm. ## Versioning -This crate uses Semantic Versioning 2.0.0 as its versioning scheme. You can read the specification [here](https://semver.org/spec/v2.0.0.html). +Like other cargo crates, this crate uses Semantic Versioning 2.0.0 as its versioning scheme. +You can read the specification [here](https://semver.org/spec/v2.0.0.html). + +Code gated behind the `backend` feature is not considered part of the public API and can change without +affecting semver compatibility. The `backend` feature is explicitly meant for use in [`symfonia`](https://github.com/polyphony-chat/symfonia) ## Contributing diff --git a/src/types/entities/channel.rs b/src/types/entities/channel.rs index df301533..02cdbb3f 100644 --- a/src/types/entities/channel.rs +++ b/src/types/entities/channel.rs @@ -8,6 +8,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; use std::fmt::{Debug, Formatter}; use std::str::FromStr; +use crate::errors::ChorusError; use crate::types::{ entities::{GuildMember, User}, utils::Snowflake, @@ -31,7 +32,7 @@ use serde::de::{Error, Visitor}; #[cfg(feature = "sqlx")] use sqlx::types::Json; -use super::{option_arc_rwlock_ptr_eq, option_vec_arc_rwlock_ptr_eq}; +use super::{option_arc_rwlock_ptr_eq, option_vec_arc_rwlock_ptr_eq, Emoji}; #[derive(Default, Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] @@ -42,17 +43,23 @@ use super::{option_arc_rwlock_ptr_eq, option_vec_arc_rwlock_ptr_eq}; /// See pub struct Channel { pub application_id: Option, - pub applied_tags: Option>, + #[cfg(not(feature = "sqlx"))] + pub applied_tags: Option>, + #[cfg(feature = "sqlx")] + pub applied_tags: Option>>, + #[cfg(not(feature = "sqlx"))] pub available_tags: Option>, + #[cfg(feature = "sqlx")] + pub available_tags: Option>>, pub bitrate: Option, #[serde(rename = "type")] + #[cfg_attr(feature = "sqlx", sqlx(rename = "type"))] pub channel_type: ChannelType, pub created_at: Option>, pub default_auto_archive_duration: Option, - pub default_forum_layout: Option, - // DefaultReaction could be stored in a separate table. However, there are a lot of default emojis. How would we handle that? + pub default_forum_layout: Option, pub default_reaction_emoji: Option, - pub default_sort_order: Option, + pub default_sort_order: Option, pub default_thread_rate_limit_per_user: Option, pub flags: Option, pub guild_id: Option, @@ -325,6 +332,15 @@ pub struct DefaultReaction { pub emoji_name: Option, } +impl From for DefaultReaction { + fn from(value: Emoji) -> Self { + Self { + emoji_id: Some(value.id), + emoji_name: value.name, + } + } +} + #[derive( Default, Clone, @@ -401,3 +417,111 @@ pub struct FollowedChannel { pub channel_id: Snowflake, pub webhook_id: Snowflake, } + +#[derive( + Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Copy, Hash, PartialOrd, Ord, Default, +)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +#[repr(u8)] +pub enum DefaultForumLayout { + #[default] + Default = 0, + List = 1, + Grid = 2, +} + +impl TryFrom for DefaultForumLayout { + type Error = ChorusError; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(DefaultForumLayout::Default), + 1 => Ok(DefaultForumLayout::List), + 2 => Ok(DefaultForumLayout::Grid), + _ => Err(ChorusError::InvalidArguments { + error: "Value is not a valid DefaultForumLayout".to_string(), + }), + } + } +} + +#[cfg(feature = "sqlx")] +impl sqlx::Type for DefaultForumLayout { + fn type_info() -> ::TypeInfo { + >::type_info() + } +} + +#[cfg(feature = "sqlx")] +impl<'q> sqlx::Encode<'q, sqlx::Postgres> for DefaultForumLayout { + fn encode_by_ref( + &self, + buf: &mut ::ArgumentBuffer<'q>, + ) -> Result { + let sqlx_pg_uint = sqlx_pg_uint::PgU8::from(*self as u8); + sqlx_pg_uint.encode_by_ref(buf) + } +} + +#[cfg(feature = "sqlx")] +impl<'r> sqlx::Decode<'r, sqlx::Postgres> for DefaultForumLayout { + fn decode( + value: ::ValueRef<'r>, + ) -> Result { + let sqlx_pg_uint = sqlx_pg_uint::PgU8::decode(value)?; + DefaultForumLayout::try_from(sqlx_pg_uint.to_uint()).map_err(|e| e.into()) + } +} + +#[derive( + Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Copy, Hash, PartialOrd, Ord, Default, +)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +#[repr(u8)] +pub enum DefaultSortOrder { + #[default] + LatestActivity = 0, + CreationTime = 1, +} + +impl TryFrom for DefaultSortOrder { + type Error = ChorusError; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(DefaultSortOrder::LatestActivity), + 1 => Ok(DefaultSortOrder::CreationTime), + _ => Err(ChorusError::InvalidArguments { + error: "Value is not a valid DefaultSearchOrder".to_string(), + }), + } + } +} + +#[cfg(feature = "sqlx")] +impl sqlx::Type for DefaultSortOrder { + fn type_info() -> ::TypeInfo { + >::type_info() + } +} + +#[cfg(feature = "sqlx")] +impl<'q> sqlx::Encode<'q, sqlx::Postgres> for DefaultSortOrder { + fn encode_by_ref( + &self, + buf: &mut ::ArgumentBuffer<'q>, + ) -> Result { + let sqlx_pg_uint = sqlx_pg_uint::PgU8::from(*self as u8); + sqlx_pg_uint.encode_by_ref(buf) + } +} + +#[cfg(feature = "sqlx")] +impl<'r> sqlx::Decode<'r, sqlx::Postgres> for DefaultSortOrder { + fn decode( + value: ::ValueRef<'r>, + ) -> Result { + let sqlx_pg_uint = sqlx_pg_uint::PgU8::decode(value)?; + DefaultSortOrder::try_from(sqlx_pg_uint.to_uint()).map_err(|e| e.into()) + } +} diff --git a/src/types/entities/user.rs b/src/types/entities/user.rs index 5e761307..8491e7c6 100644 --- a/src/types/entities/user.rs +++ b/src/types/entities/user.rs @@ -4,7 +4,7 @@ use crate::errors::ChorusError; use crate::types::utils::Snowflake; -use crate::{UInt32, UInt8}; +use crate::UInt32; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use serde_aux::prelude::{deserialize_default_from_null, deserialize_option_number_from_string}; @@ -826,11 +826,14 @@ pub struct MutualGuild { #[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] pub struct UserNote { /// Actual note contents; max 256 characters - pub note: String, + #[serde(rename = "note")] + pub content: String, /// The ID of the user the note is on - pub note_user_id: Snowflake, + #[serde(rename = "note_user_id")] + pub target_id: Snowflake, /// The ID of the user who created the note (always the current user) - pub user_id: Snowflake, + #[serde(rename = "user_id")] + pub author_id: Snowflake, } /// Structure which defines an affinity the local user has with another user. diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 62b3b53a..abc57ff3 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::types::{entities::PermissionOverwrite, ChannelType, DefaultReaction, Snowflake}; +// TODO: Needs updating #[derive(Debug, Deserialize, Serialize, Default, PartialEq, PartialOrd)] #[serde(rename_all = "snake_case")] pub struct ChannelCreateSchema {