-
Notifications
You must be signed in to change notification settings - Fork 109
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
Changes from all commits
b843fe7
fc4f0cc
9b84a51
649ba4b
bf140a1
0abd56b
2357eda
cca2ec3
644455d
fafbdee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
pub struct Repr { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
/// | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking of going a different direction and having 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>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to store |
||
} | ||
|
||
// 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; | ||
|
||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the name |
||
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()? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} | ||
|
@@ -77,9 +152,10 @@ impl Key { | |
} | ||
} | ||
|
||
|
||
#[doc(hidden)] | ||
impl Into<InternalString> for Key { | ||
fn into(self) -> InternalString { | ||
self.key | ||
self.get() | ||
} | ||
} |
There was a problem hiding this comment.
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