Skip to content

Commit 6b67489

Browse files
committed
feat: Added file types, web requests, web commands, web events
1 parent 42090c5 commit 6b67489

1 file changed

Lines changed: 53 additions & 31 deletions

File tree

src/types.rs

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use uuid::Uuid;
1111

1212
pub type Bytes = Vec<u8>;
1313

14-
#[derive(Debug, Clone, Serialize, Deserialize, Eq)]
14+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
1515
pub struct MediaReference {
1616
location: NodeId,
1717
pub id: Uuid
@@ -38,14 +38,6 @@ impl Display for MediaReference {
3838
}
3939
}
4040

41-
impl PartialEq for MediaReference {
42-
fn eq(&self, other: &Self) -> bool {
43-
self.id == other.id
44-
}
45-
}
46-
47-
48-
4941
impl FromStr for MediaReference {
5042
type Err = anyhow::Error;
5143

@@ -61,23 +53,18 @@ impl FromStr for MediaReference {
6153
}
6254
}
6355

64-
#[derive(Debug, Clone, Serialize, Deserialize, Eq)]
56+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
6557
pub struct TextFile{
66-
id: Uuid,
58+
pub id: Uuid,
6759
title: String,
6860
content: String,
69-
media_refs: Option<Vec<MediaReference>>
61+
media_refs: Vec<MediaReference>
7062
}
7163

72-
impl PartialEq for TextFile {
73-
fn eq(&self, other: &Self) -> bool {
74-
self.id == other.id
75-
}
76-
}
7764

7865
impl TextFile {
7966
#[must_use]
80-
pub fn new(title: String, content: String, media_refs: Option<Vec<MediaReference>>) -> Self {
67+
pub fn new(title: String, content: String, media_refs: Vec<MediaReference>) -> Self {
8168
Self {
8269
title,
8370
id: Uuid::new_v4(),
@@ -88,25 +75,41 @@ impl TextFile {
8875

8976
#[must_use]
9077
pub fn get_refs(&self) -> Vec<MediaReference> {
91-
if let Some(vec) = &self.media_refs {
92-
return vec.clone()
93-
}
94-
vec![]
78+
self.media_refs.clone()
79+
}
80+
9581

82+
#[must_use]
83+
pub fn get_media_ids(&self) -> Vec<Uuid> {
84+
self.media_refs.iter().map(|m| m.id).collect()
9685
}
9786
}
9887

9988

100-
#[derive(Debug, Clone, Serialize, Deserialize)]
89+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
10190
pub struct MediaFile {
102-
id: Uuid,
91+
pub id: Uuid,
10392
title: String,
10493
content: Vec<Bytes>,
10594
}
10695

107-
impl PartialEq for MediaFile {
108-
fn eq(&self, other: &Self) -> bool {
109-
self.id == other.id
96+
97+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
98+
pub struct File {
99+
pub id: Uuid,
100+
pub text_file: TextFile,
101+
media_files: Vec<MediaFile>
102+
}
103+
104+
105+
impl File {
106+
#[must_use]
107+
pub fn new(text_file: TextFile, media_files: Vec<MediaFile>) -> Self {
108+
Self {
109+
id: text_file.id,
110+
text_file,
111+
media_files
112+
}
110113
}
111114
}
112115

@@ -143,10 +146,7 @@ pub enum WebResponse {
143146
MediaFile { media_data: Vec<u8> },
144147

145148
#[serde(rename = "error_requested_not_found!")]
146-
ErrorNotFound,
147-
148-
#[serde(rename = "error_unsupported_request!")]
149-
ErrorUnsupportedRequest,
149+
ErrorFileNotFound(Uuid),
150150
}
151151

152152
#[derive(Serialize, Deserialize, Debug)]
@@ -215,6 +215,28 @@ pub enum ChatEvent {
215215
}
216216

217217

218+
#[derive(Debug, Clone)]
219+
pub enum WebCommand {
220+
GetCachedFiles,
221+
GetFile(Uuid),
222+
GetTextFiles,
223+
GetTextFile(Uuid),
224+
GetMediaFiles,
225+
GetMediaFile(Uuid),
226+
}
227+
228+
#[derive(Debug, Clone)]
229+
pub enum WebEvent {
230+
CachedFiles(Vec<File>),
231+
File(File),
232+
TextFiles(Vec<TextFile>),
233+
TextFile(TextFile),
234+
MediaFiles(Vec<MediaFile>),
235+
MediaFile(MediaFile),
236+
FileNotFound(Uuid),
237+
}
238+
239+
218240
#[derive(Debug, Clone)]
219241
pub enum NodeEvent {
220242
PacketSent(Packet),

0 commit comments

Comments
 (0)