-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add support for indexmap #527
Open
hardliner66
wants to merge
1
commit into
cobalt-org:master
Choose a base branch
from
hardliner66:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
pub mod map; | ||
mod ser; | ||
|
||
#[cfg(feature = "indexmap")] | ||
use indexmap::IndexMap; | ||
|
||
use std::collections::BTreeMap; | ||
use std::collections::HashMap; | ||
use std::fmt; | ||
|
@@ -358,6 +361,79 @@ impl<'s, O: ObjectView> fmt::Display for ObjectRender<'s, O> { | |
} | ||
} | ||
|
||
#[cfg(feature = "indexmap")] | ||
impl<K: ObjectIndex, V: ValueView> ValueView for IndexMap<K, V> { | ||
Comment on lines
+364
to
+365
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. Could you put this right after |
||
fn as_debug(&self) -> &dyn fmt::Debug { | ||
self | ||
} | ||
|
||
fn render(&self) -> DisplayCow<'_> { | ||
DisplayCow::Owned(Box::new(ObjectRender { s: self })) | ||
} | ||
fn source(&self) -> DisplayCow<'_> { | ||
DisplayCow::Owned(Box::new(ObjectSource { s: self })) | ||
} | ||
fn type_name(&self) -> &'static str { | ||
"object" | ||
} | ||
fn query_state(&self, state: State) -> bool { | ||
match state { | ||
State::Truthy => true, | ||
State::DefaultValue | State::Empty | State::Blank => self.is_empty(), | ||
} | ||
} | ||
|
||
fn to_kstr(&self) -> KStringCow<'_> { | ||
let s = ObjectRender { s: self }.to_string(); | ||
KStringCow::from_string(s) | ||
} | ||
fn to_value(&self) -> Value { | ||
Value::Object( | ||
self.iter() | ||
.map(|(k, v)| (crate::model::KString::from_ref(k.as_index()), v.to_value())) | ||
.collect(), | ||
) | ||
} | ||
|
||
fn as_object(&self) -> Option<&dyn ObjectView> { | ||
Some(self) | ||
} | ||
} | ||
|
||
#[cfg(feature = "indexmap")] | ||
impl<K: ObjectIndex, V: ValueView> ObjectView for IndexMap<K, V> { | ||
fn as_value(&self) -> &dyn ValueView { | ||
self | ||
} | ||
|
||
fn size(&self) -> i64 { | ||
self.len() as i64 | ||
} | ||
|
||
fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = KStringCow<'k>> + 'k> { | ||
let keys = IndexMap::keys(self).map(|s| s.as_index().into()); | ||
Box::new(keys) | ||
} | ||
|
||
fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn ValueView> + 'k> { | ||
let i = IndexMap::values(self).map(as_view); | ||
Box::new(i) | ||
} | ||
|
||
fn iter<'k>(&'k self) -> Box<dyn Iterator<Item = (KStringCow<'k>, &'k dyn ValueView)> + 'k> { | ||
let i = IndexMap::iter(self).map(|(k, v)| (k.as_index().into(), as_view(v))); | ||
Box::new(i) | ||
} | ||
|
||
fn contains_key(&self, index: &str) -> bool { | ||
IndexMap::contains_key(self, index) | ||
} | ||
|
||
fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> { | ||
IndexMap::get(self, index).map(as_view) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should suffix the feature with a
2
to track that this feature is for v2 (and allow us to support a v3, etc)?