use ink_analyzer_macro::FromAST;
use ra_ap_syntax::{AstNode, SourceFile};
use crate::{utils, Contract, FromAST, FromInkAttribute, InkAttributeKind, InkPathKind};
#[derive(Debug, Clone, PartialEq, Eq, FromAST)]
pub struct InkFile {
contracts: Vec<Contract>,
ast: SourceFile,
}
impl From<SourceFile> for InkFile {
fn from(file: SourceFile) -> Self {
let mut contracts = Vec::new();
let ink_descendants = utils::ink_attrs_closest_descendants(file.syntax());
for item in ink_descendants {
if let InkAttributeKind::Path(InkPathKind::Contract) = item.kind() {
contracts.push(Contract::cast(item).expect("Should be able to cast contract"))
}
}
Self {
contracts,
ast: file,
}
}
}
impl InkFile {
pub fn parse(code: &str) -> Self {
Self::from(SourceFile::parse(code).tree())
}
pub fn contracts(&self) -> &Vec<Contract> {
&self.contracts
}
}