1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
use std::fs::File; use std::io::{Read, Write}; pub struct Data { pub data: Vec<u8>, } impl Data { pub fn new(data: Vec<u8>) -> Data { Data { data } } pub fn save(&self, filename: &str) -> bool { let mut f: File; match File::create(filename).ok() { None => { crate::error::show_error( format!("[ERROR]: can't create File ({})", filename), true, ); return false; } Some(ff) => f = ff, } let r = f.write(&self.data); if r.is_err() { crate::error::show_error(format!("[ERROR]: can't write to File ({})", filename), true); return false; } true } pub fn load(&mut self, filename: &str) -> bool { let mut f: File; match File::open(filename).ok() { None => { crate::error::show_error(format!("[ERROR]: can't open File ({})", filename), true); return false; } Some(ff) => f = ff, } let mut buf: Vec<u8> = Vec::new(); let r = f.read_to_end(&mut buf); if r.is_err() { crate::error::show_error(format!("[ERROR]: can't write to File ({})", filename), true); return false; } self.data = buf; true } }