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.

198 lines
4.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
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. struct SymbolListAction<'a> {
  69. runner: &'a dyn SymbolRunner,
  70. symbols: &'a [Box<dyn Symbol + 'a>],
  71. }
  72. impl<'a> SymbolListAction<'a> {
  73. fn new(runner: &'a dyn SymbolRunner, symbols: &'a [Box<dyn Symbol + 'a>]) -> Self {
  74. Self { runner, symbols }
  75. }
  76. }
  77. impl<'a> Action for SymbolListAction<'a> {
  78. fn run(&self) -> Result<(), Box<dyn Error>> {
  79. for symbol in self.symbols {
  80. try!(symbol.as_action(self.runner).run());
  81. }
  82. Ok(())
  83. }
  84. }
  85. pub struct ListAction<'a> {
  86. actions: Vec<Box<dyn Action + 'a>>,
  87. }
  88. impl<'a> ListAction<'a> {
  89. pub fn new(actions: Vec<Box<dyn Action + 'a>>) -> Self {
  90. Self { actions }
  91. }
  92. }
  93. impl<'a> Action for ListAction<'a> {
  94. fn run(&self) -> Result<(), Box<dyn Error>> {
  95. for action in &self.actions {
  96. try!(action.run());
  97. }
  98. Ok(())
  99. }
  100. }
  101. impl<'a> fmt::Display for List<'a> {
  102. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  103. try!(write!(f, "List [ "));
  104. for symbol in &self.symbols {
  105. try!(write!(f, "{} ", symbol));
  106. }
  107. write!(f, "]")
  108. }
  109. }
  110. /*
  111. #[cfg(test)]
  112. mod test {
  113. use std::error::Error;
  114. use std::fmt;
  115. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  116. use symbols::hook::List;
  117. struct ErrSymbol(String);
  118. impl Symbol for ErrSymbol {
  119. fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
  120. fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
  121. }
  122. impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  123. struct OkSymbol(bool);
  124. impl Symbol for OkSymbol {
  125. fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
  126. fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
  127. }
  128. impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  129. #[test]
  130. fn first_target_reached_fails() {
  131. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
  132. assert_eq!(res.unwrap_err().description(), "first");
  133. }
  134. #[test]
  135. fn first_target_not_reached() {
  136. let res = List::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
  137. assert_eq!(res.unwrap(), false);
  138. }
  139. #[test]
  140. fn second_target_reached_fails() {
  141. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
  142. assert_eq!(res.unwrap_err().description(), "second");
  143. }
  144. #[test]
  145. fn second_target_not_reached() {
  146. let res = List::new(OkSymbol(true), OkSymbol(false)).target_reached();
  147. assert_eq!(res.unwrap(), false);
  148. }
  149. #[test]
  150. fn everything_reached() {
  151. let res = List::new(OkSymbol(true), OkSymbol(true)).target_reached();
  152. assert_eq!(res.unwrap(), true);
  153. }
  154. #[test]
  155. fn first_execute_fails() {
  156. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
  157. assert_eq!(res.unwrap_err().description(), "first");
  158. }
  159. #[test]
  160. fn second_execute_fails() {
  161. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).execute();
  162. assert_eq!(res.unwrap_err().description(), "second");
  163. }
  164. #[test]
  165. fn everything_executes() {
  166. let res = List::new(OkSymbol(true), OkSymbol(true)).execute();
  167. assert_eq!(res.unwrap(), ());
  168. }
  169. }
  170. */