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 CRole {
Enumerator,
Enum,
Function,
FunctionParam,
Member,
Macro,
Var,
Type,
Struct,
Union,
}
impl Display for CRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
CRole::Enumerator => "enumerator",
CRole::Enum => "enum",
CRole::Function => "function",
CRole::FunctionParam => "functionParam",
CRole::Member => "member",
CRole::Macro => "macro",
CRole::Var => "var",
CRole::Type => "type",
CRole::Struct => "struct",
CRole::Union => "union",
})
}
}
impl FromStr for CRole {
type Err = ContextError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"enumerator" => Ok(CRole::Enumerator),
"enum" => Ok(CRole::Enum),
"function" => Ok(CRole::Function),
"functionParam" => Ok(CRole::FunctionParam),
"member" => Ok(CRole::Member),
"macro" => Ok(CRole::Macro),
"var" => Ok(CRole::Var),
"type" => Ok(CRole::Type),
"struct" => Ok(CRole::Struct),
"union" => Ok(CRole::Union),
_ => Err(ContextError::new()),
}
}
}
pub(crate) fn c_role(input: &mut &str) -> ModalResult<SphinxType> {
let role = take_till(1.., AsChar::is_space)
.context(StrContext::Label("c role"))
.parse_to()
.parse_next(input)?;
Ok(SphinxType::C(role))
}