Skip to content

Commit

Permalink
Implement ToArrayString for Mention (serenity-rs#2857)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev authored and mkrasnitski committed Jul 28, 2024
1 parent ae0d4b3 commit 4cc8036
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/model/mention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::fmt;
#[cfg(all(feature = "model", feature = "utils"))]
use std::str::FromStr;

use to_arraystring::ToArrayString;

use super::prelude::*;
#[cfg(all(feature = "model", feature = "utils"))]
use crate::utils;
Expand Down Expand Up @@ -106,11 +108,26 @@ mention!(value:

impl fmt::Display for Mention {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Mention::Channel(id) => f.write_fmt(format_args!("<#{id}>")),
Mention::Role(id) => f.write_fmt(format_args!("<@&{id}>")),
Mention::User(id) => f.write_fmt(format_args!("<@{id}>")),
}
f.write_str(&self.to_arraystring())
}
}

impl ToArrayString for Mention {
type ArrayString = arrayvec::ArrayString<{ 20 + 4 }>;

fn to_arraystring(self) -> Self::ArrayString {
let (prefix, id) = match self {
Self::Channel(id) => ("<#", id.get()),
Self::Role(id) => ("<@&", id.get()),
Self::User(id) => ("<@", id.get()),
};

let mut out = Self::ArrayString::new();
out.push_str(prefix);
out.push_str(&id.to_arraystring());
out.push('>');

out
}
}

Expand Down

0 comments on commit 4cc8036

Please sign in to comment.