use std::{fmt::Display, str::FromStr};
use winnow::{
ModalResult, Parser,
combinator::trace,
error::{ContextError, StrContext},
stream::AsChar,
token::take_till,
};
use crate::roles::SphinxType;
#[derive(Debug, PartialEq)]
pub enum StdRole {
Doc,
Label,
Term,
Cmdoption,
Pdbcommand,
Token,
Opcode,
MonitoringEvent,
Envvar,
}
impl Display for StdRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
StdRole::Doc => "doc",
StdRole::Label => "label",
StdRole::Term => "term",
StdRole::Cmdoption => "cmdoption",
StdRole::Pdbcommand => "pdbcommand",
StdRole::Token => "token",
StdRole::Opcode => "opcode",
StdRole::MonitoringEvent => "monitoring-event",
StdRole::Envvar => "envvar",
})
}
}
impl FromStr for StdRole {
type Err = ContextError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"doc" => Ok(StdRole::Doc),
"label" => Ok(StdRole::Label),
"term" => Ok(StdRole::Term),
"cmdoption" => Ok(StdRole::Cmdoption),
"pdbcommand" => Ok(StdRole::Pdbcommand),
"opcode" => Ok(StdRole::Opcode),
"token" => Ok(StdRole::Token),
"monitoring-event" => Ok(StdRole::MonitoringEvent),
"envvar" => Ok(StdRole::Envvar),
_ => Err(ContextError::new()),
}
}
}
pub(crate) fn std_role(input: &mut &str) -> ModalResult<SphinxType> {
let role = trace(
"std_role",
take_till(0.., AsChar::is_space).context(StrContext::Label("std role")),
)
.parse_to::<StdRole>()
.parse_next(input)?;
Ok(SphinxType::Std(role))
}