forked from lavina/lavina
refactored PrefixedNick
This commit is contained in:
parent
9c020e970e
commit
cf6022827d
|
@ -27,7 +27,7 @@ use crate::{
|
|||
|
||||
/// Opaque player identifier. Cannot contain spaces, must be shorter than 32.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
|
||||
pub struct PlayerId(pub Str);
|
||||
pub struct PlayerId(Str);
|
||||
impl PlayerId {
|
||||
pub fn from(str: impl Into<Str>) -> Result<PlayerId> {
|
||||
let bytes = str.into();
|
||||
|
@ -46,22 +46,6 @@ impl PlayerId {
|
|||
self.0
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Prefix(pub Str);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PrefixedNick {
|
||||
pub prefix: Prefix,
|
||||
pub nick: PlayerId,
|
||||
}
|
||||
impl PrefixedNick {
|
||||
pub fn fromPlayerId(id: PlayerId) -> Result<PrefixedNick> {
|
||||
Ok(PrefixedNick { prefix: Prefix(Str::from("")), nick: id } )
|
||||
}
|
||||
pub fn fromStr(nick: Str) -> Result<PrefixedNick> {
|
||||
Ok(PrefixedNick { prefix: Prefix(Str::from("")), nick: PlayerId(nick) } )
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ConnectionId(pub AnonKey);
|
||||
|
|
|
@ -18,6 +18,7 @@ use crate::prelude::*;
|
|||
use crate::protos::irc::client::{client_message, ClientMessage};
|
||||
use crate::protos::irc::server::{AwayStatus, ServerMessage, ServerMessageBody};
|
||||
use crate::protos::irc::{Chan, Recipient};
|
||||
use crate::protos::irc::user::PrefixedNick;
|
||||
use crate::util::Terminator;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//! Client-to-Server IRC protocol.
|
||||
pub mod client;
|
||||
pub mod server;
|
||||
pub mod user;
|
||||
|
||||
use std::io::Result;
|
||||
use crate::prelude::Str;
|
||||
|
|
|
@ -3,7 +3,7 @@ use tokio::io::AsyncWrite;
|
|||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use super::*;
|
||||
use crate::core::player::PrefixedNick;
|
||||
use crate::protos::irc::user::PrefixedNick;
|
||||
|
||||
/// Server-to-client message.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
|
@ -284,17 +284,16 @@ impl ServerMessageBody {
|
|||
let mut membersTail: Vec<PrefixedNick> = members.tail.clone();
|
||||
membersTail.insert(0, members.clone().head);
|
||||
|
||||
let membersStr: Vec<String> = membersTail.iter().map(| prefixedNick| {
|
||||
let pref: String = prefixedNick.prefix.0.clone().to_string();
|
||||
let nick: String = prefixedNick.nick.0.clone().to_string();
|
||||
pref + &nick
|
||||
}).collect();
|
||||
writer.write_all(b"353 ").await?;
|
||||
writer.write_all(client.as_bytes()).await?;
|
||||
writer.write_all(b" = ").await?;
|
||||
chan.write_async(writer).await?;
|
||||
writer.write_all(b" :").await?;
|
||||
writer.write_all(membersStr.join(", ").as_bytes()).await?;
|
||||
for member in members {
|
||||
writer.write_all(member.prefix.to_string().as_bytes()).await?;
|
||||
writer.write_all(member.nick.as_bytes()).await?;
|
||||
writer.write_all(b" ").await?;
|
||||
}
|
||||
}
|
||||
ServerMessageBody::N366NamesReplyEnd { client, chan } => {
|
||||
writer.write_all(b"366 ").await?;
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
use super::*;
|
||||
use std::fmt;
|
||||
use crate::core::player::PlayerId;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Prefix{
|
||||
empty,
|
||||
}
|
||||
impl fmt::Display for Prefix {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Prefix::empty => write!(f, ""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PrefixedNick {
|
||||
pub prefix: Prefix,
|
||||
pub nick: Str,
|
||||
}
|
||||
impl PrefixedNick {
|
||||
pub fn fromStr(nick: Str) -> Result<PrefixedNick> {
|
||||
Ok(PrefixedNick { prefix: Prefix::empty, nick } )
|
||||
}
|
||||
|
||||
pub fn fromPlayerId(id: PlayerId) -> Result<PrefixedNick> {
|
||||
Ok(PrefixedNick { prefix: Prefix::empty, nick: id.into_inner() } )
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue