equal
deleted
inserted
replaced
|
1 use std::{ |
|
2 fs::{File, OpenOptions}, |
|
3 io::{Read, Write, Result, Error, ErrorKind} |
|
4 }; |
|
5 |
|
6 pub trait HWServerIO { |
|
7 fn write_file(&mut self, name: &str, content: &str) -> Result<()>; |
|
8 fn read_file(&mut self, name: &str) -> Result<String>; |
|
9 } |
|
10 |
|
11 pub struct EmptyServerIO {} |
|
12 |
|
13 impl EmptyServerIO { |
|
14 pub fn new() -> Self { |
|
15 Self {} |
|
16 } |
|
17 } |
|
18 |
|
19 impl HWServerIO for EmptyServerIO { |
|
20 fn write_file(&mut self, _name: &str, _content: &str) -> Result<()> { |
|
21 Ok(()) |
|
22 } |
|
23 |
|
24 fn read_file(&mut self, _name: &str) -> Result<String> { |
|
25 Ok("".to_string()) |
|
26 } |
|
27 } |
|
28 |
|
29 pub struct FileServerIO {} |
|
30 |
|
31 impl FileServerIO { |
|
32 pub fn new() -> Self { |
|
33 Self {} |
|
34 } |
|
35 } |
|
36 |
|
37 impl HWServerIO for FileServerIO { |
|
38 fn write_file(&mut self, name: &str, content: &str) -> Result<()> { |
|
39 let mut writer = OpenOptions::new().create(true).write(true).open(name)?; |
|
40 writer.write_all(content.as_bytes()) |
|
41 } |
|
42 |
|
43 fn read_file(&mut self, name: &str) -> Result<String> { |
|
44 let mut reader = File::open(name)?; |
|
45 let mut result = String::new(); |
|
46 reader.read_to_string(&mut result)?; |
|
47 Ok(result) |
|
48 } |
|
49 } |