pddl/types/typed.rs
1//! Contains typed elements.
2
3use crate::types::{PrimitiveType, Type};
4use std::ops::Deref;
5
6/// A typed element.
7///
8/// ## Usage
9/// Used by [`TypedList`](crate::TypedList).
10#[derive(Debug, Clone, Eq, PartialEq)]
11pub struct Typed<O>(O, Type);
12
13impl<O> Typed<O> {
14 pub const fn new(value: O, r#type: Type) -> Self {
15 Self(value, r#type)
16 }
17
18 pub const fn new_object(value: O) -> Self {
19 Self::new(value, Type::OBJECT)
20 }
21
22 /// Gets the value.
23 pub const fn value(&self) -> &O {
24 &self.0
25 }
26
27 /// Gets the assigned type.
28 pub const fn type_(&self) -> &Type {
29 &self.1
30 }
31}
32
33pub trait ToTyped<T> {
34 /// Wraps the value into a [`Typed`] as [`Type::Exactly`] the specified type.
35 ///
36 /// ## Example
37 /// ```
38 /// # use pddl::{Name, PrimitiveType, ToTyped, Type, Typed};
39 /// assert_eq!(
40 /// Name::from("kitchen").to_typed("room"),
41 /// Typed::new(Name::from("kitchen"), Type::Exactly(PrimitiveType::from("room")))
42 /// );
43 /// ```
44 fn to_typed<I: Into<Type>>(self, r#type: I) -> Typed<T>;
45
46 /// Wraps the value into a [`Typed`] as [`Type::EitherOf`] the specified types.
47 ///
48 /// ## Example
49 /// ```
50 /// # use pddl::{Name, PrimitiveType, ToTyped, Type, Typed};
51 /// assert_eq!(
52 /// Name::from("georgia").to_typed_either(["country", "state"]),
53 /// Typed::new(Name::from("georgia"), Type::EitherOf(
54 /// vec![
55 /// PrimitiveType::from("country"),
56 /// PrimitiveType::from("state")
57 /// ])
58 /// )
59 /// );
60 /// ```
61 fn to_typed_either<I: IntoIterator<Item = P>, P: Into<PrimitiveType>>(
62 self,
63 r#type: I,
64 ) -> Typed<T>;
65}
66
67impl<'a, O> From<O> for Typed<O> {
68 fn from(value: O) -> Self {
69 Typed::new_object(value)
70 }
71}
72
73impl<'a, O> Deref for Typed<O> {
74 type Target = O;
75
76 fn deref(&self) -> &Self::Target {
77 self.value()
78 }
79}