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.

44 lines
1.0 KiB

7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::fs;
  4. use std::io;
  5. use symbols::Symbol;
  6. pub struct NotASymlink<D> where D: AsRef<str> + fmt::Display {
  7. path: D
  8. }
  9. impl<D> NotASymlink<D> where D: AsRef<str> + fmt::Display {
  10. pub fn new(path: D) -> Self {
  11. NotASymlink {
  12. path: path
  13. }
  14. }
  15. }
  16. impl<D> Symbol for NotASymlink<D> where D: AsRef<str> + fmt::Display {
  17. fn target_reached(&self) -> Result<bool, Box<Error>> {
  18. let metadata = fs::symlink_metadata(self.path.as_ref());
  19. // Check if file exists
  20. if let Err(e) = metadata {
  21. return if e.kind() == io::ErrorKind::NotFound {
  22. Ok(true)
  23. } else {
  24. Err(Box::new(e))
  25. };
  26. }
  27. Ok(!metadata.unwrap().file_type().is_symlink())
  28. }
  29. fn execute(&self) -> Result<(), Box<Error>> {
  30. try!(fs::remove_file(self.path.as_ref()));
  31. Ok(())
  32. }
  33. }
  34. impl<D> fmt::Display for NotASymlink<D> where D: AsRef<str> + fmt::Display {
  35. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  36. write!(f, "NotASymlink {}", self.path)
  37. }
  38. }