A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use resources::Resource;
  4. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  5. pub struct IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
  6. a: A,
  7. b: B
  8. }
  9. impl<A, B> IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
  10. pub fn new(a: A, b: B) -> Self {
  11. IfAlreadyPresent { a: a, b: b }
  12. }
  13. }
  14. impl<A, B> Symbol for IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
  15. fn target_reached(&self) -> Result<bool, Box<Error>> {
  16. self.a.target_reached().and_then(|reached| if reached { self.b.target_reached() } else { Ok(reached) })
  17. }
  18. fn execute(&self) -> Result<(), Box<Error>> {
  19. // Execute a & b if a is not reached
  20. // Execute b if a was reached and b isn't
  21. if !try!(self.a.target_reached()) {
  22. try!(self.a.execute());
  23. self.b.execute()
  24. } else if !try!(self.b.target_reached()) {
  25. self.b.execute()
  26. } else {
  27. Ok(())
  28. }
  29. }
  30. fn get_prerequisites(&self) -> Vec<Resource> {
  31. let mut r = vec![];
  32. r.extend(self.a.get_prerequisites().into_iter());
  33. r.extend(self.b.get_prerequisites().into_iter());
  34. r
  35. }
  36. fn provides(&self) -> Option<Vec<Resource>> {
  37. let mut r = vec![];
  38. if let Some(provides) = self.a.provides() {
  39. r.extend(provides.into_iter());
  40. }
  41. if let Some(provides) = self.b.provides() {
  42. r.extend(provides.into_iter());
  43. }
  44. if r.len() > 0 { Some(r) } else { None }
  45. }
  46. fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
  47. Box::new(SymbolAction::new(runner, self))
  48. }
  49. fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
  50. Box::new(OwnedSymbolAction::new(runner, *self))
  51. }
  52. }
  53. impl<A, B> fmt::Display for IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
  54. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  55. write!(f, "IfAlreadyPresent {} and then {}", self.a, self.b)
  56. }
  57. }
  58. #[cfg(test)]
  59. mod test {
  60. use std::error::Error;
  61. use std::fmt;
  62. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  63. use symbols::if_already_present::IfAlreadyPresent;
  64. struct ErrSymbol(String);
  65. impl Symbol for ErrSymbol {
  66. fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
  67. fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
  68. }
  69. impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  70. struct OkSymbol(bool);
  71. impl Symbol for OkSymbol {
  72. fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
  73. fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
  74. }
  75. impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  76. #[test]
  77. fn first_target_reached_fails() {
  78. let res = IfAlreadyPresent::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
  79. assert_eq!(res.unwrap_err().description(), "first");
  80. }
  81. #[test]
  82. fn first_target_not_reached() {
  83. let res = IfAlreadyPresent::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
  84. assert_eq!(res.unwrap(), false);
  85. }
  86. #[test]
  87. fn second_target_reached_fails() {
  88. let res = IfAlreadyPresent::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
  89. assert_eq!(res.unwrap_err().description(), "second");
  90. }
  91. #[test]
  92. fn second_target_not_reached() {
  93. let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(false)).target_reached();
  94. assert_eq!(res.unwrap(), false);
  95. }
  96. #[test]
  97. fn everything_reached() {
  98. let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(true)).target_reached();
  99. assert_eq!(res.unwrap(), true);
  100. }
  101. #[test]
  102. fn first_execute_fails() {
  103. let res = IfAlreadyPresent::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
  104. assert_eq!(res.unwrap_err().description(), "first");
  105. }
  106. #[test]
  107. fn second_execute_fails_but_isnt_run() {
  108. let res = IfAlreadyPresent::new(OkSymbol(false), ErrSymbol("second".into())).execute();
  109. assert_eq!(res.unwrap(), ());
  110. }
  111. #[test]
  112. fn second_execute_fails() {
  113. let res = IfAlreadyPresent::new(OkSymbol(true), ErrSymbol("second".into())).execute();
  114. assert_eq!(res.unwrap_err().description(), "second");
  115. }
  116. #[test]
  117. fn everything_executes() {
  118. let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(true)).execute();
  119. assert_eq!(res.unwrap(), ());
  120. }
  121. }