use sway_types::{Ident, Span};
use std::{collections::HashMap, hash::Hash, sync::Arc};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Attribute {
pub name: Ident,
pub args: Vec<Ident>,
pub span: Span,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum AttributeKind {
Doc,
DocComment,
Storage,
Inline,
Test,
Payable,
}
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct AttributesMap(Arc<HashMap<AttributeKind, Vec<Attribute>>>);
impl AttributesMap {
pub fn new(attrs_map: Arc<HashMap<AttributeKind, Vec<Attribute>>>) -> AttributesMap {
AttributesMap(attrs_map)
}
pub fn first(&self) -> Option<(&AttributeKind, &Attribute)> {
let mut first: Option<(&AttributeKind, &Attribute)> = None;
for (kind, attrs) in self.iter() {
for attr in attrs {
if let Some((_, first_attr)) = first {
if attr.span.start() < first_attr.span.start() {
first = Some((kind, attr));
}
} else {
first = Some((kind, attr));
}
}
}
first
}
pub fn inner(&self) -> &HashMap<AttributeKind, Vec<Attribute>> {
&self.0
}
}
impl std::ops::Deref for AttributesMap {
type Target = Arc<HashMap<AttributeKind, Vec<Attribute>>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}