Function optional_m

Source
pub fn optional_m<M>(marker: M, token: Token) -> Token
where M: Hash,
Expand description

A variant of name with a marker assigned, making it easy to jump directly to the matched result within the token tree.

ยงExamples

We can inspect the metadata of the optional token to see if a match occurred or not. More normally, the inner token would be marked and we would check to see if the marked token exists in the tree (TokenMatch::contains_token).

use initiative_core::command::prelude::*;

#[derive(Hash)]
enum Marker {
    Optional,
}

let token = sequence([
    optional_m(Marker::Optional, keyword("badger")),
    keyword("mushroom"),
]);

// We can inspect the metadata to see that this case contains a match
let query = "badger mushroom";
let token_match = token.match_input_exact(query, &app_meta).next().await.unwrap();
assert!(token_match.find_marker(Marker::Optional).unwrap().meta_single().is_some());

// and this case does not.
let query = "mushroom";
let token_match = token.match_input_exact(query, &app_meta).next().await.unwrap();
assert!(token_match.find_marker(Marker::Optional).unwrap().meta_single().is_none());