Save only module name in File, not whole module

This commit is contained in:
Matthias Beyer 2015-12-27 23:46:08 +01:00
parent f774effabb
commit 470f1b1378
3 changed files with 39 additions and 32 deletions

View File

@ -23,14 +23,18 @@ use self::header::data::*;
* Internal abstract view on a file. Does not exist on the FS and is just kept * Internal abstract view on a file. Does not exist on the FS and is just kept
* internally until it is written to disk. * internally until it is written to disk.
*/ */
pub struct File<'a> { pub struct File {
pub owning_module : &'a Module<'a>, pub owning_module_name : &'static str,
pub header : FileHeaderData, pub header : FileHeaderData,
pub data : String, pub data : String,
pub id : FileID, pub id : FileID,
} }
impl<'a> File<'a> { impl File {
pub fn owner_name(&self) -> &'static str {
self.owning_module_name
}
pub fn header(&self) -> &FileHeaderData { pub fn header(&self) -> &FileHeaderData {
&self.header &self.header
@ -48,17 +52,13 @@ impl<'a> File<'a> {
&self.id &self.id
} }
pub fn owner(&self) -> &'a Module<'a> {
self.owning_module
}
pub fn matches_with(&self, r: &Regex) -> bool { pub fn matches_with(&self, r: &Regex) -> bool {
r.is_match(&self.data[..]) || self.header.matches_with(r) r.is_match(&self.data[..]) || self.header.matches_with(r)
} }
} }
impl<'a> Display for File<'a> { impl Display for File {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, write!(fmt,
@ -66,7 +66,7 @@ impl<'a> Display for File<'a> {
FileID: '{:?}' FileID: '{:?}'
Header: '{:?}' Header: '{:?}'
Data : '{:?}'", Data : '{:?}'",
self.owning_module, self.owning_module_name,
self.header, self.header,
self.data, self.data,
self.id); self.id);
@ -75,7 +75,7 @@ impl<'a> Display for File<'a> {
} }
impl<'a> Debug for File<'a> { impl Debug for File {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, write!(fmt,
@ -83,7 +83,7 @@ impl<'a> Debug for File<'a> {
FileID: '{:?}' FileID: '{:?}'
Header: '{:?}' Header: '{:?}'
Data : '{:?}'", Data : '{:?}'",
self.owning_module, self.owning_module_name,
self.header, self.header,
self.data, self.data,
self.id); self.id);

View File

@ -14,23 +14,25 @@ use storage::file::hash::FileHash;
use storage::parser::{FileHeaderParser, Parser, ParserError}; use storage::parser::{FileHeaderParser, Parser, ParserError};
use storage::file::header::data::FileHeaderData; use storage::file::header::data::FileHeaderData;
type Cache<'a> = HashMap<FileID, RefCell<File<'a>>>; type Cache = HashMap<FileID, RefCell<File>>;
pub struct Store<'a> { pub struct Store {
cache : RefCell<Cache<'a>>, cache : RefCell<Cache>,
} }
impl<'a> Store<'a> { impl Store {
pub fn new() -> Store<'a> { pub fn new() -> Store {
Store { Store {
cache: RefCell::new(HashMap::new()), cache: RefCell::new(HashMap::new()),
} }
} }
pub fn new_file(&self, module: &'a Module<'a>) -> File<'a> { pub fn new_file(&self, module: &Module)
-> &RefCell<File>
{
let f = File { let f = File {
owning_module: module, owning_module_name: module.name(),
header: FileHeaderData::Null, header: FileHeaderData::Null,
data: String::from(""), data: String::from(""),
id: self.get_new_file_id(), id: self.get_new_file_id(),
@ -39,9 +41,14 @@ impl<'a> Store<'a> {
f f
} }
pub fn new_file_from_parser_result(&self, module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> { pub fn new_file_from_parser_result(&self,
module: &Module,
id: FileID,
header: FileHeaderData,
data: String) -> File
{
let f = File { let f = File {
owning_module: module, owning_module_name: module.name(),
header: header, header: header,
data: data, data: data,
id: id, id: id,
@ -50,9 +57,9 @@ impl<'a> Store<'a> {
f f
} }
pub fn new_file_with_header(&self, module: &'a Module<'a>, h: FileHeaderData) -> File<'a> { pub fn new_file_with_header(&self, module: &Module, h: FileHeaderData) -> File {
let f = File { let f = File {
owning_module: module, owning_module_name: module.name(),
header: h, header: h,
data: String::from(""), data: String::from(""),
id: self.get_new_file_id(), id: self.get_new_file_id(),
@ -61,9 +68,9 @@ impl<'a> Store<'a> {
f f
} }
pub fn new_file_with_data(&self, module: &'a Module<'a>, d: String) -> File<'a> { pub fn new_file_with_data(&self, module: &Module, d: String) -> File {
let f = File { let f = File {
owning_module: module, owning_module_name: module.name(),
header: FileHeaderData::Null, header: FileHeaderData::Null,
data: d, data: d,
id: self.get_new_file_id(), id: self.get_new_file_id(),
@ -72,9 +79,9 @@ impl<'a> Store<'a> {
f f
} }
pub fn new_file_with_content(&self, module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> { pub fn new_file_with_content(&self, module: &Module, h: FileHeaderData, d: String) -> File {
let f = File { let f = File {
owning_module: module, owning_module_name: module.name(),
header: h, header: h,
data: d, data: d,
id: self.get_new_file_id(), id: self.get_new_file_id(),

View File

@ -14,7 +14,7 @@ pub trait FilePrinter {
/* /*
* Print a list of files * Print a list of files
*/ */
fn print_files<'a, I: Iterator<Item = File<'a>>>(&self, files: I) { fn print_files<I: Iterator<Item = File>>(&self, files: I) {
for file in files { for file in files {
self.print_file(&file); self.print_file(&file);
} }
@ -88,7 +88,7 @@ impl FilePrinter for TablePrinter {
self.sp.print_file(f); self.sp.print_file(f);
} }
fn print_files<'a, I: Iterator<Item = File<'a>>>(&self, files: I) { fn print_files<I: Iterator<Item = File>>(&self, files: I) {
use prettytable::Table; use prettytable::Table;
use prettytable::row::Row; use prettytable::row::Row;
use prettytable::cell::Cell; use prettytable::cell::Cell;
@ -103,7 +103,7 @@ impl FilePrinter for TablePrinter {
debug!("Printing file: {:?}", file); debug!("Printing file: {:?}", file);
i += 1; i += 1;
let cell_i = Cell::new(&format!("{}", i)[..]); let cell_i = Cell::new(&format!("{}", i)[..]);
let cell_o = Cell::new(&format!("{}", file.owner().name())[..]); let cell_o = Cell::new(&format!("{}", file.owner_name())[..]);
let id : String = file.id().clone().into(); let id : String = file.id().clone().into();
let cell_id = Cell::new(&id[..]); let cell_id = Cell::new(&id[..]);