1use serde::{Deserialize, Serialize};
5
6pub enum FileMethod {
8 DeleteFile,
9 MakeDir,
10 ReadFile,
11 WriteFile,
12 Stat,
13}
14
15impl std::str::FromStr for FileMethod {
16 type Err = anyhow::Error;
17
18 fn from_str(method: &str) -> Result<Self, Self::Err> {
19 match method {
20 "DeleteFile" => Ok(FileMethod::DeleteFile),
21 "MakeDir" => Ok(FileMethod::MakeDir),
22 "ReadFile" => Ok(FileMethod::ReadFile),
23 "WriteFile" => Ok(FileMethod::WriteFile),
24 "Stat" => Ok(FileMethod::Stat),
25 _ => return Err(format_err!("Invalid File Facade method: {}", method)),
26 }
27 }
28}
29
30#[derive(Serialize, Deserialize, Debug)]
31pub enum DeleteFileResult {
32 NotFound,
33 Success,
34}
35
36#[derive(Serialize, Deserialize, Debug)]
37pub enum MakeDirResult {
38 AlreadyExists,
39 Success,
40}
41
42#[derive(Serialize, Deserialize, Debug)]
43pub enum WriteFileResult {
44 Success,
45}
46
47#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
48pub enum StatResult {
49 NotFound,
50 Success(Metadata),
51}
52
53#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
54pub struct Metadata {
55 pub kind: NodeKind,
56 pub size: u64,
57}
58
59#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
60#[serde(rename_all = "snake_case")]
61pub enum NodeKind {
62 Directory,
63 File,
64 Other,
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use serde_json::{json, to_value};
71
72 #[test]
73 fn serialize_stat_result_not_found() {
74 assert_eq!(to_value(StatResult::NotFound).unwrap(), json!("NotFound"));
75 }
76
77 #[test]
78 fn serialize_stat_result_success() {
79 assert_eq!(
80 to_value(StatResult::Success(Metadata { kind: NodeKind::File, size: 42 })).unwrap(),
81 json!({
82 "Success": {
83 "kind": "file",
84 "size": 42,
85 },
86 })
87 );
88 }
89}