Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix deserialization of burst_colours on MessageReaction #2893

Merged
merged 1 commit into from
Jun 9, 2024

Conversation

jamesbt365
Copy link
Member

Fixes #2892.

@github-actions github-actions bot added the model Related to the `model` module. label Jun 7, 2024
Copy link
Member

@GnomedDev GnomedDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not merging this as-is, we cannot duplicate logic like this. There must be a better way to do this.

@jamesbt365
Copy link
Member Author

I couldn't think of a nicer way at the time, It would probably involve the creation of another module (so that it doesn't reexport the function in serenity::all, or changing it carefully so it reexports everything but this function) alongside somehow handling the difference between the optional and non optional version.

Do you have any suggestions in mind?

@GnomedDev
Copy link
Member

Can this not go in /src/model/utils.rs?

@jamesbt365
Copy link
Member Author

I didn't think about moving it there to be fair, I don't think much, that only leaves one issue with one being an option and the other not.

@jamesbt365
Copy link
Member Author

Would this pass?

fn discord_colours_opt<'de, D>(deserializer: D) -> Result<Option<Vec<Colour>>, D::Error>
where
    D: Deserializer<'de>,
{
    let vec_str: Option<Vec<CowStr<'_>>> = Deserialize::deserialize(deserializer)?;

    let Some(vec_str) = vec_str else { return Ok(None) };

    if vec_str.is_empty() {
        return Ok(None);
    }

    deserialize_colours::<D>(vec_str).map(Some)
}

fn discord_colours<'de, D>(deserializer: D) -> Result<Vec<Colour>, D::Error>
where
    D: Deserializer<'de>,
{
    let vec_str: Vec<CowStr<'_>> = Deserialize::deserialize(deserializer)?;

    deserialize_colours::<D>(vec_str)
}

fn deserialize_colours<'de, D>(vec_str: Vec<CowStr<'_>>) -> Result<Vec<Colour>, D::Error>
where
    D: Deserializer<'de>,
{
    vec_str
        .into_iter()
        .map(|s| {
            let s = s.0.strip_prefix('#').ok_or_else(|| DeError::custom("Invalid colour data"))?;

            if s.len() != 6 {
                return Err(DeError::custom("Invalid colour data length"));
            }

            u32::from_str_radix(s, 16)
                .map(Colour::new)
                .map_err(|_| DeError::custom("Invalid colour data"))
        })
        .collect()
}

@GnomedDev
Copy link
Member

Sure

@AlexCzar
Copy link

AlexCzar commented Jun 9, 2024

Do I understand correctly, that 0.12.* won't work with reactions until this is merged?
I'm trying to update my bot to latest version and see following error:

missing field `burst_colours` at line 1 column 816

at

    ctx.http
        .get_message(reaction.channel_id.into(), message_id.into())
        .await

@jamesbt365
Copy link
Member Author

This is a 2 part fix, the 1st part was #2887.

@AlexCzar
Copy link

AlexCzar commented Jun 9, 2024

Can't wait :) I tried applying it myself and specifying git dependency, but some magic happens, and my shuttle app refuses to recognize serenity::Client 🤷🏽

@jamesbt365 jamesbt365 force-pushed the burst_colours-oopsie-2 branch from 573b50e to 5457d3a Compare June 9, 2024 19:22
@jamesbt365 jamesbt365 requested a review from GnomedDev June 9, 2024 19:29
@GnomedDev GnomedDev merged commit e34f449 into serenity-rs:current Jun 9, 2024
22 checks passed
@jamesbt365 jamesbt365 deleted the burst_colours-oopsie-2 branch June 9, 2024 22:01
chrisliebaer added a commit to chrisliebaer/cheapt that referenced this pull request Aug 8, 2024
@FichteFoll
Copy link

FichteFoll commented Aug 31, 2024

Hi, I'd just like to mention that a release with this fix would be much appreciated, also considering the number of duplicate issues of #2892 since people keep running into it. For now, I'm staying on 0.10 which still works but there have been some breaking API changes since then, so I presume not everyone would be happy to revert to that.

heydabop pushed a commit to heydabop/serenity that referenced this pull request Sep 13, 2024
@Friendly-Banana
Copy link

@FichteFoll you can use a newer version, this issue was introduced in 0.12.2. serenity = { version = "=0.12.1" } in Cargo,.toml also works if you use Poise

nnyyxxxx added a commit to awfixer-org/axyl-modmail that referenced this pull request Oct 20, 2024
mbund added a commit to rhombusgg/rhombus that referenced this pull request Oct 20, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
model Related to the `model` module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid Type Error
5 participants