plissken-core::parser::traits
Rust
Parser trait and related types for language-agnostic parsing.
This module defines the Parser trait which provides a common interface
for parsing source code from different languages.
enum ParserLanguage
Section titled “enum ParserLanguage”pub
Language identifier for parsers.
Variants
Section titled “Variants”Rust- Rust programming languagePython- Python programming language
enum Module
Section titled “enum Module”pub
A parsed module, either Rust or Python.
This enum allows parsers to return a unified type while preserving the specific module information for each language.
Variants
Section titled “Variants”Rust- A parsed Rust modulePython- A parsed Python module
Methods
Section titled “Methods”pub
fn path(&self) -> &strGet the module path.
Source
pub fn path(&self) -> &str { match self { Module::Rust(m) => &m.path, Module::Python(m) => &m.path, } }language
Section titled “language”pub
fn language(&self) -> ParserLanguageGet the language of this module.
Source
pub fn language(&self) -> ParserLanguage { match self { Module::Rust(_) => ParserLanguage::Rust, Module::Python(_) => ParserLanguage::Python, } }as_rust
Section titled “as_rust”pub
fn as_rust(&self) -> Option<&RustModule>Try to get as a Rust module.
Source
pub fn as_rust(&self) -> Option<&RustModule> { match self { Module::Rust(m) => Some(m), Module::Python(_) => None, } }as_python
Section titled “as_python”pub
fn as_python(&self) -> Option<&PythonModule>Try to get as a Python module.
Source
pub fn as_python(&self) -> Option<&PythonModule> { match self { Module::Rust(_) => None, Module::Python(m) => Some(m), } }into_rust
Section titled “into_rust”pub
fn into_rust(self) -> Option<RustModule>Convert into a Rust module, if applicable.
Source
pub fn into_rust(self) -> Option<RustModule> { match self { Module::Rust(m) => Some(m), Module::Python(_) => None, } }into_python
Section titled “into_python”pub
fn into_python(self) -> Option<PythonModule>Convert into a Python module, if applicable.
Source
pub fn into_python(self) -> Option<PythonModule> { match self { Module::Rust(_) => None, Module::Python(m) => Some(m), } }Traits
Section titled “Traits”trait Parser
Section titled “trait Parser”pub
Supertraits: Send
A language-specific documentation parser.
This trait provides a unified interface for parsing source code from different programming languages. Implementations handle the language-specific parsing logic while exposing a common API.
Examples:
use plissken_core::parser::{Parser, ParserLanguage, create_parser};
// Create a parser dynamicallylet mut parser = create_parser(ParserLanguage::Rust);let module = parser.parse_file(Path::new("src/lib.rs"))?;
// Or use concrete types directlylet mut rust_parser = RustParser::new();let rust_module = rust_parser.parse_file(Path::new("src/lib.rs"))?;Methods
Section titled “Methods”parse_file
Section titled “parse_file”fn parse_file(&mut self, path: &Path) -> Result<Module>Parse a source file and return its module representation.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
&Path |
Path to the source file to parse |
Errors:
Returns an error if the file cannot be read or contains invalid syntax.
Source
fn parse_file(&mut self, path: &Path) -> Result<Module>;parse_str
Section titled “parse_str”fn parse_str( &mut self, content: &str, virtual_path: &Path,) -> Result<Module>Parse source code from a string.
This is useful for testing or when source code is available in memory.
Parameters:
| Name | Type | Description |
|---|---|---|
content |
&str |
The source code to parse |
virtual_path |
&Path |
A path to use for error messages and source locations |
Errors:
Returns an error if the source contains invalid syntax.
Source
fn parse_str(&mut self, content: &str, virtual_path: &Path) -> Result<Module>;language
Section titled “language”fn language(&self) -> ParserLanguageThe language this parser handles.
Source
fn language(&self) -> ParserLanguage;fn name(&self) -> &'static strHuman-readable name for error messages.
Source
fn name(&self) -> &'static str;extensions
Section titled “extensions”fn extensions(&self) -> &'static [&'static str]File extensions this parser handles.
Source
fn extensions(&self) -> &'static [&'static str];can_parse_extension
Section titled “can_parse_extension”fn can_parse_extension(&self, ext: &str) -> boolCheck if this parser can handle the given file extension.
Source
fn can_parse_extension(&self, ext: &str) -> bool { self.extensions() .iter() .any(|e| e.eq_ignore_ascii_case(ext)) }Functions
Section titled “Functions”fn create_parser
Section titled “fn create_parser”pub
fn create_parser(language: ParserLanguage) -> Box<dyn Parser>Create a parser for the given language.
Examples:
use plissken_core::parser::{create_parser, ParserLanguage};
let mut parser = create_parser(ParserLanguage::Python);let module = parser.parse_file(Path::new("module.py"))?;Source
pub fn create_parser(language: ParserLanguage) -> Box<dyn Parser> { match language { ParserLanguage::Rust => Box::new(super::RustParser::new()), ParserLanguage::Python => Box::new(super::PythonParser::new()), }}fn parser_for_extension
Section titled “fn parser_for_extension”pub
fn parser_for_extension(ext: &str) -> Option<Box<dyn Parser>>Get a parser for the given file extension.
Returns None if the extension is not recognized.
Examples:
use plissken_core::parser::parser_for_extension;
if let Some(mut parser) = parser_for_extension("py") { let module = parser.parse_file(Path::new("module.py"))?;}Source
pub fn parser_for_extension(ext: &str) -> Option<Box<dyn Parser>> { match ext.to_lowercase().as_str() { "rs" => Some(Box::new(super::RustParser::new())), "py" | "pyi" => Some(Box::new(super::PythonParser::new())), _ => None, }}