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.

117 lines
3.0 KiB

7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use symbols::Symbol;
  4. pub struct List<'a> {
  5. symbols: Vec<Box<Symbol + 'a>>
  6. }
  7. impl<'a> List<'a> {
  8. pub fn new(symbols: Vec<Box<Symbol + 'a>>) -> Self {
  9. List { symbols: symbols }
  10. }
  11. }
  12. impl<'a> Symbol for List<'a> {
  13. fn target_reached(&self) -> Result<bool, Box<Error>> {
  14. for symbol in &self.symbols {
  15. match symbol.target_reached() {
  16. Ok(false) => return Ok(false),
  17. Err(e) => return Err(e),
  18. Ok(true) => {}
  19. }
  20. }
  21. Ok(true)
  22. }
  23. fn execute(&self) -> Result<(), Box<Error>> {
  24. for symbol in &self.symbols {
  25. try!(symbol.execute());
  26. }
  27. Ok(())
  28. }
  29. }
  30. impl<'a> fmt::Display for List<'a> {
  31. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  32. try!(write!(f, "List [ "));
  33. for symbol in &self.symbols {
  34. try!(write!(f, "{} ", symbol));
  35. }
  36. write!(f, "]")
  37. }
  38. }
  39. /*
  40. #[cfg(test)]
  41. mod test {
  42. use std::error::Error;
  43. use std::fmt;
  44. use symbols::Symbol;
  45. use symbols::hook::List;
  46. struct ErrSymbol(String);
  47. impl Symbol for ErrSymbol {
  48. fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
  49. fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
  50. }
  51. impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  52. struct OkSymbol(bool);
  53. impl Symbol for OkSymbol {
  54. fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
  55. fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
  56. }
  57. impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
  58. #[test]
  59. fn first_target_reached_fails() {
  60. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
  61. assert_eq!(res.unwrap_err().description(), "first");
  62. }
  63. #[test]
  64. fn first_target_not_reached() {
  65. let res = List::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
  66. assert_eq!(res.unwrap(), false);
  67. }
  68. #[test]
  69. fn second_target_reached_fails() {
  70. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
  71. assert_eq!(res.unwrap_err().description(), "second");
  72. }
  73. #[test]
  74. fn second_target_not_reached() {
  75. let res = List::new(OkSymbol(true), OkSymbol(false)).target_reached();
  76. assert_eq!(res.unwrap(), false);
  77. }
  78. #[test]
  79. fn everything_reached() {
  80. let res = List::new(OkSymbol(true), OkSymbol(true)).target_reached();
  81. assert_eq!(res.unwrap(), true);
  82. }
  83. #[test]
  84. fn first_execute_fails() {
  85. let res = List::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
  86. assert_eq!(res.unwrap_err().description(), "first");
  87. }
  88. #[test]
  89. fn second_execute_fails() {
  90. let res = List::new(OkSymbol(true), ErrSymbol("second".into())).execute();
  91. assert_eq!(res.unwrap_err().description(), "second");
  92. }
  93. #[test]
  94. fn everything_executes() {
  95. let res = List::new(OkSymbol(true), OkSymbol(true)).execute();
  96. assert_eq!(res.unwrap(), ());
  97. }
  98. }
  99. */