Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dotted keys #73

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/decor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub struct Formatted<T> {

// String representation of a key or a value
// together with a decoration.
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
pub(crate) struct Repr {
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Hash)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the derived ordering is "correct" here, it will first compare prefix, then suffixes, then raw_value

pub struct Repr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it used in pub API?

pub decor: Decor,
pub raw_value: InternalString,
}

/// A prefix and suffix,
/// including comments, whitespaces and newlines.
#[derive(Eq, PartialEq, Clone, Default, Debug, Hash)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Default, Debug, Hash)]
pub struct Decor {
pub(crate) prefix: InternalString,
pub(crate) suffix: InternalString,
Expand Down
48 changes: 46 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::decor::{Formatted, Repr};
use crate::document::Document;
use crate::table::{Item, Table};
use crate::value::{Array, DateTime, InlineTable, Value};
use crate::key::Key;
use std::str::FromStr;
use std::cell::RefCell;

use std::fmt::{Display, Formatter, Result, Write};

impl Display for Repr {
Expand Down Expand Up @@ -110,6 +114,35 @@ impl Table {
}
}

fn visit_dotted_key(
f: &mut dyn Write,
table: &Table,
key: &Key
) -> Result {
// Descend to dotted key given parts.
let mut current = table;
for p in &key.parts[.. key.parts.len() - 1] {
let next = match current.get(p.get()) {
None => return Err(std::fmt::Error),
Some(thing) => thing
};
current = match next.as_table() {
None => return Err(std::fmt::Error),
Some(thing) => thing
}
}
let real_kv = match current.items.get(key.parts[key.parts.len() - 1].get()) {
None => return Err(std::fmt::Error),
Some(thing) => thing
};


if let Item::Value(ref value) = real_kv.value {
writeln!(f, "{}={}", real_kv.key, value)?
}
Ok(())
}

fn visit_table(
f: &mut dyn Write,
table: &Table,
Expand All @@ -122,15 +155,26 @@ fn visit_table(
write!(f, "{}[[", table.decor.prefix)?;
write!(f, "{}", path.join("."))?;
writeln!(f, "]]{}", table.decor.suffix)?;
} else if !(table.implicit && table.values_len() == 0) {
} else if !(table.implicit && table.values_len() == 0) &&
!(table.implicit && table.has_dotted) {
write!(f, "{}[", table.decor.prefix)?;
write!(f, "{}", path.join("."))?;
writeln!(f, "]{}", table.decor.suffix)?;
}
// print table body
for kv in table.items.values() {
let key = Key::from_str(&kv.key.raw_value).unwrap();
if let Item::Value(ref value) = kv.value {
writeln!(f, "{}={}", kv.key, value)?;
// If this key is a dotted key it should have been
// written out in the parent table of first simple-key
// of the dotted key.
if !key.is_dotted_key() {
writeln!(f, "{}={}", kv.key, value)?;
}
} else if let Item::DottedKeyMarker(_) = kv.value {
if let Err(_e) = visit_dotted_key(f, table, &key) {
eprintln!("Something went wrong with dotted key {}, continuing.", key.get());
}
}
}
Ok(())
Expand Down
86 changes: 81 additions & 5 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::decor::InternalString;
use crate::parser;
use combine::stream::state::State;
use std::str::FromStr;
use crate::decor::Decor;

/// Key as part of a Key/Value Pair or a table header.
///
Expand All @@ -26,11 +27,36 @@ use std::str::FromStr;
///
/// To parse a key use `FromStr` trait implementation: `"string".parse::<Key>()`.
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
pub struct Key {
pub struct SimpleKey {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about SingleKey? Also the doccomments need to be updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of going a different direction and having Key and KeyPath or something.

One thing benefit of "SimpleKey" is that this is the term the grammar uses

// Repr.raw_value have things like single quotes.
pub(crate) decor: Decor,
pub(crate) raw: InternalString,
key: InternalString,
}

// Generally, a key is made up of a list of simple-key's separated by dots.
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
pub struct Key {
raw: InternalString,
pub(crate) parts: Vec<SimpleKey>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to store raw representation or is it always constructible from the parts?

}

// impl PartialEq for Key {
// fn eq(&self, other: &Self) -> bool {
// if self.key.len() != other.key.len() {
// return false;
// }

// for i in 0..self.key.len() {
// if self.key[i].raw_value != other.key.raw_value {
// return false;
// }
// }
// true
// }
// }
// impl Eq for Key {}

impl FromStr for Key {
type Err = parser::TomlError;

Expand All @@ -54,14 +80,63 @@ impl Key {
Ok((_, ref rest)) if !rest.input.is_empty() => {
Err(parser::TomlError::from_unparsed(rest.positioner, s))
}
Ok(((raw, key), _)) => Ok(Key::new(raw, key)),
Ok(((raw, parts), _)) => Ok(Key::new(raw.into(), parts)),
Err(e) => Err(parser::TomlError::new(e, s)),
}
}

pub(crate) fn new(raw: &str, key: InternalString) -> Self {
pub(crate) fn new(raw: InternalString, parts: Vec<SimpleKey>) -> Self {
Self {
raw,
parts,
}
}

/// Returns the parsed key value.
pub fn get(&self) -> InternalString {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name InternalString suggests it's not for public API, we can return a String instead, but I'd prefer a IntoString impl or something like that.

let keys_parts: Vec<_> = self.parts.iter().map(|k| k.key.clone()).collect();
keys_parts.join(".")
}

/// Returns the parsed key value with decorators.
pub fn get_with_decor(&self) -> InternalString {
let keys_parts: Vec<_> = self.parts.iter().map(|k| k.raw.clone()).collect();
keys_parts.join(".")

// same as raw()?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like it

}

/// Returns the key raw representation.
pub fn raw(&self) -> &str {
&self.raw
}

/// Get key path.
pub fn get_key_path(&self) -> &[SimpleKey] {
&self.parts
}

/// Get key path.
pub fn get_string_path(&self) -> Vec<InternalString> {
self.parts.iter().map(|r| r.key.clone()).collect()
}
Comment on lines +109 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced we need this.


pub fn len(&self) -> usize {
self.parts.len()
}

pub fn is_dotted_key(&self) -> bool {
self.parts.len() > 1
}
}


impl SimpleKey {
// TODO: repr and raw are same?
pub(crate) fn new(decor: Decor, raw: InternalString, key: InternalString) -> Self {
Self {
raw: raw.into(),
decor,
raw,
key,
}
}
Expand All @@ -77,9 +152,10 @@ impl Key {
}
}


#[doc(hidden)]
impl Into<InternalString> for Key {
fn into(self) -> InternalString {
self.key
self.get()
}
}
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![deny(missing_docs)]
#![deny(warnings)]
// TODO: fixme.
// #![deny(missing_docs)]
// #![deny(warnings)]
#![allow(dead_code)]
#![allow(missing_docs)]
#![allow(unused_imports)]

// https://github.com/Marwes/combine/issues/172
#![recursion_limit = "256"]

Expand Down
45 changes: 32 additions & 13 deletions src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use crate::document::Document;
use crate::formatted::decorated;
use crate::parser::errors::CustomError;
use crate::parser::inline_table::KEYVAL_SEP;
use crate::parser::key::key;
use crate::parser::table::table;
use crate::parser::key::key_path2;
use crate::parser::trivia::{comment, line_ending, line_trailing, newline, ws};
use crate::parser::value::value;
use crate::parser::{TomlError, TomlParser};
use crate::table::{Item, TableKeyValue};
use crate::key::{SimpleKey, Key};
use combine::char::char;
use combine::range::recognize;
use combine::stream::state::State;
Expand All @@ -32,12 +33,12 @@ toml_parser!(parse_newline, parser, {
});

toml_parser!(keyval, parser, {
parse_keyval().and_then(|(k, kv)| parser.borrow_mut().deref_mut().on_keyval(k, kv))
parse_keyval().and_then(|(k, kv)| parser.borrow_mut().deref_mut().on_keyval(&k, kv))
});

// keyval = key keyval-sep val
parser! {
fn parse_keyval['a, I]()(I) -> (InternalString, TableKeyValue)
fn parse_keyval['a, I]()(I) -> (Key, TableKeyValue)
where
[I: RangeStream<
Range = &'a str,
Expand All @@ -50,17 +51,17 @@ parser! {
From<crate::parser::errors::CustomError>
] {
(
(key(), ws()),
(key_path2(), ws()),
char(KEYVAL_SEP),
(ws(), value(), line_trailing())
).map(|(k, _, v)| {
let (pre, v, suf) = v;
let v = decorated(v, pre, suf);
let ((raw, key), suf) = k;
let (key, suf) = k;
(
key,
key.clone(),
TableKeyValue {
key: Repr::new("", raw, suf),
key: Repr::new("", key.raw(), suf),
value: Item::Value(v),
}
)
Expand Down Expand Up @@ -113,24 +114,42 @@ impl TomlParser {
self.document.trailing.push_str(e);
}

fn on_keyval(&mut self, key: InternalString, mut kv: TableKeyValue) -> Result<(), CustomError> {
fn on_keyval(&mut self, key: &Key, mut kv: TableKeyValue) -> Result<(), CustomError> {
let path = key.get_key_path();
debug_assert!(!path.is_empty());

let prefix = mem::replace(&mut self.document.trailing, InternalString::new());
kv.key.decor.prefix = prefix + &kv.key.decor.prefix;

let root = self.document.as_table_mut();
let table = Self::descend_path(root, self.current_table_path.as_slice(), 0)

// Descend to path relative to current_table_path.
let table = Self::descend_path(root, self.current_table_path.as_slice(), 0, false)
.expect("the current table path is valid; qed");

if key.is_dotted_key() {
// Insert marker key. So we know when we need to print it in display walker.
table.items.insert(key.get().to_string(), TableKeyValue {
key: kv.key.clone(),
value: Item::DottedKeyMarker(key.parts.clone()),
});
}

let table = Self::descend_path(table, &path[.. path.len() - 1], 0, true)
.expect("the table path is valid; qed");
if table.contains_key(&key) {
let last_simple_key = &path[path.len() - 1];

if table.contains_key(last_simple_key.get()) {
Err(CustomError::DuplicateKey {
key,
key: last_simple_key.get().to_string(),
table: "<unknown>".into(), // TODO: get actual table name
})
} else {
let tkv = TableKeyValue {
key: kv.key,
key: kv.key.clone(),
value: kv.value,
};
table.items.insert(key, tkv);
table.items.insert(last_simple_key.get().to_string(), tkv);
Ok(())
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/parser/inline_table.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::decor::{InternalString, Repr};
use crate::decor::{InternalString};
use crate::formatted::decorated;
use crate::parser::errors::CustomError;
use crate::parser::key::key;
use crate::parser::key::key_path2;
use crate::parser::trivia::ws;
use crate::parser::value::value;
use crate::table::{Item, TableKeyValue};
use crate::decor::Repr;
use crate::value::InlineTable;
use combine::char::char;
use combine::stream::RangeStream;
Expand Down Expand Up @@ -62,17 +63,18 @@ parse!(inline_table_keyvals() -> (&'a str, Vec<(InternalString, TableKeyValue)>)

parse!(keyval() -> (InternalString, TableKeyValue), {
(
attempt((ws(), key(), ws())),
attempt(key_path2()),
char(KEYVAL_SEP),
(ws(), value(), ws()),
).map(|(k, _, v)| {
).map(|(key, _, v)| {
let (pre, v, suf) = v;
let v = decorated(v, pre, suf);
let (pre, (raw, key), suf) = k;

(
key,
key.get(),
TableKeyValue {
key: Repr::new(pre, raw, suf),
// At least one, TODO: fix me for dotted.
key: Repr::new(key.parts[0].decor.prefix.clone(), key.parts[0].raw.clone(), key.parts[0].decor.suffix.clone()),
value: Item::Value(v),
}
)
Expand Down
Loading