summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_fdman/src/message/open_options.rs
blob: 39cc926e998e65517691a2b823b3008e4be394cd (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::path::PathBuf;

use tedge_api::Message;

use crate::file::FileGuard;

#[derive(Clone, Debug, bevy_reflect::TypeUuid)]
#[uuid = "cf82a6ed-3fc1-4011-bbf0-c79fc6fdf781"]
pub struct OpenOptions {
    append: bool,
    create: bool,
    create_new: bool,
    read: bool,
    truncate: bool,
    write: bool,
    path: PathBuf,
}

impl OpenOptions {
    pub fn new(path: PathBuf) -> Self {
        Self {
            append: false,
            create: false,
            create_new: false,
            read: false,
            truncate: false,
            write: false,
            path,
        }
    }

    pub(crate) fn path(&self) -> &PathBuf {
        &self.path
    }

    pub(crate) fn as_std(&self) -> std::fs::OpenOptions {
        let mut o = std::fs::OpenOptions::new();
        o.append(self.append);
        o.create(self.create);
        o.create_new(self.create_new);
        o.read(self.read);
        o.truncate(self.truncate);
        o.write(self.write);
        o
    }

    pub fn append(mut self, b: bool) -> Self {
        self.append = b;
        self
    }

    pub fn create(mut self, b: bool) -> Self {
        self.create = b;
        self
    }

    pub fn create_new(mut self, b: bool) -> Self {
        self.create_new = b;
        self
    }

    pub fn read(mut self, b: bool) -> Self {
        self.read = b;
        self
    }

    pub fn truncate(mut self, b: bool) -> Self {
        self.truncate = b;
        self
    }

    pub fn write(mut self, b: bool) -> Self {
        self.write = b;
        self
    }
}

impl tedge_api::Message for OpenOptions {}
impl tedge_api::message::AcceptsReplies for OpenOptions {
    type Reply = OpenOptionsResult;
}

#[derive(Debug, thiserror::Error)]
pub enum OpenOptionsError {
    #[error("FdMan Plugin error")]
    FdMan(#[from] crate::error::Error),
}

#[derive(Debug, bevy_reflect::TypeUuid)]
#[uuid = "e259bed9-14b9-4edf-ab7b-e87d454b823f"]
pub struct OpenOptionsResult {
    open_options: OpenOptions,
    result: Result<FileGuard, OpenOptionsError>,
}

impl OpenOptionsResult {
    pub(crate) fn new(
        open_options: OpenOptions,
        result: Result<FileGuard, OpenOptionsError>,
    ) -> Self {
        Self {
            open_options,
            result,
        }
    }

    pub fn open_options(&self) -> &OpenOptions {
        &self.open_options
    }

    pub fn result(&self) -> &Result<FileGuard, OpenOptionsError> {
        &self.result
    }

    pub fn into_result(self) -> Result<FileGuard, OpenOptionsError> {
        self.result
    }
}

impl Message for OpenOptionsResult {}