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.

218 lines
5.4 KiB

7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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, Symbol, SymbolRunner};
  5. pub struct List<'a> {
  6. symbols: Vec<Box<dyn Symbol + 'a>>,
  7. }
  8. impl<'a> List<'a> {
  9. pub fn new(symbols: Vec<Box<dyn Symbol + 'a>>) -> Self {
  10. List { symbols }
  11. }
  12. }
  13. impl<'a> Symbol for List<'a> {
  14. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  15. for symbol in &self.symbols {
  16. if !try!(symbol.target_reached()) {
  17. return Ok(false);
  18. }
  19. }
  20. Ok(true)
  21. }
  22. fn execute(&self) -> Result<(), Box<dyn Error>> {
  23. for symbol in &self.symbols {
  24. try!(symbol.execute());
  25. }
  26. Ok(())
  27. }
  28. fn get_prerequisites(&self) -> Vec<Resource> {
  29. let mut r = vec![];
  30. for symbol in &self.symbols {
  31. for req in symbol.get_prerequisites() {
  32. if self.provides().map_or(true, |p| !p.contains(&req)) {
  33. r.push(req)
  34. }
  35. }
  36. }
  37. r
  38. }
  39. fn provides(&self) -> Option<Vec<Resource>> {
  40. let mut r = vec![];
  41. for symbol in &self.symbols {
  42. if let Some(provides) = symbol.provides() {
  43. r.extend(provides.into_iter());
  44. }
  45. }
  46. if r.is_empty() {
  47. None
  48. } else {
  49. Some(r)
  50. }
  51. }
  52. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  53. Box::new(SymbolListAction::new(runner, &self.symbols))
  54. }
  55. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  56. where
  57. Self: 'b,
  58. {
  59. Box::new(ListAction::new(
  60. self
  61. .symbols
  62. .into_iter()
  63. .map(|s| s.into_action(runner))
  64. .collect(),
  65. ))
  66. }
  67. }
  68. impl<'a, A: 'a + Symbol, B: 'a + Symbol> From<(A, B)> for List<'a> {
  69. fn from((a, b): (A, B)) -> Self {
  70. Self::new(vec![Box::new(a), Box::new(b)])
  71. }
  72. }
  73. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol> From<(A, B, C)> for List<'a> {
  74. fn from((a, b, c): (A, B, C)) -> Self {
  75. Self::new(vec![Box::new(a), Box::new(b), Box::new(c)])
  76. }
  77. }
  78. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol> From<(A, B, C, D)>
  79. for List<'a>
  80. {
  81. fn from((a, b, c, d): (A, B, C, D)) -> Self {
  82. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d)])
  83. }
  84. }
  85. struct SymbolListAction<'a> {
  86. runner: &'a dyn SymbolRunner,
  87. symbols: &'a [Box<dyn Symbol + 'a>],
  88. }
  89. impl<'a> SymbolListAction<'a> {
  90. fn new(runner: &'a dyn SymbolRunner, symbols: &'a [Box<dyn Symbol + 'a>]) -> Self {
  91. Self { runner, symbols }
  92. }
  93. }
  94. impl<'a> Action for SymbolListAction<'a> {
  95. fn run(&self) -> Result<(), Box<dyn Error>> {
  96. for symbol in self.symbols {
  97. try!(symbol.as_action(self.runner).run());
  98. }
  99. Ok(())
  100. }
  101. }
  102. pub struct ListAction<'a> {
  103. actions: Vec<Box<dyn Action + 'a>>,
  104. }
  105. impl<'a> ListAction<'a> {
  106. pub fn new(actions: Vec<Box<dyn Action + 'a>>) -> Self {
  107. Self { actions }
  108. }
  109. }
  110. impl<'a> Action for ListAction<'a> {
  111. fn run(&self) -> Result<(), Box<dyn Error>> {
  112. for action in &self.actions {
  113. try!(action.run());
  114. }
  115. Ok(())
  116. }
  117. }
  118. impl<'a> fmt::Display for List<'a> {
  119. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  120. try!(write!(f, "List [ "));
  121. for symbol in &self.symbols {
  122. try!(write!(f, "{} ", symbol));
  123. }
  124. write!(f, "]")
  125. }
  126. }
  127. /*
  128. #[cfg(test)]
  129. mod test {
  130. use std::error::Error;
  131. use std::fmt;
  132. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  133. use symbols::hook::List;
  134. struct ErrSymbol(String);
  135. impl Symbol for ErrSymbol {
  136. fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
  137. fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
  138. }
  139. impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  140. struct OkSymbol(bool);
  141. impl Symbol for OkSymbol {
  142. fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
  143. fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
  144. }
  145. impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  146. #[test]
  147. fn first_target_reached_fails() {
  148. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
  149. assert_eq!(res.unwrap_err().description(), "first");
  150. }
  151. #[test]
  152. fn first_target_not_reached() {
  153. let res = List::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
  154. assert_eq!(res.unwrap(), false);
  155. }
  156. #[test]
  157. fn second_target_reached_fails() {
  158. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
  159. assert_eq!(res.unwrap_err().description(), "second");
  160. }
  161. #[test]
  162. fn second_target_not_reached() {
  163. let res = List::new(OkSymbol(true), OkSymbol(false)).target_reached();
  164. assert_eq!(res.unwrap(), false);
  165. }
  166. #[test]
  167. fn everything_reached() {
  168. let res = List::new(OkSymbol(true), OkSymbol(true)).target_reached();
  169. assert_eq!(res.unwrap(), true);
  170. }
  171. #[test]
  172. fn first_execute_fails() {
  173. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
  174. assert_eq!(res.unwrap_err().description(), "first");
  175. }
  176. #[test]
  177. fn second_execute_fails() {
  178. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).execute();
  179. assert_eq!(res.unwrap_err().description(), "second");
  180. }
  181. #[test]
  182. fn everything_executes() {
  183. let res = List::new(OkSymbol(true), OkSymbol(true)).execute();
  184. assert_eq!(res.unwrap(), ());
  185. }
  186. }
  187. */