localizer/enums/
standart.rs

1#![cfg(feature = "std")]
2
3use std::fs::File;
4#[derive(Debug)]
5#[allow(non_camel_case_types)]
6pub enum res_keeper {
7    file(File),
8    string(String),
9    None,
10}
11impl res_keeper {
12    pub fn new_file(res: Option<File>) -> Self {
13        match res {
14            Some(file) => Self::file(file),
15            None => Self::None,
16        }
17    }
18    pub fn new_file_from_path(res: Option<&std::path::Path>) -> Self {
19        use crate::constants::STD_PATH;
20        use std::fs::OpenOptions;
21        let file = OpenOptions::new().read(true).open(match res {
22            Some(path) => path.to_str().unwrap_or(STD_PATH),
23            None => STD_PATH,
24        });
25        Self::new_file(file.ok())
26    }
27    pub fn new_string(res: Option<String>) -> Self {
28        match res {
29            Some(string) => Self::string(string),
30            None => Self::None,
31        }
32    }
33
34    pub fn set_file(&mut self, res: Option<File>) {
35        match res {
36            Some(file) => {
37                *self = Self::file(file);
38            }
39            None => {
40                *self = Self::None;
41            }
42        }
43    }
44    pub fn set_file_from_path(&mut self, path: Option<&std::path::Path>) {
45        use crate::constants::STD_PATH;
46        use std::fs::OpenOptions;
47        let file = OpenOptions::new().read(true).open(match path {
48            Some(path) => path.to_str().unwrap_or(STD_PATH),
49            None => STD_PATH,
50        });
51        self.set_file(file.ok());
52    }
53    pub fn set_string(&mut self, res: Option<String>) {
54        match res {
55            Some(string) => {
56                *self = Self::string(string);
57            }
58            None => {
59                *self = Self::None;
60            }
61        }
62    }
63
64    pub fn get_file(self) -> Option<File> {
65        match self {
66            Self::file(file) => Some(file),
67            _ => None,
68        }
69    }
70    pub fn get_string(self) -> Option<String> {
71        match self {
72            Self::string(string) => Some(string),
73            _ => None,
74        }
75    }
76    pub fn is_file(&self) -> bool {
77        matches!(self, Self::file(_))
78        /*match self {
79            Self::file(_) => true,
80            _ => false,
81        }*/
82    }
83    pub fn is_string(&self) -> bool {
84        matches!(self, Self::string(_))
85        /*match self {
86            Self::string(_) => true,
87            _ => false,
88        }*/
89    }
90}
91
92impl Default for res_keeper {
93    fn default() -> Self {
94        Self::None
95    }
96}
97
98impl From<Option<File>> for res_keeper {
99    fn from(item: Option<File>) -> Self {
100        match item {
101            Some(file) => res_keeper::file(file),
102            None => res_keeper::None,
103        }
104    }
105}
106impl From<Option<String>> for res_keeper {
107    fn from(item: Option<String>) -> Self {
108        match item {
109            Some(string) => res_keeper::string(string),
110            None => res_keeper::None,
111        }
112    }
113}
114
115impl From<Option<res_keeper>> for res_keeper {
116    fn from(item: Option<res_keeper>) -> Self {
117        match item {
118            Some(res) => res,
119            None => res_keeper::None,
120        }
121    }
122}
123
124impl crate::traits::Res for res_keeper {
125    fn get_state(&self) -> &str {
126        match self {
127            Self::file(_) => "file",
128            Self::string(_) => "string",
129            Self::None => "None",
130        }
131    }
132
133    fn stringify(&mut self) -> Option<()> {//Result<(), dyn LocError + 'static > {//
134        use std::io::{BufReader, Read};
135        match self {
136            Self::string(_) => Some(()),
137            Self::file(file) => {
138                let mut buf_reader = BufReader::new(file);
139                let mut string = String::new();
140                buf_reader.read_to_string(&mut string).ok()?; //.unwrap();//
141                *self = Self::string(string);
142                Some(())
143            }
144            Self::None => None,
145        }
146    }
147    fn get_string(self) -> Option<String> {
148        use std::io::{BufReader, Read};
149        match self {
150            Self::string(string) => Some(string),
151            Self::file(file) => {
152                let mut buf_reader = BufReader::new(file);
153                let mut string = String::new();
154                buf_reader.read_to_string(&mut string).ok()?; //.unwrap();//
155                Some(string)
156            }
157            Self::None => None,
158        }
159    }
160    fn from_string(res: Option<String>) -> Self {
161        Self::new_string(res)
162    }
163    fn from_str(res: Option<&str>) -> Self {
164        Self::new_string(res.map(|res| res.to_string()))
165    }
166    fn set_string(&mut self, res: Option<String>) {
167        self.set_string(res);
168    }
169
170    fn user(mut self, f: &dyn Fn(&mut Self)) -> Self {
171        f(&mut self);
172        self
173    }
174
175    fn get_str(&self) -> Option<&str> {
176        match self {
177            Self::string(ref string) => Some(string),
178            _ => None,
179        }
180    }
181}