use crate::{
lint::{CheckAuthor, CheckNoticeAndDev, CheckTitle, ItemDiagnostics},
natspec::NatSpec,
};
use super::{ItemType, Parent, SourceItem, TextRange, Validate, ValidationOptions};
#[derive(Debug, Clone, bon::Builder)]
#[non_exhaustive]
#[builder(on(String, into))]
pub struct LibraryDefinition {
pub name: String,
pub span: TextRange,
pub natspec: Option<NatSpec>,
}
impl SourceItem for LibraryDefinition {
fn item_type(&self) -> ItemType {
ItemType::Library
}
fn parent(&self) -> Option<Parent> {
None
}
fn name(&self) -> String {
self.name.clone()
}
fn span(&self) -> TextRange {
self.span.clone()
}
}
impl Validate for LibraryDefinition {
fn validate(&self, options: &ValidationOptions) -> ItemDiagnostics {
let opts = &options.libraries;
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name(),
span: self.span(),
diags: vec![],
};
out.diags.extend(
CheckTitle::builder()
.natspec(&self.natspec)
.rule(opts.title)
.span(&self.span)
.build()
.check(),
);
out.diags.extend(
CheckAuthor::builder()
.natspec(&self.natspec)
.rule(opts.author)
.span(&self.span)
.build()
.check(),
);
out.diags.extend(
CheckNoticeAndDev::builder()
.natspec(&self.natspec)
.notice_rule(opts.notice)
.dev_rule(opts.dev)
.notice_or_dev(options.notice_or_dev)
.span(&self.span)
.build()
.check(),
);
out
}
}
#[cfg(test)]
#[cfg(feature = "solar")]
mod tests {
use std::sync::LazyLock;
use similar_asserts::assert_eq;
use crate::{
config::{ContractRules, Req},
definitions::Definition,
parser::{Parse as _, solar::SolarParser},
};
use super::*;
static OPTIONS: LazyLock<ValidationOptions> = LazyLock::new(|| {
ValidationOptions::builder()
.inheritdoc(false)
.libraries(
ContractRules::builder()
.title(Req::Required)
.author(Req::Required)
.notice(Req::Required)
.build(),
)
.build()
});
fn parse_file(contents: &str) -> LibraryDefinition {
let mut parser = SolarParser::default();
let doc = parser
.parse_document(contents.as_bytes(), None::<std::path::PathBuf>, false)
.unwrap();
doc.definitions
.into_iter()
.find_map(Definition::to_library)
.unwrap()
}
#[test]
fn test_library() {
let contents = "/// @title Library
/// @author Me
/// @notice This is a library
library Test {}";
let res = parse_file(contents).validate(&OPTIONS);
assert!(res.diags.is_empty(), "{:#?}", res.diags);
}
#[test]
fn test_library_no_natspec() {
let contents = "library Test {}";
let res = parse_file(contents).validate(&OPTIONS);
assert_eq!(res.diags.len(), 3);
assert_eq!(res.diags[0].message, "@title is missing");
assert_eq!(res.diags[1].message, "@author is missing");
assert_eq!(res.diags[2].message, "@notice is missing");
}
#[test]
fn test_library_multiline() {
let contents = "/**
* @title Library
* @author Me
* @notice This is a library
*/
library Test {}";
let res = parse_file(contents).validate(&OPTIONS);
assert!(res.diags.is_empty(), "{:#?}", res.diags);
}
#[test]
fn test_library_inheritdoc() {
let contents = "/// @inheritdoc ITest
library Test {}";
let res = parse_file(contents).validate(
&ValidationOptions::builder()
.libraries(ContractRules::builder().title(Req::Required).build())
.build(),
);
assert_eq!(res.diags.len(), 1);
assert_eq!(res.diags[0].message, "@title is missing");
}
}