1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
pub mod action;
pub mod args;
pub mod blackboard;
pub mod builder;
pub mod context;
pub mod env;
pub mod forester;
pub mod rtree;

use crate::runtime::action::Tick;
use crate::runtime::blackboard::BlackBoard;
use crate::tree::TreeError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::{MutexGuard, PoisonError};

/// The major type of every result in Forester.
pub type RtResult<T> = Result<T, RuntimeError>;
pub type RtOk = Result<(), RuntimeError>;

/// The result that the node returns
#[derive(Clone, Debug, PartialEq)]
pub enum TickResult {
    Success,
    Failure(String),
    Running,
}

impl TickResult {
    pub fn success() -> TickResult {
        TickResult::Success
    }
    pub fn failure_empty() -> TickResult {
        TickResult::Failure("".to_string())
    }
    pub fn failure(reason: String) -> TickResult {
        TickResult::Failure(reason)
    }
    pub fn running() -> TickResult {
        TickResult::Running
    }
}

#[derive(Debug, PartialEq)]
pub enum RuntimeError {
    CompileError(TreeError),
    UnImplementedAction(String),
    IOError(String),
    Unexpected(String),
    WrongArgument(String),
    Stopped(String),
    RecoveryToFailure(String),
    BlackBoardError(String),
    MultiThreadError(String),
}

impl RuntimeError {
    pub fn fail(reason: String) -> Self {
        Self::RecoveryToFailure(reason)
    }

    pub fn uex(s: String) -> Self {
        Self::Unexpected(s)
    }
    pub fn bb(s: String) -> Self {
        Self::BlackBoardError(s)
    }
}

impl From<TreeError> for RuntimeError {
    fn from(value: TreeError) -> Self {
        RuntimeError::CompileError(value)
    }
}
impl From<serde_yaml::Error> for RuntimeError {
    fn from(value: serde_yaml::Error) -> Self {
        RuntimeError::IOError(value.to_string())
    }
}
impl From<serde_json::Error> for RuntimeError {
    fn from(value: serde_json::Error) -> Self {
        RuntimeError::IOError(value.to_string())
    }
}
impl From<std::io::Error> for RuntimeError {
    fn from(value: std::io::Error) -> Self {
        RuntimeError::IOError(value.to_string())
    }
}
impl<T> From<PoisonError<MutexGuard<'_, T>>> for RuntimeError {
    fn from(value: PoisonError<MutexGuard<'_, T>>) -> Self {
        RuntimeError::MultiThreadError(value.to_string())
    }
}