From 806a7bd93701d8634fc1cf011c8fa9b89fa80e43 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 2 Jul 2014 17:23:35 -0700 Subject: [PATCH] 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. --- src/serialization.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 for Decoder { -> Result { 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 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 {