aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <engineering@tilde.io>2014-07-02 17:23:35 -0700
committerTim Carey-Smith <tim@spork.in>2014-07-02 17:23:35 -0700
commit806a7bd93701d8634fc1cf011c8fa9b89fa80e43 (patch)
tree8a13d81ae576f014d745d466bad9d53640633c26
parente32363c788e184ed2c2df22201b864096c053fa9 (diff)
downloadmilf-rs-806a7bd93701d8634fc1cf011c8fa9b89fa80e43.tar.gz
milf-rs-806a7bd93701d8634fc1cf011c8fa9b89fa80e43.zip
Decodes `foo-bar` into a field named `foo_bar`
This logic is specific to the Decoder, and because Rust does not support hyphenated names as identifiers, it's the only reasonable thing to do with hyphenated names in Toml.
-rw-r--r--src/serialization.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/serialization.rs b/src/serialization.rs
index 9b5ddfd..94bd201 100644
--- a/src/serialization.rs
+++ b/src/serialization.rs
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::mem;
use std::fmt;
+use std::str;
use serialize;
use {Value, Table, Array, String, Integer, Float, Boolean, Parser};
@@ -555,7 +556,10 @@ impl serialize::Decoder<DecodeError> for Decoder {
-> Result<T, DecodeError> {
let field = f_name.to_string();
let toml = match self.toml {
- Some(Table(ref mut table)) => table.pop(&field),
+ Some(Table(ref mut table)) => {
+ table.pop(&field)
+ .or_else(|| table.pop(&hyphenate(f_name)))
+ },
ref found => return Err(self.mismatch("table", found)),
};
let mut d = self.sub_decoder(toml, f_name);
@@ -694,6 +698,10 @@ impl serialize::Decoder<DecodeError> for Decoder {
}
}
+fn hyphenate(string: &str) -> String {
+ str::replace(string, "_", "-")
+}
+
impl fmt::Show for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(match self.kind {