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.

118 lines
3.0 KiB

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