Skip to content

Commit

Permalink
Improved bitflags ts-codegen, add generate_ts_sdk example
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Nov 17, 2024
1 parent 38706b4 commit 05727b0
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 117 deletions.
32 changes: 32 additions & 0 deletions examples/generate_ts_sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::io::Write as _;

use ts_bindgen::{TypeRegistry, TypeScriptDef};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut registry = TypeRegistry::default();

client_sdk::models::gateway::message::ServerMsg::register(&mut registry);
client_sdk::models::gateway::message::ClientMsg::register(&mut registry);

client_sdk::api::commands::register_routes(&mut registry);

let mut out = std::fs::File::create("api.ts")?;

write!(out, "import type {{ ")?;

for (idx, name) in registry.external().iter().enumerate() {
if idx > 0 {
write!(out, ", ")?;
}

write!(out, "{name}")?;
}

write!(
out,
" }} from './models';\nimport {{ command }} from './api';\n\n{}",
registry.display()
)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/api/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) mod sealed {

use crate::models::Permissions;

bitflags::bitflags! {
bitflags2! {
/// Flags for command functionality.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CommandFlags: u8 {
Expand Down
1 change: 0 additions & 1 deletion src/api/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
};
use smol_str::SmolStr;

// NOTE: This macro is here to aggregate schema definitions, but otherwise does very little.
command_module! {
pub mod config;
pub mod file;
Expand Down
2 changes: 1 addition & 1 deletion src/models/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bitflags::bitflags! {
bitflags2! {
/// NOTE: Formats are as individual bitflags (rather than some integer value) so we can do
/// simpler queries when matching valid formats. A user can select formats A, B and C, and testing for a match
/// can be done with a single bitwise-AND operation, rather than many comparisons or an `IN ARRAY` operation.
Expand Down
2 changes: 1 addition & 1 deletion src/models/embed/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum EmbedType {
Article,
}

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmbedFlags: i16 {
/// This embed contains spoilered content and should be displayed as such
Expand Down
2 changes: 1 addition & 1 deletion src/models/emote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmoteFlags: i16 {
const ANIMATED = 1 << 0;
Expand Down
2 changes: 1 addition & 1 deletion src/models/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Intent: u32 {
/// - PARTY_CREATE
Expand Down
2 changes: 1 addition & 1 deletion src/models/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MessageFlags: i32 {
/// This message has been deleted
Expand Down
7 changes: 4 additions & 3 deletions src/models/party/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
mod prefs;
pub use prefs::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PartyFlags: i32 {
/// Must have a verified email address
Expand All @@ -31,6 +31,7 @@ bitflags::bitflags! {
/// Top 6 bits are a language code
const LANGUAGE = 0b11_11_11 << (32 - 6);

/// Combination of all security flags: EMAIL, PHONE, NEW_USER, NEW_MEMBER, MFA_ENABLED
const SECURITY = 0
| Self::EMAIL.bits()
| Self::PHONE.bits()
Expand Down Expand Up @@ -117,7 +118,7 @@ impl Deref for ArchivedParty {
}
}

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PartyMemberFlags: i16 {
const BANNED = 1 << 0;
Expand Down Expand Up @@ -161,7 +162,7 @@ impl Deref for PartyMember {
}
}

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PinFolderFlags: i32 {
const COLOR = 0x00_FF_FF_FFu32 as i32; // top 24 bits
Expand Down
2 changes: 1 addition & 1 deletion src/models/party/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

use crate::models::Locale;

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PartyPrefsFlags: i32 {
const DEFAULT_FLAGS = 0;
Expand Down
31 changes: 15 additions & 16 deletions src/models/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,14 @@ macro_rules! perms {
}}
}

bitflags::bitflags! {
bitflags2! {
/// Party/Room Permissions
///
/// This type is 16-byte aligned to ensure consistent alignment
/// of the inner `u128`` across all platforms.
#[repr(C, align(16))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Permissions: u128 {
const DEFAULT = 0
| Self::CHANGE_NICKNAME.bits()
| Self::VIEW_ROOM.bits()
| Self::READ_MESSAGE_HISTORY.bits()
| Self::SEND_MESSAGES.bits()
| Self::USE_EXTERNAL_EMOTES.bits()
| Self::ADD_REACTIONS.bits()
| Self::EMBED_LINKS.bits()
| Self::ATTACH_FILES.bits()
| Self::SEND_TTS_MESSAGES.bits()
| Self::CONNECT.bits()
| Self::SPEAK.bits();

const ADMINISTRATOR = 1u128 << 0;
const CREATE_INVITE = 1u128 << 1;
const KICK_MEMBERS = 1u128 << 2;
Expand Down Expand Up @@ -90,10 +77,22 @@ bitflags::bitflags! {
/// Allows a user to acquire priority speaker
const PRIORITY_SPEAKER = 1u128 << 63;


#[cfg(test)]
/// Just something to fit in the top half for now during tests
const TEST = 1u128 << 127;

// place aggregate permissions last for iterator reasons
const DEFAULT = 0
| Self::CHANGE_NICKNAME.bits()
| Self::VIEW_ROOM.bits()
| Self::READ_MESSAGE_HISTORY.bits()
| Self::SEND_MESSAGES.bits()
| Self::USE_EXTERNAL_EMOTES.bits()
| Self::ADD_REACTIONS.bits()
| Self::EMBED_LINKS.bits()
| Self::ATTACH_FILES.bits()
| Self::SEND_TTS_MESSAGES.bits()
| Self::CONNECT.bits()
| Self::SPEAK.bits();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/presence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

bitflags::bitflags! {
bitflags2! {
/// NOTE: These flags are ordered such that larger values take precedence
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UserPresenceFlags: i16 {
Expand Down
2 changes: 1 addition & 1 deletion src/models/role.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RoleFlags: i16 {
const HOIST = 1 << 0;
Expand Down
2 changes: 1 addition & 1 deletion src/models/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum RoomKind {
// max value cannot exceed 15
}

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RoomFlags: i16 {
const KIND = 0xF; // first four bits are the kind
Expand Down
2 changes: 1 addition & 1 deletion src/models/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ThreadFlags: i16 {
/// Forum-style thread
Expand Down
5 changes: 3 additions & 2 deletions src/models/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
mod prefs;
pub use prefs::*;

bitflags::bitflags! {
bitflags2! {
/// NOTE: Remember to clear flag caches when they change
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UserFlags: i32 {
Expand Down Expand Up @@ -33,6 +33,7 @@ bitflags::bitflags! {

const RESERVED_4 = 1 << 15;

/// All reserved bits for future use
const RESERVED = 0
| Self::RESERVED_1.bits()
| Self::RESERVED_2.bits()
Expand Down Expand Up @@ -119,7 +120,7 @@ impl UserFlags {
pub const SYSTEM_USER: UserFlags = UserFlags::empty().with_elevation(ElevationLevel::System).union(UserFlags::VERIFIED);
}

bitflags::bitflags! {
bitflags2! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UserProfileBits: i32 {
const AVATAR_ROUNDNESS = 0x7F; // 127, lower 7 bits
Expand Down
14 changes: 11 additions & 3 deletions src/models/user/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum_codes! {
}
}

bitflags::bitflags! {
bitflags2! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UserPrefsFlags: i32 {
/// Reduce movement and animations in the UI
Expand Down Expand Up @@ -89,7 +89,15 @@ bitflags::bitflags! {
const HIDE_ALL_EMBEDS = 1 << 21;
const HIDE_NSFW_EMBEDS = 1 << 22;

const DEFAULT_FLAGS = 0
/// Default user flags for creating a new user:
/// - ALLOW_DMS
/// - GROUP_LINES
/// - ENABLE_SPELLCHECK
/// - SHOW_MEDIA_METADATA
/// - SHOW_DATE_CHANGE
/// - SHOW_GREY_IMAGE_BG
/// - SHOW_ATTACHMENT_GRID
const DEFAULT = 0
| Self::ALLOW_DMS.bits()
| Self::GROUP_LINES.bits()
| Self::ENABLE_SPELLCHECK.bits()
Expand All @@ -113,7 +121,7 @@ impl From<u64> for UserPrefsFlags {

impl Default for UserPrefsFlags {
fn default() -> Self {
Self::DEFAULT_FLAGS
Self::DEFAULT
}
}

Expand Down
Loading

0 comments on commit 05727b0

Please sign in to comment.