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

use std::{
    fs::{File, OpenOptions},
    io::{Error, ErrorKind, Read, Result, Write},
};

pub trait HWServerIO {
    fn write_file(&mut self, name: &str, content: &str) -> Result<()>;
    fn read_file(&mut self, name: &str) -> Result<String>;
}

pub struct EmptyServerIO {}

impl EmptyServerIO {
    pub fn new() -> Self {
        Self {}
    }
}

impl HWServerIO for EmptyServerIO {
    fn write_file(&mut self, _name: &str, _content: &str) -> Result<()> {
        Ok(())
    }

    fn read_file(&mut self, _name: &str) -> Result<String> {
        Ok("".to_string())
    }
}

pub struct FileServerIO {}

impl FileServerIO {
    pub fn new() -> Self {
        Self {}
    }
}

impl HWServerIO for FileServerIO {
    fn write_file(&mut self, name: &str, content: &str) -> Result<()> {
        let mut writer = OpenOptions::new().create(true).write(true).open(name)?;
        writer.write_all(content.as_bytes())
    }

    fn read_file(&mut self, name: &str) -> Result<String> {
        let mut reader = File::open(name)?;
        let mut result = String::new();
        reader.read_to_string(&mut result)?;
        Ok(result)
    }
}