pub mod doc_comment;
pub mod ruby;
pub mod typescript;
use crate::error::Result;
use crate::schema::{Document, Entity, Language, Metadata, SymbolEntry};
use std::path::Path;
#[derive(Debug, Clone)]
pub struct ParserConfig {
pub name: String,
pub version: Option<String>,
pub source_root: String,
pub id_prefix: Option<String>,
pub language: Language,
}
impl ParserConfig {
pub fn new(name: impl Into<String>, language: Language) -> Self {
Self {
name: name.into(),
version: None,
source_root: ".".to_string(),
id_prefix: None,
language,
}
}
pub fn with_version(mut self, version: impl Into<String>) -> Self {
self.version = Some(version.into());
self
}
pub fn with_source_root(mut self, root: impl Into<String>) -> Self {
self.source_root = root.into();
self
}
pub fn with_id_prefix(mut self, prefix: impl Into<String>) -> Self {
self.id_prefix = Some(prefix.into());
self
}
}
pub trait Parser {
fn language(&self) -> Language;
fn parse_file(&mut self, path: &Path, content: &str) -> Result<Vec<Entity>>;
fn generate_symbols(&self, entities: &[Entity], parent: Option<&str>) -> Vec<SymbolEntry>;
}
pub struct ParseContext {
pub config: ParserConfig,
pub entities: Vec<Entity>,
pub symbols: Vec<SymbolEntry>,
pub files: Vec<String>,
}
impl ParseContext {
pub fn new(config: ParserConfig) -> Self {
Self {
config,
entities: Vec::new(),
symbols: Vec::new(),
files: Vec::new(),
}
}
pub fn into_document(self) -> Document {
let metadata = Metadata {
name: self.config.name,
version: self.config.version,
language: self.config.language,
generated_at: chrono::Utc::now().to_rfc3339(),
source_root: Some(self.config.source_root),
id_prefix: self.config.id_prefix,
files: self.files,
external_links: Default::default(),
};
Document {
schema: crate::SCHEMA_VERSION.to_string(),
metadata,
entities: self.entities,
symbols: self.symbols,
unresolved_references: Vec::new(),
}
}
pub fn prefix_id(&self, id: &str) -> String {
match &self.config.id_prefix {
Some(prefix) => format!("{}{}", prefix, id),
None => id.to_string(),
}
}
}
pub fn relative_path(path: &Path, source_root: &Path) -> String {
path.strip_prefix(source_root)
.unwrap_or(path)
.to_string_lossy()
.to_string()
}