1
1
mirror of https://github.com/casey/just.git synced 2024-11-22 02:09:44 +03:00

PR comments

This commit is contained in:
Greg Shuflin 2024-10-30 21:10:43 -07:00
parent 722a8776be
commit 27648a97eb
5 changed files with 31 additions and 39 deletions

View File

@ -26,7 +26,7 @@ impl<'src> Alias<'src, Name<'src>> {
impl Alias<'_> {
pub(crate) fn is_private(&self) -> bool {
self.name.lexeme().starts_with('_') || self.attributes.private()
self.name.lexeme().starts_with('_') || self.attributes.contains(AttributeDiscriminant::Private)
}
}

View File

@ -79,10 +79,14 @@ impl<'run, 'src> Analyzer<'run, 'src> {
)?;
for attribute in attributes.iter() {
if let Attribute::Doc(ref doc) = attribute {
doc_attr = Some(doc.as_ref().map(|s| s.cooked.as_ref()).unwrap_or_default());
} else if let Attribute::Group(ref group) = attribute {
groups.push(group.cooked.clone());
match attribute {
Attribute::Doc(ref doc) => {
doc_attr = Some(doc.as_ref().map(|s| s.cooked.as_ref()).unwrap_or_default());
}
Attribute::Group(ref group) => {
groups.push(group.cooked.clone());
}
_ => unreachable!(),
}
}

View File

@ -96,6 +96,10 @@ impl<'src> Attribute<'src> {
})
}
pub(crate) fn discriminant(&self) -> AttributeDiscriminant {
self.into()
}
pub(crate) fn name(&self) -> &'static str {
self.into()
}
@ -129,51 +133,34 @@ impl<'src> Display for Attribute<'src> {
}
}
#[derive(Default, Debug, Clone, PartialEq)]
pub(crate) struct AttributeSet<'src> {
inner: BTreeSet<Attribute<'src>>,
}
impl<'src> Serialize for AttributeSet<'src> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.inner.serialize(serializer)
}
}
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub(crate) struct AttributeSet<'src>(BTreeSet<Attribute<'src>>);
impl<'src> AttributeSet<'src> {
pub(crate) fn from_iter(iter: impl IntoIterator<Item = Attribute<'src>>) -> Self {
Self {
inner: iter.into_iter().collect(),
}
Self(iter.into_iter().collect())
}
pub(crate) fn count(&self) -> usize {
self.inner.len()
pub(crate) fn len(&self) -> usize {
self.0.len()
}
pub(crate) fn contains(&self, target: AttributeDiscriminant) -> bool {
self.inner.iter().any(|attr| {
self.0.iter().any(|attr| {
let discriminant: AttributeDiscriminant = attr.into();
discriminant == target
})
}
pub(crate) fn get(&self, discriminant: AttributeDiscriminant) -> Option<&Attribute<'src>> {
self.inner.iter().find(|attr| {
let item_discriminant: AttributeDiscriminant = (*attr).into();
discriminant == item_discriminant
})
self
.0
.iter()
.find(|attr| discriminant == attr.discriminant())
}
pub(crate) fn iter(&self) -> impl Iterator<Item = &Attribute<'src>> {
self.inner.iter()
}
pub(crate) fn private(&self) -> bool {
self.inner.contains(&Attribute::Private)
self.0.iter()
}
pub(crate) fn ensure_valid_attributes(
@ -182,8 +169,8 @@ impl<'src> AttributeSet<'src> {
item_token: Token<'src>,
valid: &[AttributeDiscriminant],
) -> Result<(), CompileError<'src>> {
for attribute in &self.inner {
let discriminant: AttributeDiscriminant = attribute.into();
for attribute in &self.0 {
let discriminant = attribute.discriminant();
if !valid.contains(&discriminant) {
return Err(item_token.error(CompileErrorKind::InvalidAttribute {
item_kind,

View File

@ -439,7 +439,7 @@ impl<'run, 'src> Parser<'run, 'src> {
if let Some((token, attributes)) = attributes {
return Err(token.error(CompileErrorKind::ExtraneousAttributes {
count: attributes.count(),
count: attributes.len(),
}));
}
}
@ -487,7 +487,7 @@ impl<'run, 'src> Parser<'run, 'src> {
let value = self.parse_expression()?;
self.expect_eol()?;
let private = attributes.private();
let private = attributes.contains(AttributeDiscriminant::Private);
attributes.ensure_valid_attributes("Assignment", *name, &[AttributeDiscriminant::Private])?;
Ok(Assignment {
@ -922,7 +922,8 @@ impl<'run, 'src> Parser<'run, 'src> {
}));
}
let private = name.lexeme().starts_with('_') || attributes.private();
let private =
name.lexeme().starts_with('_') || attributes.contains(AttributeDiscriminant::Private);
Ok(Recipe {
shebang: shebang || script,

View File

@ -97,7 +97,7 @@ impl<'src, D> Recipe<'src, D> {
}
pub(crate) fn is_public(&self) -> bool {
!self.private && !self.attributes.private()
!self.private && !self.attributes.contains(AttributeDiscriminant::Private)
}
pub(crate) fn is_script(&self) -> bool {