-
Notifications
You must be signed in to change notification settings - Fork 505
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 AttributeSet type #2419
Merged
Merged
Add AttributeSet type #2419
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
722a877
AttributeSet type
neunenak 27648a9
PR comments
neunenak a452435
WIP PR comments
neunenak b647045
More pr comments
neunenak 0716e62
use discriminant
neunenak aca47c7
fix
neunenak 90a1102
tweak
neunenak ab3fcc4
Merge branch 'master' into attributeset
neunenak c78467a
Merge branch 'master' into attributeset
neunenak 90537ca
Implement FromIterator for AttributeSet
casey a9e568f
Merge remote-tracking branch 'origin/master' into attributeset
casey 43d4e45
Consolidate imports
casey e439bdd
Move AttributeSet into its own file
casey 1379edc
Add newline
casey 5ff8500
Merge remote-tracking branch 'origin/master' into attributeset
casey af508bf
Reform
casey b2b63ca
Amend
casey 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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use {super::*, std::collections}; | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Serialize)] | ||
pub(crate) struct AttributeSet<'src>(BTreeSet<Attribute<'src>>); | ||
|
||
impl<'src> AttributeSet<'src> { | ||
pub(crate) fn len(&self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
pub(crate) fn contains(&self, target: AttributeDiscriminant) -> bool { | ||
self.0.iter().any(|attr| attr.discriminant() == target) | ||
} | ||
|
||
pub(crate) fn get(&self, discriminant: AttributeDiscriminant) -> Option<&Attribute<'src>> { | ||
self | ||
.0 | ||
.iter() | ||
.find(|attr| discriminant == attr.discriminant()) | ||
} | ||
|
||
pub(crate) fn iter<'a>(&'a self) -> collections::btree_set::Iter<'a, Attribute<'src>> { | ||
self.0.iter() | ||
} | ||
|
||
pub(crate) fn ensure_valid_attributes( | ||
&self, | ||
item_kind: &'static str, | ||
item_token: Token<'src>, | ||
valid: &[AttributeDiscriminant], | ||
) -> Result<(), CompileError<'src>> { | ||
for attribute in &self.0 { | ||
let discriminant = attribute.discriminant(); | ||
if !valid.contains(&discriminant) { | ||
return Err(item_token.error(CompileErrorKind::InvalidAttribute { | ||
item_kind, | ||
item_name: item_token.lexeme(), | ||
attribute: attribute.clone(), | ||
})); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'src> FromIterator<Attribute<'src>> for AttributeSet<'src> { | ||
fn from_iter<T: IntoIterator<Item = attribute::Attribute<'src>>>(iter: T) -> Self { | ||
Self(iter.into_iter().collect()) | ||
} | ||
} | ||
|
||
impl<'src, 'a> IntoIterator for &'a AttributeSet<'src> { | ||
type Item = &'a Attribute<'src>; | ||
|
||
type IntoIter = collections::btree_set::Iter<'a, Attribute<'src>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.0.iter() | ||
} | ||
} |
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
Oops, something went wrong.
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.
I think we should keep the error checking in the
else
below, otherwise the set of allowed attributes is in two places, here, where we callensure_valid_attributes
, and below, in theif / else
statement.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.
If we keep the error checking here, then we would have pretty similar error-generation code here and in the
ensure_valid_attributes
method. I addedensure_valid_attributes
as a way of de-duplicating attribute error generation code. I modified the code below to add anunreachable
and hopefully make it clearer what's going on.