Add header editing support

This commit is contained in:
Matthias Beyer 2018-03-23 15:27:26 +01:00
parent ed41922c2e
commit 13af364b76
4 changed files with 35 additions and 0 deletions

View file

@ -21,6 +21,7 @@ maintenance = { status = "actively-developed" }
[dependencies]
error-chain = "0.11"
toml = "0.4"
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }

View file

@ -29,6 +29,11 @@ pub trait Edit {
fn edit_content(&mut self, rt: &Runtime) -> Result<()>;
}
pub trait EditHeader : Edit {
fn edit_header(&mut self, rt: &Runtime) -> Result<()>;
fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()>;
}
impl Edit for String {
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
@ -46,6 +51,24 @@ impl Edit for Entry {
}
impl EditHeader for Entry {
fn edit_header(&mut self, rt: &Runtime) -> Result<()> {
let mut header = ::toml::ser::to_string_pretty(self.get_header())?;
let _ = edit_in_tmpfile(rt, &mut header)?;
let header = ::toml::de::from_str(&header)?;
*self.get_header_mut() = header;
Ok(())
}
fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()> {
let mut header_and_content = self.to_str();
let _ = edit_in_tmpfile(rt, &mut header_and_content)?;
self.replace_from_buffer(&header_and_content).map_err(EE::from)
}
}
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> {
use libimagutil::edit::edit_in_tmpfile_with_command;

View file

@ -22,6 +22,16 @@ error_chain! {
EditError, EditErrorKind, ResultExt, Result;
}
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
foreign_links {
TomlSerError(::toml::ser::Error);
TomlDeserError(::toml::de::Error);
}
errors {
IOError {
description("IO Error")

View file

@ -40,6 +40,7 @@ extern crate libimagstore;
extern crate libimagrt;
extern crate libimagutil;
#[macro_use] extern crate error_chain;
extern crate toml;
pub mod edit;
pub mod error;