pddl_core/parsers/terms/
variable.rs1use crate::parsers::error::R;
4use crate::parsers::is_name;
5use crate::parsers::sexpr::Node;
6use crate::types::{Name, Variable};
7
8pub(crate) fn parse_variable(node: &Node) -> R<Variable> {
10 match node.atom().and_then(|text| text.strip_prefix('?')) {
11 Some(name) if is_name(name) => Ok(Variable::from(Name::from(name))),
12 _ => Err(node.error()),
13 }
14}
15
16crate::parsers::impl_parser!(Variable => parse_variable);
17
18#[cfg(test)]
19mod tests {
20 use crate::parsers::UnwrapValue;
21 use crate::{Parser, Variable};
22 use assert2::check;
23
24 #[test]
25 fn test_parse() {
26 check!(Variable::from_str("?abcde").is_value("abcde".into()));
27 check!(Variable::from_str("?a-1_2").is_value("a-1_2".into()));
28 check!(Variable::from_str("?Z01").is_value("Z01".into()));
29 check!(Variable::from_str("?x-_-_").is_value("x-_-_".into()));
30 check!(Variable::from_str("abcde").is_err());
31 check!(Variable::from_str("?-").is_err());
32 check!(Variable::from_str("?1").is_err());
33 check!(Variable::from_str("?").is_err());
34 check!(Variable::from_str("? x").is_err());
35 }
36}