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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! General network related functionality.

use std::{io, pin::Pin};

use futures_util::{Future, Stream};
use tokio::net::ToSocketAddrs;

use crate::parser;

/// A network connection.
pub trait Connection: Stream<Item = Result<Event, Error>> {
    /// Creates a connection.
    fn connect<A, B>(addr: A, nickname: B) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + 'static>> where A: ToSocketAddrs + 'static, B: Into<Vec<u8>> + 'static, Self: Sized;
    /// Sends a message.
    fn send<B>(&mut self, message: Message, recipients: Vec<B>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + '_>> where B: Into<Vec<u8>>, B: 'static;
    /// Obtains a list of all connected users.
    fn user_list(&mut self) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<u8>>, Error>> + '_>>;
    /// Changes the nickname.
    fn change_nickname<B>(&mut self, nickname: B) -> Pin<Box<dyn Future<Output = Result<(), Error>> + '_>> where B: Into<Vec<u8>>, B: 'static;
}

#[derive(Debug)]
/// A network error.
pub enum Error {
    /// A error caused by a I/O failure.
    Io(io::Error),
    /// A error caused by the packet parser.
    Parser(parser::Error),
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl From<parser::Error> for Error {
    fn from(e: parser::Error) -> Self {
        Self::Parser(e)
    }
}

#[derive(Debug)]
/// A message that can be sent or recieved.
pub enum Message {
    /// A string of bytes.
    Word(Vec<u8>),
    /// A string of bytes that contains instructions to be executed.
    Instructions(Vec<u8>),
    /// A list of strings.
    List(Vec<Vec<u8>>),
    /// An image.
    Image(Vec<u8>),
    /// An Imagine object.
    Object(Vec<u8>),
}

impl Message {
    /// Creates a message containing a word.
    pub fn word_from<B>(word: B) -> Self
        where B: Into<Vec<u8>>
    {
        Self::Word(word.into())
    }

    /// Creates a message containing instructions.
    pub fn instructions_from<B>(instructions: B) -> Self
        where B: Into<Vec<u8>>
    {
        Self::Instructions(instructions.into())
    }

    /// Creates a message containing a list.
    pub fn list_from<B>(list: Vec<B>) -> Self
        where B: Into<Vec<u8>>
    {
        Self::List(list.into_iter().map(|w| w.into()).collect())
    }

    /// Creates a message containing an image.
    pub fn image_from<B>(bytes: B) -> Self
        where B: Into<Vec<u8>>
    {
        Self::Image(bytes.into())
    }

    /// Creates a message containing an object.
    pub fn object_from<B>(bytes: B) -> Self
        where B: Into<Vec<u8>>
    {
        Self::Object(bytes.into())
    }
}

#[derive(Debug)]
/// A event that can occur during while connected.
pub enum Event {
    /// A recieved message.
    RecievedMessage {
        /// The nickname of the message sender.
        sender: Vec<u8>,
        /// The recieved message.
        message: Message,
    },
    /// A nickname setup.
    SetNickname(Vec<u8>),
    /// A nickname change.
    ChangedNickname(Vec<u8>),
}