From 55e6d65b1c94f9247c72dbdffc17d43d5cb262e0 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 4 Nov 2019 20:31:20 -0800 Subject: [PATCH] Handle case of missing metadata.json. --- src/lib.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d0091d2..d55bcf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -383,16 +383,17 @@ impl MovieLibrary { pub fn update_metadata(&self) -> Result, Error> { let path = Path::new(&self.root).join(FULL_METADATA_FILENAME); - // TODO(wathiede): if we fail to parse the old file (i.e. file not found), create an empty - // old_metadata and keep going. - // Open the file in read-only mode with buffer. - let f = File::open(&path).context(format!("open {}", path.display()))?; - let r = BufReader::new(f); - - // Read the JSON contents of the file as an instance of `User`. - let old_metadata: HashMap = serde_json::from_reader(r) - .context(format!("serde_json::from_reader {}", path.display()))?; + let old_metadata: HashMap = match File::open(&path) { + Ok(f) => { + let r = BufReader::new(f); + serde_json::from_reader(r)? + } + Err(e) => { + error!("Failed to open {}: {}", path.display(), e); + HashMap::new() + } + }; info!("Read metadata, {} videos found", old_metadata.len());