sl4f_lib/file/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use serde::{Deserialize, Serialize};

/// Enum for supported file related commands.
pub enum FileMethod {
    DeleteFile,
    MakeDir,
    ReadFile,
    WriteFile,
    Stat,
}

impl std::str::FromStr for FileMethod {
    type Err = anyhow::Error;

    fn from_str(method: &str) -> Result<Self, Self::Err> {
        match method {
            "DeleteFile" => Ok(FileMethod::DeleteFile),
            "MakeDir" => Ok(FileMethod::MakeDir),
            "ReadFile" => Ok(FileMethod::ReadFile),
            "WriteFile" => Ok(FileMethod::WriteFile),
            "Stat" => Ok(FileMethod::Stat),
            _ => return Err(format_err!("Invalid File Facade method: {}", method)),
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub enum DeleteFileResult {
    NotFound,
    Success,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum MakeDirResult {
    AlreadyExists,
    Success,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum WriteFileResult {
    Success,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum StatResult {
    NotFound,
    Success(Metadata),
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Metadata {
    pub kind: NodeKind,
    pub size: u64,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    Directory,
    File,
    Other,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{json, to_value};

    #[test]
    fn serialize_stat_result_not_found() {
        assert_eq!(to_value(StatResult::NotFound).unwrap(), json!("NotFound"));
    }

    #[test]
    fn serialize_stat_result_success() {
        assert_eq!(
            to_value(StatResult::Success(Metadata { kind: NodeKind::File, size: 42 })).unwrap(),
            json!({
                "Success": {
                    "kind": "file",
                    "size": 42,
                },
            })
        );
    }
}