cao_lang/compiler/
function.rs

1use super::Card;
2use crate::VarName;
3use std::str::FromStr;
4
5/// Cao-lang functions
6#[derive(Debug, Clone, Default)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Function {
9    pub arguments: Vec<VarName>,
10    pub cards: Vec<Card>,
11}
12
13impl Function {
14    #[must_use]
15    pub fn with_arg(mut self, name: &str) -> Self {
16        let name = VarName::from_str(name).expect("Bad variable name");
17        self.arguments.push(name);
18        self
19    }
20
21    #[must_use]
22    pub fn with_card(mut self, card: Card) -> Self {
23        self.cards.push(card);
24        self
25    }
26
27    /// overrides the existing cards
28    #[must_use]
29    pub fn with_cards<C: Into<Vec<Card>>>(mut self, cards: C) -> Self {
30        self.cards = cards.into();
31        self
32    }
33}