rust/hedgewars-server/src/server/io.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14676 455865ccd36c
parent 14462 98ef2913ec73
child 14784 f43ab2bd76ae
permissions -rw-r--r--
Server action refactoring part 2 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14397
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     1
use std::{
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     2
    fs::{File, OpenOptions},
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     3
    io::{Error, ErrorKind, Read, Result, Write},
14397
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     4
};
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     5
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     6
pub trait HWServerIO {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     7
    fn write_file(&mut self, name: &str, content: &str) -> Result<()>;
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     8
    fn read_file(&mut self, name: &str) -> Result<String>;
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     9
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    10
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    11
pub struct EmptyServerIO {}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    12
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    13
impl EmptyServerIO {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    14
    pub fn new() -> Self {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    15
        Self {}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    16
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    17
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    18
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    19
impl HWServerIO for EmptyServerIO {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    20
    fn write_file(&mut self, _name: &str, _content: &str) -> Result<()> {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    21
        Ok(())
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    22
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    23
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    24
    fn read_file(&mut self, _name: &str) -> Result<String> {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    25
        Ok("".to_string())
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    26
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    27
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    28
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    29
pub struct FileServerIO {}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    30
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    31
impl FileServerIO {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    32
    pub fn new() -> Self {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    33
        Self {}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    34
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    35
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    36
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    37
impl HWServerIO for FileServerIO {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    38
    fn write_file(&mut self, name: &str, content: &str) -> Result<()> {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    39
        let mut writer = OpenOptions::new().create(true).write(true).open(name)?;
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    40
        writer.write_all(content.as_bytes())
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    41
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    42
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    43
    fn read_file(&mut self, name: &str) -> Result<String> {
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    44
        let mut reader = File::open(name)?;
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    45
        let mut result = String::new();
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    46
        reader.read_to_string(&mut result)?;
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    47
        Ok(result)
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    48
    }
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    49
}