Skip to content

Commit 266d7b5

Browse files
feat: add save_file and save_files
1 parent 547a2b2 commit 266d7b5

3 files changed

Lines changed: 74 additions & 7 deletions

File tree

src/file_conversion.rs

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,111 @@
1-
use std::fs::{self, File};
1+
use std::fs::{self, File as StdFile};
22
use std::path::Path;
3-
use crate::types::{MediaFile, TextFile};
3+
use crate::types::{MediaFile, TextFile, File};
44
use std::io::Write;
55

6+
/// Saves a [`File`] into a directory named `cached_files_{notification_from}`.
7+
///
8+
/// The function writes the [`TextFile`] content and appends a line
9+
/// for each attached [`MediaFile`].
10+
///
11+
/// # Errors
12+
///
13+
/// Returns an error if the directory cannot be created or if the file cannot
14+
/// be created or written to.
15+
pub fn save_file(notification_from: &u8, file: &File) -> std::io::Result<()> {
16+
let dir_name = format!("cached_files_{notification_from}");
17+
let dir_path = Path::new(&dir_name);
18+
fs::create_dir_all(dir_path)?;
19+
20+
let file_name = format!("{}_{}", file.text_file.id, file.text_file.title);
21+
let file_path = dir_path.join(file_name);
22+
23+
let mut f = StdFile::create(file_path)?;
24+
writeln!(f, "{}", file.text_file.content)?;
25+
for media_file in &file.media_files {
26+
writeln!(f, "MediaFile attached: {}_{}", media_file.id, media_file.title)?;
27+
}
28+
Ok(())
29+
}
30+
31+
/// Saves a list of [`File`]s into `cached_files_{notification_from}` by
32+
/// delegating to [`save_file`] for each element.
33+
///
34+
/// # Errors
35+
///
36+
/// Returns an error if saving any single file fails.
37+
pub fn save_files(notification_from: &u8, files: &Vec<File>) -> std::io::Result<()> {
38+
for file in files {
39+
save_file(notification_from, file)?;
40+
}
41+
Ok(())
42+
}
43+
44+
/// Saves a [`TextFile`] into `cached_files_{notification_from}`.
45+
///
46+
/// The function writes the text content and appends a line for each
47+
/// attached [`MediaReference`].
48+
///
49+
/// # Errors
50+
///
51+
/// Returns an error if the directory cannot be created or if the file cannot
52+
/// be created or written to.
653
pub fn save_text_file(notification_from: &u8, file: &TextFile) -> std::io::Result<()> {
7-
let dir_name = format!("cached_files_{}", notification_from);
54+
let dir_name = format!("cached_files_{notification_from}");
855
let dir_path = Path::new(&dir_name);
956
fs::create_dir_all(dir_path)?;
1057

1158
let file_name = format!("{}_{}", file.id, file.title);
1259
let file_path = dir_path.join(file_name);
1360

14-
let mut f = File::create(file_path)?;
61+
let mut f = StdFile::create(file_path)?;
1562
writeln!(f, "{}", file.content)?;
1663
for media_ref in &file.media_refs {
1764
writeln!(f, "MediaFile attached: {}_{}", media_ref.location, media_ref.id)?;
1865
}
1966
Ok(())
2067
}
2168

69+
/// Saves a list of [`TextFile`]s by delegating to [`save_text_file`].
70+
///
71+
/// # Errors
72+
///
73+
/// Returns an error if saving any single file fails.
2274
pub fn save_text_files(notification_from: &u8, files: &Vec<TextFile>) -> std::io::Result<()> {
2375
for file in files {
2476
save_text_file(notification_from, file)?;
2577
}
2678
Ok(())
2779
}
2880

81+
/// Saves a single [`MediaFile`] into `cached_files_{notification_from}`.
82+
///
83+
/// The file is written as `{id}_{title}` and its binary content flushed.
84+
///
85+
/// # Errors
86+
///
87+
/// Returns an error if the directory cannot be created or if the file cannot
88+
/// be created or written to.
2989
pub fn save_media_file(notification_from: &u8, file: &MediaFile) -> std::io::Result<()> {
30-
let dir_name = format!("cached_files_{}", notification_from);
90+
let dir_name = format!("cached_files_{notification_from}");
3191
let dir_path = Path::new(&dir_name);
3292
fs::create_dir_all(dir_path)?;
3393

3494
let file_name = format!("{}_{}", file.id, file.title);
3595
let file_path = dir_path.join(file_name);
3696

37-
let mut f = File::create(file_path)?;
97+
let mut f = StdFile::create(file_path)?;
3898
for chunk in &file.content {
3999
f.write_all(chunk)?;
40100
}
41101
Ok(())
42102
}
43103

104+
/// Saves a list of [`MediaFile`]s by delegating to [`save_media_file`].
105+
///
106+
/// # Errors
107+
///
108+
/// Returns an error if saving any single file fails.
44109
pub fn save_media_files(notification_from: &u8, files: &[MediaFile]) -> std::io::Result<()> {
45110
for file in files {
46111
save_media_file(notification_from, file)?;

src/network.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ impl Node {
5454
Self { id, kind, adjacents }
5555
}
5656

57+
#[must_use]
5758
pub fn get_id(&self) -> NodeId {
5859
self.id
5960
}
6061

62+
#[must_use]
6163
pub fn get_node_type(&self) -> NodeType {
6264
self.kind
6365
}

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl MediaFile {
129129
pub struct File {
130130
pub id: Uuid,
131131
pub text_file: TextFile,
132-
media_files: Vec<MediaFile>,
132+
pub media_files: Vec<MediaFile>,
133133
}
134134

135135
impl File {

0 commit comments

Comments
 (0)