Skip to content

Commit 8c976df

Browse files
add file_conversion
1 parent 4b7c8dd commit 8c976df

3 files changed

Lines changed: 39 additions & 41 deletions

File tree

src/file_conversion.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::fs;
2+
use std::path::Path;
3+
use crate::types::{MediaFile, TextFile};
4+
5+
/// Converts a file path into a `MediaFile`.
6+
///
7+
/// # Errors
8+
///
9+
/// Returns an error if the file cannot be read, parsed, or converted
10+
/// into a `MediaFile`.
11+
pub fn file_to_media_file(file_path: &str) -> Result<MediaFile, Box<dyn std::error::Error>> {
12+
let filename = Path::new(file_path)
13+
.file_name()
14+
.and_then(|name| name.to_str())
15+
.unwrap_or("unknown")
16+
.to_string();
17+
18+
let data = fs::read(file_path)?;
19+
Ok(MediaFile::from_u8(filename, &data))
20+
}
21+
22+
/// Converts a file path into a `TextFile`.
23+
///
24+
/// # Errors
25+
///
26+
/// Returns an error if the file cannot be read, parsed, or converted
27+
/// into a `TextFile`.
28+
pub fn file_to_text_file(file_path: &str) -> Result<TextFile, Box<dyn std::error::Error>> {
29+
let filename = Path::new(file_path)
30+
.file_stem()
31+
.and_then(|name| name.to_str())
32+
.unwrap_or("unknown")
33+
.to_string();
34+
35+
let content = fs::read_to_string(file_path)?;
36+
37+
Ok(TextFile::new(filename, content, vec![]))
38+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod types;
44
pub mod assembler;
55
pub mod routing_handler;
66
pub mod packet_processor;
7+
pub mod file_conversion;
78

89
pub use routing_handler::RoutingHandler;
910
pub use assembler::FragmentAssembler;

src/types.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -319,45 +319,4 @@ pub enum ServerType {
319319
ChatServer,
320320
TextServer,
321321
MediaServer,
322-
}
323-
324-
pub mod file_conversion {
325-
use super::{MediaFile, TextFile};
326-
use std::fs;
327-
use std::path::Path;
328-
329-
/// Converts a file path into a `MediaFile`.
330-
///
331-
/// # Errors
332-
///
333-
/// Returns an error if the file cannot be read, parsed, or converted
334-
/// into a `MediaFile`.
335-
pub fn file_to_media_file(file_path: &str) -> Result<MediaFile, Box<dyn std::error::Error>> {
336-
let filename = Path::new(file_path)
337-
.file_name()
338-
.and_then(|name| name.to_str())
339-
.unwrap_or("unknown")
340-
.to_string();
341-
342-
let data = fs::read(file_path)?;
343-
Ok(MediaFile::from_u8(filename, &data))
344-
}
345-
346-
/// Converts a file path into a `TextFile`.
347-
///
348-
/// # Errors
349-
///
350-
/// Returns an error if the file cannot be read, parsed, or converted
351-
/// into a `TextFile`.
352-
pub fn file_to_text_file(file_path: &str) -> Result<TextFile, Box<dyn std::error::Error>> {
353-
let filename = Path::new(file_path)
354-
.file_stem()
355-
.and_then(|name| name.to_str())
356-
.unwrap_or("unknown")
357-
.to_string();
358-
359-
let content = fs::read_to_string(file_path)?;
360-
361-
Ok(TextFile::new(filename, content, vec![]))
362-
}
363322
}

0 commit comments

Comments
 (0)