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.

32 lines
961 B

  1. use std::fs::File;
  2. use std::io::{self, BufRead};
  3. use std::path::Path;
  4. use std::string::String;
  5. fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
  6. where P: AsRef<Path>, {
  7. let file = File::open(filename)?;
  8. Ok(io::BufReader::new(file).lines())
  9. }
  10. #[derive(Debug)]
  11. pub struct Playlist {
  12. path: String,
  13. tracks: Box<Vec<String>>
  14. }
  15. impl Playlist {
  16. pub fn read(path: &str) -> Result<Playlist, io::Error> {
  17. let playlist_path: String = String::from(path);
  18. let mut playlist_tracks: Vec<String> = Vec::new();
  19. let lines = read_lines(path)?;
  20. lines.for_each(|read_line| {
  21. if read_line.is_ok() {
  22. let line = read_line.unwrap();
  23. if line.len() > 0 && !line.starts_with("#") {
  24. playlist_tracks.push(line)
  25. }
  26. }
  27. });
  28. Ok(Playlist { path: playlist_path, tracks: Box::new(playlist_tracks) })
  29. }
  30. }