Move File-creation code to Store
This commit is contained in:
parent
cb2c512440
commit
c595f5a1e8
2 changed files with 70 additions and 88 deletions
|
@ -32,89 +32,6 @@ pub struct File<'a> {
|
||||||
|
|
||||||
impl<'a> File<'a> {
|
impl<'a> File<'a> {
|
||||||
|
|
||||||
pub fn new(module: &'a Module<'a>) -> File<'a> {
|
|
||||||
let f = File {
|
|
||||||
owning_module: module,
|
|
||||||
header: FileHeaderData::Null,
|
|
||||||
data: String::from(""),
|
|
||||||
id: File::get_new_file_id(),
|
|
||||||
};
|
|
||||||
debug!("Create new File object: {:?}", f);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_parser_result(module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> {
|
|
||||||
let f = File {
|
|
||||||
owning_module: module,
|
|
||||||
header: header,
|
|
||||||
data: data,
|
|
||||||
id: id,
|
|
||||||
};
|
|
||||||
debug!("Create new File object from parser result: {:?}", f);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_header(module: &'a Module<'a>, h: FileHeaderData) -> File<'a> {
|
|
||||||
let f = File {
|
|
||||||
owning_module: module,
|
|
||||||
header: h,
|
|
||||||
data: String::from(""),
|
|
||||||
id: File::get_new_file_id(),
|
|
||||||
};
|
|
||||||
debug!("Create new File object with header: {:?}", f);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_data(module: &'a Module<'a>, d: String) -> File<'a> {
|
|
||||||
let f = File {
|
|
||||||
owning_module: module,
|
|
||||||
header: FileHeaderData::Null,
|
|
||||||
data: d,
|
|
||||||
id: File::get_new_file_id(),
|
|
||||||
};
|
|
||||||
debug!("Create new File object with data: {:?}", f);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_content(module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> {
|
|
||||||
let f = File {
|
|
||||||
owning_module: module,
|
|
||||||
header: h,
|
|
||||||
data: d,
|
|
||||||
id: File::get_new_file_id(),
|
|
||||||
};
|
|
||||||
debug!("Create new File object with content: {:?}", f);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call editor on the file
|
|
||||||
*
|
|
||||||
* Return true if exit code from editor was good (and content was changed), false otherwise.
|
|
||||||
*/
|
|
||||||
pub fn edit(rt: &Runtime) {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call editor on the file contents, but don't include the header inside the editor call.
|
|
||||||
*
|
|
||||||
* Return true if exit code from editor was good (and content was changed), false otherwise.
|
|
||||||
*/
|
|
||||||
pub fn edit_contents(rt: &Runtime) -> bool {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make file persistent.
|
|
||||||
*
|
|
||||||
* Retreive store path from the runtime, file id is already available in self.
|
|
||||||
*
|
|
||||||
* Return true if action succeeded, false otherwise.
|
|
||||||
*/
|
|
||||||
pub fn persist(rt: &Runtime) -> bool {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn header(&self) -> FileHeaderData {
|
pub fn header(&self) -> FileHeaderData {
|
||||||
self.header.clone()
|
self.header.clone()
|
||||||
}
|
}
|
||||||
|
@ -139,11 +56,6 @@ impl<'a> File<'a> {
|
||||||
r.is_match(&self.data[..]) || self.header.matches_with(r)
|
r.is_match(&self.data[..]) || self.header.matches_with(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_new_file_id() -> FileID {
|
|
||||||
use uuid::Uuid;
|
|
||||||
let hash = FileHash::from(Uuid::new_v4().to_hyphenated_string());
|
|
||||||
FileID::new(FileIDType::UUID, hash)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Display for File<'a> {
|
impl<'a> Display for File<'a> {
|
||||||
|
|
|
@ -6,8 +6,13 @@ pub mod file;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod json;
|
pub mod json;
|
||||||
|
|
||||||
|
use module::Module;
|
||||||
use storage::file::File;
|
use storage::file::File;
|
||||||
use storage::file::id::FileID;
|
use storage::file::id::FileID;
|
||||||
|
use storage::file::id_type::FileIDType;
|
||||||
|
use storage::file::hash::FileHash;
|
||||||
|
use storage::parser::{FileHeaderParser, Parser, ParserError};
|
||||||
|
use storage::file::header::data::FileHeaderData;
|
||||||
|
|
||||||
type Cache<'a> = HashMap<FileID, RefCell<File<'a>>>;
|
type Cache<'a> = HashMap<FileID, RefCell<File<'a>>>;
|
||||||
|
|
||||||
|
@ -23,4 +28,69 @@ impl<'a> Store<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_file(&self, module: &'a Module<'a>) -> File<'a> {
|
||||||
|
let f = File {
|
||||||
|
owning_module: module,
|
||||||
|
header: FileHeaderData::Null,
|
||||||
|
data: String::from(""),
|
||||||
|
id: self.get_new_file_id(),
|
||||||
|
};
|
||||||
|
debug!("Create new File object: {:?}", f);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_file_from_parser_result(&self, module: &'a Module<'a>, id: FileID, header: FileHeaderData, data: String) -> File<'a> {
|
||||||
|
let f = File {
|
||||||
|
owning_module: module,
|
||||||
|
header: header,
|
||||||
|
data: data,
|
||||||
|
id: id,
|
||||||
|
};
|
||||||
|
debug!("Create new File object from parser result: {:?}", f);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_file_with_header(&self, module: &'a Module<'a>, h: FileHeaderData) -> File<'a> {
|
||||||
|
let f = File {
|
||||||
|
owning_module: module,
|
||||||
|
header: h,
|
||||||
|
data: String::from(""),
|
||||||
|
id: self.get_new_file_id(),
|
||||||
|
};
|
||||||
|
debug!("Create new File object with header: {:?}", f);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_file_with_data(&self, module: &'a Module<'a>, d: String) -> File<'a> {
|
||||||
|
let f = File {
|
||||||
|
owning_module: module,
|
||||||
|
header: FileHeaderData::Null,
|
||||||
|
data: d,
|
||||||
|
id: self.get_new_file_id(),
|
||||||
|
};
|
||||||
|
debug!("Create new File object with data: {:?}", f);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_file_with_content(&self, module: &'a Module<'a>, h: FileHeaderData, d: String) -> File<'a> {
|
||||||
|
let f = File {
|
||||||
|
owning_module: module,
|
||||||
|
header: h,
|
||||||
|
data: d,
|
||||||
|
id: self.get_new_file_id(),
|
||||||
|
};
|
||||||
|
debug!("Create new File object with content: {:?}", f);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn persist(&self, file: &File) -> bool {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_new_file_id(&self) -> FileID {
|
||||||
|
use uuid::Uuid;
|
||||||
|
let hash = FileHash::from(Uuid::new_v4().to_hyphenated_string());
|
||||||
|
FileID::new(FileIDType::UUID, hash)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue