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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/// Client module for p2p text editor
pub mod client;
/// Server module for p2p text editor
pub mod server;
/// Utils module for p2p text editor
pub mod utils;

#[cfg(test)]
mod test;

use std::{cmp::Ordering, net::SocketAddr};

use server::editor_rpc::{ContentPosition, OperationType, Pos, UpdateContentRequest};

use crate::types::rpc_types::{CursorPosition, FileOperation};

pub trait GetCmpType {
    type Type: Clone;

    fn new(t: &Self::Type) -> Self;
}

#[derive(Clone)]
pub struct OpRange {
    pub start: CursorPosition,
    pub end: CursorPosition,
}

#[derive(Clone)]
pub struct Modification {
    pub version: u64,
    pub op: OperationType,
    pub op_range: OpRange,
    pub modified_content: String,
}

#[derive(Clone, Debug)]
pub struct ClientCursor {
    pub addr: SocketAddr,
    pub row: u64,
    pub col: u64,
}

struct CursorAsc(ClientCursor);

pub struct CursorRowEq(ClientCursor);

impl Into<Pos> for CursorPosition {
    fn into(self) -> Pos {
        Pos {
            row: self.row,
            col: self.col,
        }
    }
}

impl From<Pos> for CursorPosition {
    fn from(pos: Pos) -> Self {
        CursorPosition {
            row: pos.row,
            col: pos.col,
        }
    }
}

impl Into<ContentPosition> for OpRange {
    fn into(self) -> ContentPosition {
        ContentPosition {
            start: Option::Some(self.start.into()),
            end: Option::Some(self.end.into()),
        }
    }
}

impl From<ContentPosition> for OpRange {
    fn from(pos: ContentPosition) -> Self {
        OpRange {
            start: pos.start.unwrap().into(),
            end: pos.end.unwrap().into(),
        }
    }
}

impl Into<UpdateContentRequest> for Modification {
    fn into(self) -> UpdateContentRequest {
        UpdateContentRequest {
            version: self.version,
            op: self.op.into(),
            op_range: Option::Some(self.op_range.into()),
            modified_content: self.modified_content,
        }
    }
}

impl From<UpdateContentRequest> for Modification {
    fn from(req: UpdateContentRequest) -> Self {
        Modification {
            version: req.version,
            op: req.op(),
            op_range: req.op_range.unwrap().into(),
            modified_content: req.modified_content,
        }
    }
}

impl GetCmpType for CursorAsc {
    type Type = ClientCursor;

    fn new(t: &Self::Type) -> Self {
        CursorAsc { 0: t.clone() }
    }
}

impl PartialEq for CursorAsc {
    fn eq(&self, other: &Self) -> bool {
        self.0.addr == other.0.addr
    }
}

impl Eq for CursorAsc {}

impl PartialOrd for CursorAsc {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Option::Some(
            self.0
                .row
                .cmp(&other.0.row)
                .then(self.0.col.cmp(&other.0.col))
                .then(self.0.addr.cmp(&other.0.addr)),
        )
    }
}

impl Ord for CursorAsc {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0
            .row
            .cmp(&other.0.row)
            .then(self.0.col.cmp(&other.0.col))
            .then(self.0.addr.cmp(&other.0.addr))
    }
}

impl GetCmpType for CursorRowEq {
    type Type = ClientCursor;

    fn new(t: &Self::Type) -> Self {
        CursorRowEq { 0: t.clone() }
    }
}

impl PartialEq for CursorRowEq {
    fn eq(&self, other: &Self) -> bool {
        self.0.row == other.0.row
    }
}

impl Eq for CursorRowEq {}

impl Into<OperationType> for FileOperation {
    fn into(self) -> OperationType {
        match self {
            FileOperation::Insert => OperationType::Insert,
            FileOperation::Delete => OperationType::Delete,
            FileOperation::Replace => OperationType::Replace,
        }
    }
}