boseiju/ability_tree/
object.rs

1mod attached_to;
2mod object_count;
3mod object_kind;
4mod object_specifiers;
5mod previously_mentionned;
6mod self_referencing;
7mod specified_object;
8
9pub use attached_to::ObjectAttachedTo;
10pub use object_count::CountSpecifier;
11pub use object_kind::ArtifactSubtype;
12pub use object_kind::BattleSubtype;
13pub use object_kind::CardObjectKind;
14pub use object_kind::CardType;
15pub use object_kind::CreatureSubtype;
16pub use object_kind::EnchantmentSubtype;
17pub use object_kind::LandSubtype;
18pub use object_kind::ObjectKind;
19pub use object_kind::PermanentObjectKind;
20pub use object_kind::PlaneswalkerSubtype;
21pub use object_kind::SpellObjectKind;
22pub use object_kind::SpellSubtype;
23pub use object_kind::Supertype;
24pub use object_specifiers::AnotherObjectSpecifier;
25pub use object_specifiers::CastSpecifier;
26pub use object_specifiers::ControlSpecifier;
27pub use object_specifiers::NotPreviouslySelectedObjectSpecifier;
28pub use object_specifiers::ObjectSpecifier;
29pub use object_specifiers::ObjectSpecifiers;
30pub use object_specifiers::SpecifierAndList;
31pub use object_specifiers::SpecifierOrList;
32pub use object_specifiers::SpecifierOrOfAndList;
33pub use previously_mentionned::PreviouslyMentionnedObject;
34pub use self_referencing::SelfReferencingObject;
35pub use specified_object::SpecifiedObject;
36
37use crate::ability_tree::AbilityTreeNode;
38use crate::ability_tree::MAX_CHILDREN_PER_NODE;
39
40/// An object reference is a way to refer to one or more objects in the game.
41///
42/// Objects can be anything like cards, tokens, emblems, spells on the stack, etc.
43///
44/// Whenever an ability will refer to objects, they will almost always use object references.
45#[derive(serde::Serialize, serde::Deserialize)]
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub enum ObjectReference {
48    SelfReferencing(SelfReferencingObject),
49    ObjectAttachedTo(ObjectAttachedTo),
50    SpecifiedObj(SpecifiedObject),
51    PreviouslyMentionned(PreviouslyMentionnedObject),
52}
53
54#[cfg(feature = "spanned_tree")]
55impl ObjectReference {
56    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
57        match self {
58            Self::SelfReferencing(child) => child.span,
59            Self::ObjectAttachedTo(child) => child.span,
60            Self::SpecifiedObj(child) => child.span,
61            Self::PreviouslyMentionned(child) => child.span,
62        }
63    }
64}
65
66impl crate::ability_tree::AbilityTreeNode for ObjectReference {
67    fn node_id(&self) -> usize {
68        use idris::Idris;
69        crate::ability_tree::NodeKind::ObjectReference.id()
70    }
71
72    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
73        let mut children = arrayvec::ArrayVec::new_const();
74        match self {
75            Self::SelfReferencing(child) => children.push(child as &dyn AbilityTreeNode),
76            Self::ObjectAttachedTo(child) => children.push(child as &dyn AbilityTreeNode),
77            Self::SpecifiedObj(child) => children.push(child as &dyn AbilityTreeNode),
78            Self::PreviouslyMentionned(child) => children.push(child as &dyn AbilityTreeNode),
79        }
80        children
81    }
82
83    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
84        use std::io::Write;
85        write!(out, "object reference:")?;
86        out.push_final_branch()?;
87        match self {
88            Self::SelfReferencing(child) => child.display(out)?,
89            Self::ObjectAttachedTo(child) => child.display(out)?,
90            Self::SpecifiedObj(child) => child.display(out)?,
91            Self::PreviouslyMentionned(child) => child.display(out)?,
92        }
93        out.pop_branch();
94        Ok(())
95    }
96
97    fn node_tag(&self) -> &'static str {
98        "object reference"
99    }
100
101    #[cfg(feature = "spanned_tree")]
102    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
103        match self {
104            Self::SelfReferencing(child) => child.node_span(),
105            Self::ObjectAttachedTo(child) => child.node_span(),
106            Self::SpecifiedObj(child) => child.node_span(),
107            Self::PreviouslyMentionned(child) => child.node_span(),
108        }
109    }
110}
111
112#[cfg(feature = "parser")]
113impl crate::utils::DummyInit for ObjectReference {
114    fn dummy_init() -> Self {
115        Self::SelfReferencing(crate::utils::dummy())
116    }
117}