rl_model/model/
precondition.rs1use super::*;
2use crate::parser::*;
3use std::collections::HashMap;
4
5#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Default)]
6pub struct PreconditionId(pub SkillId, pub usize);
7impl Id for PreconditionId {
8 fn index(&self) -> usize {
9 self.1
10 }
11}
12impl PreconditionId {
13 pub fn skill(&self) -> SkillId {
14 self.0
15 }
16}
17
18#[derive(Debug, Clone)]
19pub struct Precondition {
20 id: PreconditionId,
21 name: String,
22 expr: Expr,
23 position: Option<Position>,
24}
25
26impl Precondition {
27 pub fn new<S: Into<String>>(name: S, expr: Expr, position: Option<Position>) -> Self {
28 let id = PreconditionId::default();
29 let name = name.into();
30 Self {
31 id,
32 name,
33 expr,
34 position,
35 }
36 }
37
38 pub fn expr(&self) -> &Expr {
39 &self.expr
40 }
41
42 pub fn resolve_resource(&mut self, map: &HashMap<String, ResourceId>) -> Result<(), RlError> {
45 self.expr.resolve_resource(map)
46 }
47
48 pub fn resolve_state(&mut self, map: &HashMap<String, StateId>) -> Result<(), RlError> {
49 self.expr.resolve_state(map)
50 }
51}
52
53impl Named<PreconditionId> for Precondition {
54 fn id(&self) -> PreconditionId {
55 self.id
56 }
57
58 fn set_id(&mut self, id: PreconditionId) {
59 self.id = id;
60 }
61
62 fn name(&self) -> &str {
63 &self.name
64 }
65
66 fn position(&self) -> Option<Position> {
67 self.position.clone()
68 }
69}
70
71impl ToLang for Precondition {
72 fn to_lang(&self, skillset: &Skillset) -> String {
73 format!("{}: {}", self.name, self.expr.to_lang(skillset))
74 }
75}
76
77impl std::fmt::Display for Precondition {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 write!(f, "{}", self.name)
80 }
81}