rust/hedgewars-server/src/server/io.rs
author unc0rr
Mon, 10 Dec 2018 22:44:46 +0100
changeset 14436 06672690d71b
parent 14413 gameServer2/src/server/io.rs@e335b3120f59
child 14478 98ef2913ec73
permissions -rw-r--r--
Move rust server into rust folder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14413
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},
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     3
    io::{Read, Write, Result, Error, ErrorKind}
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
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    49
}