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.

282 lines
8.8 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
5 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. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol> From<(A, B, C, D, E)>
  86. for List<'a>
  87. {
  88. fn from((a, b, c, d, e): (A, B, C, D, E)) -> Self {
  89. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e)])
  90. }
  91. }
  92. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol> From<(A, B, C, D, E, F)>
  93. for List<'a>
  94. {
  95. fn from((a, b, c, d, e, f): (A, B, C, D, E, F)) -> Self {
  96. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f)])
  97. }
  98. }
  99. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol> From<(A, B, C, D, E, F, G)>
  100. for List<'a>
  101. {
  102. fn from((a, b, c, d, e, f, g): (A, B, C, D, E, F, G)) -> Self {
  103. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g)])
  104. }
  105. }
  106. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol> From<(A, B, C, D, E, F, G, H)>
  107. for List<'a>
  108. {
  109. fn from((a, b, c, d, e, f, g, h): (A, B, C, D, E, F, G, H)) -> Self {
  110. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h)])
  111. }
  112. }
  113. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K)>
  114. for List<'a>
  115. {
  116. fn from((a, b, c, d, e, f, g, h, i, j, k): (A, B, C, D, E, F, G, H, I, J, K)) -> Self {
  117. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k)])
  118. }
  119. }
  120. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L)>
  121. for List<'a>
  122. {
  123. fn from((a, b, c, d, e, f, g, h, i, j, k, l): (A, B, C, D, E, F, G, H, I, J, K, L)) -> Self {
  124. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l)])
  125. }
  126. }
  127. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol, M: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L, M)>
  128. for List<'a>
  129. {
  130. fn from((a, b, c, d, e, f, g, h, i, j, k, l, m): (A, B, C, D, E, F, G, H, I, J, K, L, M)) -> Self {
  131. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l), Box::new(m)])
  132. }
  133. }
  134. impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol, M: 'a + Symbol, N: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L, M, N)>
  135. for List<'a>
  136. {
  137. fn from((a, b, c, d, e, f, g, h, i, j, k, l, m, n): (A, B, C, D, E, F, G, H, I, J, K, L, M, N)) -> Self {
  138. Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l), Box::new(m), Box::new(n)])
  139. }
  140. }
  141. struct SymbolListAction<'a> {
  142. runner: &'a dyn SymbolRunner,
  143. symbols: &'a [Box<dyn Symbol + 'a>],
  144. }
  145. impl<'a> SymbolListAction<'a> {
  146. fn new(runner: &'a dyn SymbolRunner, symbols: &'a [Box<dyn Symbol + 'a>]) -> Self {
  147. Self { runner, symbols }
  148. }
  149. }
  150. impl<'a> Action for SymbolListAction<'a> {
  151. fn run(&self) -> Result<(), Box<dyn Error>> {
  152. for symbol in self.symbols {
  153. try!(symbol.as_action(self.runner).run());
  154. }
  155. Ok(())
  156. }
  157. }
  158. pub struct ListAction<'a> {
  159. actions: Vec<Box<dyn Action + 'a>>,
  160. }
  161. impl<'a> ListAction<'a> {
  162. pub fn new(actions: Vec<Box<dyn Action + 'a>>) -> Self {
  163. Self { actions }
  164. }
  165. }
  166. impl<'a> Action for ListAction<'a> {
  167. fn run(&self) -> Result<(), Box<dyn Error>> {
  168. for action in &self.actions {
  169. try!(action.run());
  170. }
  171. Ok(())
  172. }
  173. }
  174. impl<'a> fmt::Display for List<'a> {
  175. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  176. try!(write!(f, "List [ "));
  177. for symbol in &self.symbols {
  178. try!(write!(f, "{} ", symbol));
  179. }
  180. write!(f, "]")
  181. }
  182. }
  183. /*
  184. #[cfg(test)]
  185. mod test {
  186. use std::error::Error;
  187. use std::fmt;
  188. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  189. use symbols::hook::List;
  190. struct ErrSymbol(String);
  191. impl Symbol for ErrSymbol {
  192. fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
  193. fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
  194. }
  195. impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  196. struct OkSymbol(bool);
  197. impl Symbol for OkSymbol {
  198. fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
  199. fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
  200. }
  201. impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  202. #[test]
  203. fn first_target_reached_fails() {
  204. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
  205. assert_eq!(res.unwrap_err().description(), "first");
  206. }
  207. #[test]
  208. fn first_target_not_reached() {
  209. let res = List::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
  210. assert_eq!(res.unwrap(), false);
  211. }
  212. #[test]
  213. fn second_target_reached_fails() {
  214. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
  215. assert_eq!(res.unwrap_err().description(), "second");
  216. }
  217. #[test]
  218. fn second_target_not_reached() {
  219. let res = List::new(OkSymbol(true), OkSymbol(false)).target_reached();
  220. assert_eq!(res.unwrap(), false);
  221. }
  222. #[test]
  223. fn everything_reached() {
  224. let res = List::new(OkSymbol(true), OkSymbol(true)).target_reached();
  225. assert_eq!(res.unwrap(), true);
  226. }
  227. #[test]
  228. fn first_execute_fails() {
  229. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
  230. assert_eq!(res.unwrap_err().description(), "first");
  231. }
  232. #[test]
  233. fn second_execute_fails() {
  234. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).execute();
  235. assert_eq!(res.unwrap_err().description(), "second");
  236. }
  237. #[test]
  238. fn everything_executes() {
  239. let res = List::new(OkSymbol(true), OkSymbol(true)).execute();
  240. assert_eq!(res.unwrap(), ());
  241. }
  242. }
  243. */