rust/hedgewars-server/src/server/io.rs
changeset 14436 06672690d71b
parent 14413 e335b3120f59
child 14478 98ef2913ec73
equal deleted inserted replaced
14435:6843c4551cde 14436:06672690d71b
       
     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 }