use crate::parser::Stream;
use winnow::combinator::cut_err;
use winnow::error::StrContext;
use winnow::error::StrContextValue;
use winnow::token::take_while;
use winnow::{ModalResult, Parser, seq};
fn valid_code_char(c: char) -> bool {
!matches!(
c,
')' | '\'' | '(' | '[' | ']' | '{' | '}' | '<' | '>' | '\r' | '\n'
)
}
pub(crate) fn parse_txn_code<'s>(is: &mut Stream<'s>) -> ModalResult<&'s str> {
let code = seq!(
_: '(',
take_while(0..,valid_code_char),
_: cut_err(')')
.context(StrContext::Label("transaction code"))
.context(StrContext::Expected(StrContextValue::Description(
"valid characters or closing ')'
Invalid characters for code value are:
'\\'', '(', ')', '[', ']', '{', '}', '<', '>', \\r', '\\n'
"
))),
)
.parse_next(is)?;
Ok(code.0.trim())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::kernel::Settings;
#[test]
fn test_txn_code() {
let mut settings = Settings::default();
let input = "(#foo)";
let mut is = Stream {
input,
state: &mut settings,
};
let res = parse_txn_code(&mut is);
assert_eq!(res.ok(), Some("#foo"));
}
}