diff --git a/src/token.rs b/src/token.rs index fc97550..49970dd 100644 --- a/src/token.rs +++ b/src/token.rs @@ -52,16 +52,17 @@ impl From<&str> for DelimTokenType { } impl DelimTokenType { - pub fn string(&self) -> String { + #[cfg(not(tarpaulin_include))] + pub fn as_str(&self) -> &'static str { use DelimTokenType::*; match self { - OpenParen => "(".to_string(), - CloseParen => ")".to_string(), - OpenBracket => "[".to_string(), - CloseBracket => "]".to_string(), - OpenBrace => "{".to_string(), - CloseBrace => "}".to_string(), - Unknown => "??".to_string(), + OpenParen => "(", + CloseParen => ")", + OpenBracket => "[", + CloseBracket => "]", + OpenBrace => "{", + CloseBrace => "}", + Unknown => "??", } } } @@ -86,7 +87,7 @@ pub enum Token<'input> { pub fn check_op(token: Token, expected: &str) -> bool { match token { Token::Delim(op, _) => { - if op.string() == expected { + if op.as_str() == expected { return true; } } @@ -187,7 +188,7 @@ impl<'input> Token<'input> { Reference(val, _) => val.to_string(), Function(val, _) => val.to_string(), Semicolon(val, _) => val.to_string(), - Delim(ty, _) => ty.string(), + Delim(ty, _) => ty.as_str().to_string(), EOF => "EOF".to_string(), } } @@ -213,7 +214,7 @@ impl<'input> fmt::Display for Token<'input> { Function(val, span) => write!(f, "Function Token: {}, {}", val, span), String(val, span) => write!(f, "String Token: {}, {}", val, span), Semicolon(val, span) => write!(f, "Semicolon Token: {}, {}", val, span), - Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.string(), span), + Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.as_str(), span), EOF => write!(f, "EOF"), } } @@ -224,6 +225,18 @@ mod tests { use super::{DelimTokenType, Span, Token}; use rstest::rstest; + #[rstest] + #[case(DelimTokenType::OpenParen, "(")] + #[case(DelimTokenType::CloseParen, ")")] + #[case(DelimTokenType::OpenBracket, "[")] + #[case(DelimTokenType::CloseBracket, "]")] + #[case(DelimTokenType::OpenBrace, "{")] + #[case(DelimTokenType::CloseBrace, "}")] + #[case(DelimTokenType::Unknown, "??")] + fn test_delim_token_type_as_str(#[case] input: DelimTokenType, #[case] output: &str) { + assert_eq!(input.as_str(), output) + } + #[rstest] #[case("(", DelimTokenType::OpenParen)] #[case(")", DelimTokenType::CloseParen)] diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 2b7f954..0f72566 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -145,7 +145,7 @@ impl<'a> Tokenizer<'a> { self.next()?; match token { Token::Delim(bracket, _) => { - if bracket.string() == op { + if bracket.as_str() == op { return Ok(()); } }