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 JsRole {
Module,
Function,
Method,
Class,
Data,
Attribute,
}
impl Display for JsRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
JsRole::Module => "module",
JsRole::Function => "function",
JsRole::Method => "method",
JsRole::Class => "class",
JsRole::Data => "data",
JsRole::Attribute => "attribute",
})
}
}
impl FromStr for JsRole {
type Err = ContextError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"module" => Ok(JsRole::Module),
"data" => Ok(JsRole::Data),
"function" => Ok(JsRole::Function),
"method" => Ok(JsRole::Method),
"class" => Ok(JsRole::Class),
"attribute" => Ok(JsRole::Attribute),
_ => Err(ContextError::new()),
}
}
}
pub(crate) fn js_role(input: &mut &str) -> ModalResult<SphinxType> {
let role = take_till(1.., AsChar::is_space)
.context(StrContext::Label("Js Role"))
.parse_to()
.parse_next(input)?;
Ok(SphinxType::JavaScript(role))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_sphinx_role_parsing_std_err() {
assert!(JsRole::from_str("asdf").is_err());
assert!(JsRole::from_str("doc").is_err());
assert!(JsRole::from_str("").is_err());
assert!(JsRole::from_str("::::").is_err());
assert!(JsRole::from_str(" label").is_err());
assert!(JsRole::from_str(" asdf").is_err());
}
#[test]
fn test_sphinx_type_parsing_js() -> Result<(), ContextError> {
assert_eq!(JsRole::from_str("module")?, JsRole::Module);
assert_eq!(JsRole::from_str("function")?, JsRole::Function);
assert_eq!(JsRole::from_str("method")?, JsRole::Method);
assert_eq!(JsRole::from_str("class")?, JsRole::Class);
assert_eq!(JsRole::from_str("data")?, JsRole::Data);
Ok(())
}
}