Skip to content

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.

pub

Language identifier for parsers.

  • Rust - Rust programming language
  • Python - Python programming language

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.

  • Rust - A parsed Rust module
  • Python - A parsed Python module

pub

fn path(&self) -> &str

Get the module path.

Source
pub fn path(&self) -> &str {
match self {
Module::Rust(m) => &m.path,
Module::Python(m) => &m.path,
}
}

pub

fn language(&self) -> ParserLanguage

Get the language of this module.

Source
pub fn language(&self) -> ParserLanguage {
match self {
Module::Rust(_) => ParserLanguage::Rust,
Module::Python(_) => ParserLanguage::Python,
}
}

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,
}
}

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),
}
}

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,
}
}

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),
}
}

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 dynamically
let mut parser = create_parser(ParserLanguage::Rust);
let module = parser.parse_file(Path::new("src/lib.rs"))?;
// Or use concrete types directly
let mut rust_parser = RustParser::new();
let rust_module = rust_parser.parse_file(Path::new("src/lib.rs"))?;
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>;
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>;
fn language(&self) -> ParserLanguage

The language this parser handles.

Source
fn language(&self) -> ParserLanguage;
fn name(&self) -> &'static str

Human-readable name for error messages.

Source
fn name(&self) -> &'static str;
fn extensions(&self) -> &'static [&'static str]

File extensions this parser handles.

Source
fn extensions(&self) -> &'static [&'static str];
fn can_parse_extension(&self, ext: &str) -> bool

Check 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))
}

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()),
}
}

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,
}
}