use core::fmt;
use crate::model::FreeSectionKind;
use crate::model::SectionKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NumPySectionKind {
Parameters,
Returns,
Yields,
Receives,
OtherParameters,
KeywordParameters,
Raises,
Warns,
Warnings,
SeeAlso,
Notes,
References,
Examples,
Attributes,
Methods,
Todo,
Attention,
Caution,
Danger,
Error,
Hint,
Important,
Tip,
Unknown,
}
impl NumPySectionKind {
pub(crate) fn entry_role(self) -> crate::parse::EntryRole {
use crate::parse::EntryRole;
match self {
Self::Parameters | Self::OtherParameters | Self::Receives | Self::KeywordParameters => EntryRole::Parameter,
Self::Returns => EntryRole::Return,
Self::Yields => EntryRole::Yield,
Self::Raises => EntryRole::Exception,
Self::Warns => EntryRole::Warning,
Self::SeeAlso => EntryRole::SeeAlsoItem,
Self::Attributes => EntryRole::Attribute,
Self::Methods => EntryRole::Method,
Self::References => EntryRole::Citation,
_ => EntryRole::FreeText,
}
}
pub const ALL: &[NumPySectionKind] = &[
Self::Parameters,
Self::Returns,
Self::Yields,
Self::Receives,
Self::OtherParameters,
Self::KeywordParameters,
Self::Raises,
Self::Warns,
Self::Warnings,
Self::SeeAlso,
Self::Notes,
Self::References,
Self::Examples,
Self::Attributes,
Self::Methods,
Self::Todo,
Self::Attention,
Self::Caution,
Self::Danger,
Self::Error,
Self::Hint,
Self::Important,
Self::Tip,
];
#[rustfmt::skip]
pub fn from_name(name: &str) -> Self {
match name {
"parameters" | "parameter" | "params" | "param" => Self::Parameters,
"arguments" | "argument" | "args" | "arg" => Self::Parameters,
"returns" | "return" => Self::Returns,
"yields" | "yield" => Self::Yields,
"receives" | "receive" => Self::Receives,
"other parameters" | "other parameter" | "other params" | "other param" => Self::OtherParameters,
"other arguments" | "other argument" | "other args" | "other arg" => Self::OtherParameters,
"keyword parameters" | "keyword parameter" | "keyword params" | "keyword param" => Self::KeywordParameters,
"keyword arguments" | "keyword argument" | "keyword args" | "keyword arg" => Self::KeywordParameters,
"raises" | "raise" => Self::Raises,
"warns" | "warn" => Self::Warns,
"warnings" | "warning" => Self::Warnings,
"see also" => Self::SeeAlso,
"notes" | "note" => Self::Notes,
"references" | "reference" => Self::References,
"examples" | "example" => Self::Examples,
"attributes" | "attribute" => Self::Attributes,
"methods" | "method" => Self::Methods,
"todo" => Self::Todo,
"attention" => Self::Attention,
"caution" => Self::Caution,
"danger" => Self::Danger,
"error" => Self::Error,
"hint" => Self::Hint,
"important" => Self::Important,
"tip" => Self::Tip,
_ => Self::Unknown,
}
}
pub fn is_known(name: &str) -> bool {
!matches!(Self::from_name(name), Self::Unknown)
}
#[rustfmt::skip]
pub fn to_section_kind(self, header_name: &str) -> SectionKind {
match self {
Self::Parameters => SectionKind::Parameters,
Self::KeywordParameters => SectionKind::KeywordParameters,
Self::OtherParameters => SectionKind::OtherParameters,
Self::Receives => SectionKind::Receives,
Self::Returns => SectionKind::Returns,
Self::Yields => SectionKind::Yields,
Self::Raises => SectionKind::Raises,
Self::Warns => SectionKind::Warns,
Self::Attributes => SectionKind::Attributes,
Self::Methods => SectionKind::Methods,
Self::SeeAlso => SectionKind::SeeAlso,
Self::References => SectionKind::References,
Self::Notes => SectionKind::FreeText(FreeSectionKind::Notes),
Self::Examples => SectionKind::FreeText(FreeSectionKind::Examples),
Self::Warnings => SectionKind::FreeText(FreeSectionKind::Warnings),
Self::Todo => SectionKind::FreeText(FreeSectionKind::Todo),
Self::Attention => SectionKind::FreeText(FreeSectionKind::Attention),
Self::Caution => SectionKind::FreeText(FreeSectionKind::Caution),
Self::Danger => SectionKind::FreeText(FreeSectionKind::Danger),
Self::Error => SectionKind::FreeText(FreeSectionKind::Error),
Self::Hint => SectionKind::FreeText(FreeSectionKind::Hint),
Self::Important => SectionKind::FreeText(FreeSectionKind::Important),
Self::Tip => SectionKind::FreeText(FreeSectionKind::Tip),
Self::Unknown => SectionKind::FreeText(FreeSectionKind::Unknown(header_name.to_owned())),
}
}
pub fn is_structured(&self) -> bool {
matches!(
self,
Self::Parameters
| Self::Returns
| Self::Yields
| Self::Receives
| Self::OtherParameters
| Self::KeywordParameters
| Self::Raises
| Self::Warns
| Self::SeeAlso
| Self::References
| Self::Attributes
| Self::Methods
)
}
pub fn is_freetext(&self) -> bool {
matches!(
self,
Self::Notes
| Self::Examples
| Self::Warnings
| Self::Todo
| Self::Attention
| Self::Caution
| Self::Danger
| Self::Error
| Self::Hint
| Self::Important
| Self::Tip
| Self::Unknown
)
}
}
impl fmt::Display for NumPySectionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Parameters => "Parameters",
Self::Returns => "Returns",
Self::Yields => "Yields",
Self::Receives => "Receives",
Self::OtherParameters => "Other Parameters",
Self::KeywordParameters => "Keyword Parameters",
Self::Raises => "Raises",
Self::Warns => "Warns",
Self::Warnings => "Warnings",
Self::SeeAlso => "See Also",
Self::Notes => "Notes",
Self::References => "References",
Self::Examples => "Examples",
Self::Attributes => "Attributes",
Self::Methods => "Methods",
Self::Todo => "Todo",
Self::Attention => "Attention",
Self::Caution => "Caution",
Self::Danger => "Danger",
Self::Error => "Error",
Self::Hint => "Hint",
Self::Important => "Important",
Self::Tip => "Tip",
Self::Unknown => "Unknown",
};
write!(f, "{}", s)
}
}