API Documentation
FSH Lint provides a Rust API for programmatic use.
Crate Structure
Section titled “Crate Structure”fsh-lint/├── fsh-lint-core # Core linting engine├── fsh-lint-rules # Rule engine and built-in rules└── fsh-lint-cli # Command-line interface
Using as a Library
Section titled “Using as a Library”Add to Cargo.toml
:
[dependencies]fsh-lint-core = "0.1"fsh-lint-rules = "0.1"
Basic Usage
Section titled “Basic Usage”use fsh_lint_core::{LintContext, Parser};use fsh_lint_rules::RuleRegistry;
fn main() -> Result<(), Box<dyn std::error::Error>> { // Parse FSH file let source = std::fs::read_to_string("profile.fsh")?; let ast = Parser::parse(&source)?;
// Create lint context let mut context = LintContext::new(&source, &ast);
// Load rules let registry = RuleRegistry::default();
// Run linting let diagnostics = registry.lint(&mut context)?;
// Print diagnostics for diagnostic in diagnostics { println!("{}", diagnostic); }
Ok(())}
API Reference
Section titled “API Reference”fsh-lint-core
Section titled “fsh-lint-core”Parser
Section titled “Parser”pub struct Parser;
impl Parser { pub fn parse(source: &str) -> Result<Ast, ParseError>; pub fn parse_with_recovery(source: &str) -> (Option<Ast>, Vec<ParseError>);}
LintContext
Section titled “LintContext”pub struct LintContext<'a> { source: &'a str, ast: &'a Ast, // ...}
impl<'a> LintContext<'a> { pub fn new(source: &'a str, ast: &'a Ast) -> Self; pub fn add_diagnostic(&mut self, diagnostic: Diagnostic); pub fn diagnostics(&self) -> &[Diagnostic];}
Diagnostic
Section titled “Diagnostic”pub struct Diagnostic { pub severity: Severity, pub message: String, pub location: Location, pub suggestions: Vec<Suggestion>,}
pub enum Severity { Error, Warning, Info, Hint,}
fsh-lint-rules
Section titled “fsh-lint-rules”RuleRegistry
Section titled “RuleRegistry”pub struct RuleRegistry { rules: Vec<Box<dyn Rule>>,}
impl RuleRegistry { pub fn default() -> Self; pub fn new() -> Self; pub fn add_rule(&mut self, rule: Box<dyn Rule>); pub fn lint(&self, context: &mut LintContext) -> Result<Vec<Diagnostic>>;}
Rule Trait
Section titled “Rule Trait”pub trait Rule { fn name(&self) -> &str; fn category(&self) -> RuleCategory; fn severity(&self) -> Severity; fn lint(&self, context: &mut LintContext);}
Custom Rules
Section titled “Custom Rules”Implement the Rule
trait:
use fsh_lint_core::{LintContext, Diagnostic, Severity};use fsh_lint_rules::Rule;
struct MyCustomRule;
impl Rule for MyCustomRule { fn name(&self) -> &str { "custom/my-rule" }
fn category(&self) -> RuleCategory { RuleCategory::Style }
fn severity(&self) -> Severity { Severity::Warning }
fn lint(&self, context: &mut LintContext) { // Implement linting logic for profile in context.ast().profiles() { if !profile.name().ends_with("Profile") { context.add_diagnostic(Diagnostic { severity: self.severity(), message: format!("Profile '{}' should end with 'Profile'", profile.name()), location: profile.location(), suggestions: vec![], }); } } }}
Full API Documentation
Section titled “Full API Documentation”Generate complete API docs:
cargo doc --open --no-deps -p fsh-lint-core -p fsh-lint-rules
Online documentation: https://docs.rs/fsh-lint-core