Minify EntryHeader code

This commit is contained in:
Matthias Beyer 2016-11-14 14:18:48 +01:00
parent 5470ffceac
commit 7cfff0f0d8

View file

@ -965,9 +965,7 @@ pub type EntryContent = String;
/// This is basically a wrapper around `toml::Table` which provides convenience to the user of the /// This is basically a wrapper around `toml::Table` which provides convenience to the user of the
/// library. /// library.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct EntryHeader { pub struct EntryHeader(Value);
header: Value,
}
pub type EntryResult<V> = RResult<V, ParserError>; pub type EntryResult<V> = RResult<V, ParserError>;
@ -977,19 +975,15 @@ pub type EntryResult<V> = RResult<V, ParserError>;
impl EntryHeader { impl EntryHeader {
pub fn new() -> EntryHeader { pub fn new() -> EntryHeader {
EntryHeader { EntryHeader(build_default_header())
header: build_default_header()
}
} }
pub fn header(&self) -> &Value { pub fn header(&self) -> &Value {
&self.header &self.0
} }
fn from_table(t: Table) -> EntryHeader { fn from_table(t: Table) -> EntryHeader {
EntryHeader { EntryHeader(Value::Table(t))
header: Value::Table(t)
}
} }
pub fn parse(s: &str) -> EntryResult<EntryHeader> { pub fn parse(s: &str) -> EntryResult<EntryHeader> {
@ -1003,7 +997,7 @@ impl EntryHeader {
} }
pub fn verify(&self) -> Result<()> { pub fn verify(&self) -> Result<()> {
match self.header { match self.0 {
Value::Table(ref t) => verify_header(&t), Value::Table(ref t) => verify_header(&t),
_ => Err(SE::new(SEK::HeaderTypeFailure, None)), _ => Err(SE::new(SEK::HeaderTypeFailure, None)),
} }
@ -1011,22 +1005,22 @@ impl EntryHeader {
#[inline] #[inline]
pub fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> { pub fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> {
self.header.insert_with_sep(spec, sep, v) self.0.insert_with_sep(spec, sep, v)
} }
#[inline] #[inline]
pub fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> { pub fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> {
self.header.set_with_sep(spec, sep, v) self.0.set_with_sep(spec, sep, v)
} }
#[inline] #[inline]
pub fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> { pub fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> {
self.header.read_with_sep(spec, splitchr) self.0.read_with_sep(spec, splitchr)
} }
#[inline] #[inline]
pub fn delete(&mut self, spec: &str) -> Result<Option<Value>> { pub fn delete(&mut self, spec: &str) -> Result<Option<Value>> {
self.header.delete(spec) self.0.delete(spec)
} }
#[inline] #[inline]
@ -1049,7 +1043,7 @@ impl EntryHeader {
impl Into<Table> for EntryHeader { impl Into<Table> for EntryHeader {
fn into(self) -> Table { fn into(self) -> Table {
match self.header { match self.0 {
Value::Table(t) => t, Value::Table(t) => t,
_ => panic!("EntryHeader is not a table!"), _ => panic!("EntryHeader is not a table!"),
} }
@ -1060,7 +1054,7 @@ impl Into<Table> for EntryHeader {
impl From<Table> for EntryHeader { impl From<Table> for EntryHeader {
fn from(t: Table) -> EntryHeader { fn from(t: Table) -> EntryHeader {
EntryHeader { header: Value::Table(t) } EntryHeader(Value::Table(t))
} }
} }
@ -1195,7 +1189,7 @@ impl Entry {
pub fn to_str(&self) -> String { pub fn to_str(&self) -> String {
format!("---\n{header}---\n{content}", format!("---\n{header}---\n{content}",
header = ::toml::encode_str(&self.header.header), header = ::toml::encode_str(&self.header.0),
content = self.content) content = self.content)
} }