diff -r 42776184fc39 -r e335b3120f59 gameServer2/src/server/io.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gameServer2/src/server/io.rs Sun Dec 09 04:09:38 2018 +0300 @@ -0,0 +1,49 @@ +use std::{ + fs::{File, OpenOptions}, + io::{Read, Write, Result, Error, ErrorKind} +}; + +pub trait HWServerIO { + fn write_file(&mut self, name: &str, content: &str) -> Result<()>; + fn read_file(&mut self, name: &str) -> Result; +} + +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 { + 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 { + let mut reader = File::open(name)?; + let mut result = String::new(); + reader.read_to_string(&mut result)?; + Ok(result) + } +} \ No newline at end of file