use std::{fmt::Display, str::FromStr};
use winnow::{
ModalResult, Parser,
error::{ContextError, StrContext},
stream::AsChar,
token::take_till,
};
use crate::roles::SphinxType;
#[derive(Debug, PartialEq)]
pub enum RstRole {
Directive,
Option,
Role,
}
impl Display for RstRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
RstRole::Directive => "directive",
RstRole::Option => "directive:option",
RstRole::Role => "role",
})
}
}
pub(crate) fn rst_role(input: &mut &str) -> ModalResult<SphinxType> {
let role = take_till(1.., AsChar::is_space)
.context(StrContext::Label("rst role"))
.parse_to::<RstRole>()
.parse_next(input)?;
Ok(SphinxType::ReStructuredText(role))
}
impl FromStr for RstRole {
type Err = ContextError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"directive:option" => Ok(RstRole::Option),
"directive" => Ok(RstRole::Directive),
"role" => Ok(RstRole::Role),
_ => Err(ContextError::new()),
}
}
}