ras_filesystem/domain/
file.rs1use ras_errors::AppError;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum FileExtension {
7 Md,
8 Json,
9 Jsonl,
10 Csv,
11 Html,
12 Docx,
13 Pdf,
14 Txt,
15}
16
17impl FileExtension {
18 pub fn parse(ext: &str) -> Result<Self, AppError> {
19 match ext.to_ascii_lowercase().as_str() {
20 "md" => Ok(Self::Md),
21 "json" => Ok(Self::Json),
22 "jsonl" => Ok(Self::Jsonl),
23 "csv" => Ok(Self::Csv),
24 "html" | "htm" => Ok(Self::Html),
25 "docx" => Ok(Self::Docx),
26 "pdf" => Ok(Self::Pdf),
27 "txt" => Ok(Self::Txt),
28 other => Err(AppError::BadRequest(format!(
29 "unsupported extension: {other}"
30 ))),
31 }
32 }
33
34 #[must_use]
35 pub fn as_str(self) -> &'static str {
36 match self {
37 Self::Md => "md",
38 Self::Json => "json",
39 Self::Jsonl => "jsonl",
40 Self::Csv => "csv",
41 Self::Html => "html",
42 Self::Docx => "docx",
43 Self::Pdf => "pdf",
44 Self::Txt => "txt",
45 }
46 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct FileSystemFile {
51 pub name: String,
52 pub extension: FileExtension,
53 pub bytes: Vec<u8>,
54}
55
56pub trait BaseFile: Send + Sync + 'static {
57 fn extension(&self) -> FileExtension;
58 fn allowed_extensions() -> &'static [FileExtension]
59 where
60 Self: Sized;
61 fn read(&self, raw: &[u8]) -> Result<String, AppError>;
62 fn write(&self, content: &str) -> Result<Vec<u8>, AppError>;
63}