test_dsl/
arguments.rs

1//! Traits related to arguments of verbs and conditions
2
3/// A type that can be used as an argument of Verbs and Conditions
4pub trait VerbArgument: Sized + 'static {
5    /// A human-readable typename
6    ///
7    /// This is shown only in error-messages
8    const TYPE_NAME: &'static str;
9
10    /// Convert from a [`KdlEntry`](kdl::KdlEntry) to the value
11    ///
12    /// Implementations are free to accept more than a single way of interpreting values. E.g. a
13    /// string and a integer.
14    fn from_value(value: &kdl::KdlEntry) -> Option<Self>;
15}
16
17impl VerbArgument for String {
18    const TYPE_NAME: &'static str = "string";
19    fn from_value(value: &kdl::KdlEntry) -> Option<Self> {
20        value.value().as_string().map(ToOwned::to_owned)
21    }
22}
23
24impl VerbArgument for usize {
25    const TYPE_NAME: &'static str = "integer";
26    fn from_value(value: &kdl::KdlEntry) -> Option<Self> {
27        value.value().as_integer().map(|i| i as usize)
28    }
29}