tadpole/types/ids

Opaque ID types so a UserId can never be passed where a GuildId goes. Every ID wraps a validated snowflake; nothing outside this module can build one from thin air or read the integer back out.

When you reach for this

Events hand you typed IDs already — message.channel_id is a ChannelId, ready for bot.send_message — so most code never calls a constructor. You construct when an id arrives as text: config, a command argument, a URL. The constructor validates it as a snowflake before it can go anywhere near Discord.

The types

UserId, GuildId, ChannelId, MessageId, RoleId, ApplicationId, WebhookId — one per Discord object kind this slice touches, all opaque, all built the same way:

import tadpole/types/ids

// "123456789012345678" -> Ok(channel id)
// "nope"              -> Error(InvalidId("nope", NotANumber))
ids.channel_id("123456789012345678")

Reading back: user_to_string, user_to_int, guild_to_string, channel_to_string, message_to_string, role_to_string — the explicit accessors this slice needs. Missing accessors get added when a milestone dereferences the ID, with a reason in their doc.

Failure modes

Constructors fail with InvalidId(value, reason) when the string is not a number, is negative, or embeds a timestamp past ~2090 — see tadpole/types/snowflake for the rules. Decoding Discord payloads validates through the same constructors, so a payload with a garbage id fails as DecodeFailed, not with a broken ID in hand. Conversions cannot fail.

See also

Types

pub opaque type ApplicationId
pub opaque type ChannelId
pub opaque type GuildId
pub type InvalidId {
  InvalidId(
    value: String,
    reason: snowflake.InvalidSnowflakeReason,
  )
}

Constructors

pub opaque type MessageId
pub opaque type RoleId
pub opaque type UserId
pub opaque type WebhookId

Values

pub fn application_id(
  value: String,
) -> Result(ApplicationId, InvalidId)
pub fn channel_id(value: String) -> Result(ChannelId, InvalidId)
pub fn channel_to_string(id: ChannelId) -> String
pub fn guild_id(value: String) -> Result(GuildId, InvalidId)
pub fn guild_to_string(id: GuildId) -> String
pub fn message_id(value: String) -> Result(MessageId, InvalidId)
pub fn message_to_string(id: MessageId) -> String
pub fn role_id(value: String) -> Result(RoleId, InvalidId)
pub fn role_to_string(id: RoleId) -> String
pub fn user_id(value: String) -> Result(UserId, InvalidId)
pub fn user_to_int(id: UserId) -> Int
pub fn user_to_string(id: UserId) -> String
pub fn webhook_id(value: String) -> Result(WebhookId, InvalidId)
Search Document