1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

use std::fmt;
use std::sync::Arc;

use crate::Error;
use crate::Player;
use crate::Connector;

use crate::net::Packet;
use crate::net::BinaryReader;

use crate::message::any_chat_message::prelude::*;

pub struct UnicastChatMessage {
    data:   ChatMessageData,
    to:     Arc<Player>,
    message:String,
}

impl UnicastChatMessage {
    pub fn from_packet(connector: &Arc<Connector>, packet: &Packet, reader: &mut BinaryReader) -> Result<UnicastChatMessage, Error> {
        Ok(UnicastChatMessage {
            data:   ChatMessageData::from_packet(connector, packet, reader)?,
            to:     connector.player_for(reader.read_u16()?)?,
            message:reader.read_string()?,
        })
    }

    pub fn to(&self) -> &Arc<Player> {
        &self.to
    }

    pub fn message(&self) -> &str {
        &self.message
    }
}

// TODO replace with delegation directive
// once standardized: https://github.com/rust-lang/rfcs/pull/1406
impl Message for UnicastChatMessage {
    fn timestamp(&self) -> &DateTime {
        self.data.timestamp()
    }
}

// TODO replace with delegation directive
// once standardized: https://github.com/rust-lang/rfcs/pull/1406
impl ChatMessage for UnicastChatMessage {
    fn from(&self) -> &Arc<Player> {
        self.data.from()
    }
}

impl fmt::Display for UnicastChatMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}] <{}> {}", self.timestamp(), self.from().name(), self.message())
    }
}