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
 96
 97
 98
 99
100
pub mod commands;
mod vm;

pub use vm::State;

#[derive(Debug, Copy, Clone)]
pub enum Status {
  Failure,
  Success,
  ReturnFailure,
  ReturnSuccess,
}

impl Status {
  pub fn is_return(self) -> bool {
    use Status::*;

    match self {
      Failure | Success => false,
      ReturnFailure | ReturnSuccess => true,
    }
  }

  pub fn no_return(self) -> Self {
    use Status::*;

    match self {
        x@Failure | x@Success => x,
        ReturnFailure => Failure,
        ReturnSuccess => Success,
    }
  }
}

impl std::ops::Deref for Status {
  type Target = usize;

  fn deref(&self) -> &usize {
    use Status::*;

    static ONE: usize = 1;
    static ZERO: usize = 0;
    match *self {
      Failure | ReturnFailure => &ZERO,
      Success | ReturnSuccess => &ONE,
    }
  }
}

impl std::ops::Not for Status {
  type Output = Status;

  fn not(self) -> Self::Output {
    use Status::*;

    match self {
      Failure => Success,
      Success => Failure,
      ReturnFailure => ReturnSuccess,
      ReturnSuccess => ReturnFailure,
    }
  }
}

impl std::ops::Neg for Status {
  type Output = Status;

  fn neg(self) -> Self::Output {
    use Status::*;

    match self {
      Failure => ReturnFailure,
      Success => ReturnSuccess,
      ReturnFailure => Failure,
      ReturnSuccess => Success,
    }
  }
}

impl From<bool> for Status {
  fn from(b: bool) -> Status {
    if b {
      Status::Success
    } else {
      Status::Failure
    }
  }
}

pub trait Command {
  fn get_name(&self) -> &'static str;

  fn run(&self, state: &mut State, status: Status, args: &[String]) -> Status;
}

impl std::fmt::Debug for Command {
  fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
    write!(fmt, "Command: {}", self.get_name())
  }
}