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
use std::{io, pin::Pin};
use futures_util::{Future, Stream};
use tokio::net::ToSocketAddrs;
use crate::parser;
pub trait Connection: Stream<Item = Result<Event, Error>> {
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;
fn send<B>(&mut self, message: Message, recipients: Vec<B>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + '_>> where B: Into<Vec<u8>>, B: 'static;
fn user_list(&mut self) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<u8>>, Error>> + '_>>;
fn change_nickname<B>(&mut self, nickname: B) -> Pin<Box<dyn Future<Output = Result<(), Error>> + '_>> where B: Into<Vec<u8>>, B: 'static;
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
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)]
pub enum Message {
Word(Vec<u8>),
Instructions(Vec<u8>),
List(Vec<Vec<u8>>),
Image(Vec<u8>),
Object(Vec<u8>),
}
impl Message {
pub fn word_from<B>(word: B) -> Self
where B: Into<Vec<u8>>
{
Self::Word(word.into())
}
pub fn instructions_from<B>(instructions: B) -> Self
where B: Into<Vec<u8>>
{
Self::Instructions(instructions.into())
}
pub fn list_from<B>(list: Vec<B>) -> Self
where B: Into<Vec<u8>>
{
Self::List(list.into_iter().map(|w| w.into()).collect())
}
pub fn image_from<B>(bytes: B) -> Self
where B: Into<Vec<u8>>
{
Self::Image(bytes.into())
}
pub fn object_from<B>(bytes: B) -> Self
where B: Into<Vec<u8>>
{
Self::Object(bytes.into())
}
}
#[derive(Debug)]
pub enum Event {
RecievedMessage {
sender: Vec<u8>,
message: Message,
},
SetNickname(Vec<u8>),
ChangedNickname(Vec<u8>),
}