pddl_validate/atom.rs
1//! Ground atoms: a predicate or function symbol applied to objects.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5use core::fmt;
6
7/// A predicate or function symbol applied to objects, such as `(at t1 home)` or `(fuel t1)`.
8///
9/// The validator stores every name in ASCII lowercase, because PDDL names are
10/// case-insensitive; [`Atom::new`] lowercases its input to match. Atoms order by
11/// symbol, then by arguments, so traces and diagnostics list them deterministically.
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[non_exhaustive]
14pub struct Atom {
15 /// The predicate or function symbol, in ASCII lowercase.
16 pub symbol: String,
17 /// The objects the symbol is applied to, in ASCII lowercase. Empty for a nullary symbol.
18 pub arguments: Vec<String>,
19}
20
21impl Atom {
22 /// Builds an atom, lowercasing the symbol and every argument.
23 ///
24 /// # Examples
25 ///
26 /// ```
27 /// use pddl_validate::Atom;
28 ///
29 /// let atom = Atom::new("AT", ["T1", "home"]);
30 /// assert_eq!(atom.to_string(), "(at t1 home)");
31 /// ```
32 #[must_use]
33 pub fn new<S, I, A>(symbol: S, arguments: I) -> Self
34 where
35 S: AsRef<str>,
36 I: IntoIterator<Item = A>,
37 A: AsRef<str>,
38 {
39 Self {
40 symbol: lower(symbol.as_ref()),
41 arguments: arguments.into_iter().map(|a| lower(a.as_ref())).collect(),
42 }
43 }
44
45 /// Builds an atom from names that are lowercase already.
46 pub(crate) const fn lowered(symbol: String, arguments: Vec<String>) -> Self {
47 Self { symbol, arguments }
48 }
49}
50
51impl fmt::Display for Atom {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "({}", self.symbol)?;
54 for argument in &self.arguments {
55 write!(f, " {argument}")?;
56 }
57
58 f.write_str(")")
59 }
60}
61
62/// Returns `name` in ASCII lowercase, the form in which the validator compares PDDL names.
63pub(crate) fn lower(name: &str) -> String {
64 name.to_ascii_lowercase()
65}