Skip to content

Implement FromStr for Snowflake-related types #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ext4cats opened this issue Feb 28, 2022 · 1 comment
Closed

Implement FromStr for Snowflake-related types #1755

ext4cats opened this issue Feb 28, 2022 · 1 comment
Labels
feature-request A new requested functionality.

Comments

@ext4cats
Copy link

As of v0.10.10 it seems all the Snowflake types (GuildId, ChannelId, etc.) are missing an implementation of the FromStr type. The BSON format, for instance, does not support unsigned 64 bit integers, which means the only alternative is to serialize and deserialize them as strings. However, this can't be done trivially via serde_with::DisplayFromStr because the aforementioned types do not implement the FromStr trait, needed for deserialization.

@nickelc
Copy link
Contributor

nickelc commented Feb 28, 2022

The ID types in the next branch are now serialized as string.

serenity/src/model/id.rs

Lines 259 to 273 in 0391d7c

pub(crate) mod snowflake {
use std::convert::TryFrom;
use std::fmt;
use serde::de::{Error, Visitor};
use serde::{Deserializer, Serializer};
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
deserializer.deserialize_any(SnowflakeVisitor)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S: Serializer>(id: &u64, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(id)
}

@arqunis arqunis added the feature-request A new requested functionality. label Feb 28, 2022
@arqunis arqunis closed this as completed Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A new requested functionality.
Projects
None yet
Development

No branches or pull requests

3 participants