#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Docstring {
pub summary: Option<String>,
pub extended_summary: Option<String>,
pub directives: Vec<Directive>,
pub sections: Vec<Section>,
}
impl Docstring {
pub fn deprecation(&self) -> Option<&Directive> {
self.directives.iter().find(|d| d.name == "deprecated")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Section {
pub kind: SectionKind,
pub blocks: Vec<Block>,
}
impl Section {
pub fn new(kind: SectionKind, blocks: Vec<Block>) -> Self {
Self { kind, blocks }
}
pub fn parameters(params: Vec<Parameter>) -> Self {
Self::new(
SectionKind::Parameters,
params.into_iter().map(Block::Parameter).collect(),
)
}
pub fn keyword_parameters(params: Vec<Parameter>) -> Self {
Self::new(
SectionKind::KeywordParameters,
params.into_iter().map(Block::Parameter).collect(),
)
}
pub fn other_parameters(params: Vec<Parameter>) -> Self {
Self::new(
SectionKind::OtherParameters,
params.into_iter().map(Block::Parameter).collect(),
)
}
pub fn receives(params: Vec<Parameter>) -> Self {
Self::new(
SectionKind::Receives,
params.into_iter().map(Block::Parameter).collect(),
)
}
pub fn returns(returns: Vec<Return>) -> Self {
Self::new(SectionKind::Returns, returns.into_iter().map(Block::Return).collect())
}
pub fn yields(returns: Vec<Return>) -> Self {
Self::new(SectionKind::Yields, returns.into_iter().map(Block::Return).collect())
}
pub fn raises(entries: Vec<ExceptionEntry>) -> Self {
Self::new(SectionKind::Raises, entries.into_iter().map(Block::Exception).collect())
}
pub fn warns(entries: Vec<ExceptionEntry>) -> Self {
Self::new(SectionKind::Warns, entries.into_iter().map(Block::Exception).collect())
}
pub fn attributes(attrs: Vec<Attribute>) -> Self {
Self::new(
SectionKind::Attributes,
attrs.into_iter().map(Block::Attribute).collect(),
)
}
pub fn methods(methods: Vec<Method>) -> Self {
Self::new(SectionKind::Methods, methods.into_iter().map(Block::Method).collect())
}
pub fn see_also(items: Vec<SeeAlsoEntry>) -> Self {
Self::new(SectionKind::SeeAlso, items.into_iter().map(Block::SeeAlso).collect())
}
pub fn references(refs: Vec<Reference>) -> Self {
Self::new(
SectionKind::References,
refs.into_iter().map(Block::Reference).collect(),
)
}
pub fn free_text(kind: FreeSectionKind, body: String) -> Self {
Self::new(SectionKind::FreeText(kind), vec![Block::Paragraph(body)])
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Block {
Paragraph(String),
Parameter(Parameter),
Return(Return),
Exception(ExceptionEntry),
Attribute(Attribute),
Method(Method),
SeeAlso(SeeAlsoEntry),
Reference(Reference),
}
impl Block {
pub fn as_parameter(&self) -> Option<&Parameter> {
match self {
Block::Parameter(p) => Some(p),
_ => None,
}
}
pub fn as_return(&self) -> Option<&Return> {
match self {
Block::Return(r) => Some(r),
_ => None,
}
}
pub fn as_exception(&self) -> Option<&ExceptionEntry> {
match self {
Block::Exception(e) => Some(e),
_ => None,
}
}
pub fn as_attribute(&self) -> Option<&Attribute> {
match self {
Block::Attribute(a) => Some(a),
_ => None,
}
}
pub fn as_method(&self) -> Option<&Method> {
match self {
Block::Method(m) => Some(m),
_ => None,
}
}
pub fn as_see_also(&self) -> Option<&SeeAlsoEntry> {
match self {
Block::SeeAlso(s) => Some(s),
_ => None,
}
}
pub fn as_reference(&self) -> Option<&Reference> {
match self {
Block::Reference(r) => Some(r),
_ => None,
}
}
pub fn as_paragraph(&self) -> Option<&str> {
match self {
Block::Paragraph(p) => Some(p),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SectionKind {
Parameters,
KeywordParameters,
OtherParameters,
Receives,
Returns,
Yields,
Raises,
Warns,
Attributes,
Methods,
SeeAlso,
References,
FreeText(FreeSectionKind),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FreeSectionKind {
Notes,
Examples,
Warnings,
Todo,
Attention,
Caution,
Danger,
Error,
Hint,
Important,
Tip,
Unknown(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parameter {
pub names: Vec<String>,
pub type_annotation: Option<String>,
pub description: Option<String>,
pub is_optional: bool,
pub default_value: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Return {
pub name: Option<String>,
pub type_annotation: Option<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionEntry {
pub type_name: String,
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeeAlsoEntry {
pub names: Vec<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reference {
pub label: Option<String>,
pub content: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attribute {
pub names: Vec<String>,
pub type_annotation: Option<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Method {
pub name: String,
pub type_annotation: Option<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Directive {
pub name: String,
pub argument: Option<String>,
pub description: Option<String>,
}