use std::fmt::{self, Display, Formatter};
pub mod v1 {
tonic::include_proto!("protocol.chat.v1");
}
pub use v1::*;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Place {
Top { before: u64 },
Between { after: u64, before: u64 },
Bottom { after: u64 },
}
impl Place {
pub fn between(before: u64, after: u64) -> Self {
Self::Between { after, before }
}
pub fn top(before: u64) -> Self {
Self::Top { before }
}
pub fn bottom(after: u64) -> Self {
Self::Bottom { after }
}
pub fn next(&self) -> u64 {
match self {
Place::Top { before } => *before,
Place::Between { before, after: _ } => *before,
Place::Bottom { after: _ } => 0,
}
}
pub fn previous(&self) -> u64 {
match self {
Place::Top { before: _ } => 0,
Place::Between { before: _, after } => *after,
Place::Bottom { after } => *after,
}
}
}
#[into_request("JoinGuildRequest", "PreviewGuildRequest")]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct InviteId {
invite_id: String,
}
impl InviteId {
pub fn new(name: impl ToString) -> Option<Self> {
let name = name.to_string();
if name.is_empty() {
None
} else {
Some(Self { invite_id: name })
}
}
}
impl From<InviteId> for String {
fn from(other: InviteId) -> String {
other.invite_id
}
}
impl Display for InviteId {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}", self.invite_id)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
fn empty_invite_id() {
InviteId::new("").unwrap();
}
#[test]
fn invite_id() {
const ID: &str = "harmony";
assert_eq!(
InviteId::new(ID).unwrap(),
InviteId {
invite_id: ID.to_string()
}
);
}
}