Repository: wickedchicken/checklist (language: rust)

Issue:
Version config file
In case we need to upgrade/downgrade later.


Discussion / hints:

Fix patch:
diff --git a/.checklist.yml b/.checklist.yml
index b29f239..6e4d755 100644
--- a/.checklist.yml
+++ b/.checklist.yml
@@ -1,4 +1,5 @@
 ---
+schema_version: 1
 committing:
   - run cargo fmt
   - run cargo build
diff --git a/Cargo.toml b/Cargo.toml
index ce2c793..2108102 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "checklist"
-version = "0.1.1"
+version = "0.2.0"
 authors = ["Mike Stipicevic <stipredirect@gmail.com>"]
 edition = "2018"
 
diff --git a/src/lib.rs b/src/lib.rs
index a3e88f8..15522ac 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,6 +19,15 @@ use dialoguer::theme::ColorfulTheme;
 use dialoguer::Confirmation;
 use failure::Error;
 
+// Increment the version number every time the version changes. I can't figure out how to
+// break this out into its own const, see https://github.com/rust-lang/rust/issues/52393.
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(tag = "schema_version")]
+enum VersionedCheckListList {
+    #[serde(rename = "1")] // increment here
+    Current(CheckListList),
+}
+
 #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
 struct CheckListList(BTreeMap<String, CheckList>);
 
@@ -38,9 +47,11 @@ impl CheckListList {
     }
 
     fn from_reader<R: Read>(input: R) -> Result<CheckListList, Error> {
-        match serde_yaml::from_reader::<_, CheckListList>(input) {
+        match serde_yaml::from_reader::<_, VersionedCheckListList>(input) {
             Err(e) => bail!("couldn't parse yaml: {}", e),
-            Ok(s) => Ok(s),
+            Ok(s) => match s {
+                VersionedCheckListList::Current(s) => Ok(s),
+            },
         }
     }
 }
@@ -118,7 +129,7 @@ mod tests {
             t.close().unwrap();
         });
         temp.child(".checklist.yml")
-            .write_str("committing:\n- test")
+            .write_str("schema_version: 1\ncommitting:\n- test")
             .unwrap();
         assert_eq!(
             CheckListList::from_file(temp.child(".checklist.yml").path()).unwrap(),
@@ -138,6 +149,18 @@ mod tests {
         assert!(CheckListList::from_file(temp.child(".checklist.yml").path()).is_err())
     }
 
+    #[test]
+    fn test_incorrect_schema_version() {
+        let t = assert_fs::TempDir::new().unwrap();
+        let temp = scopeguard::guard(t, |t| {
+            t.close().unwrap();
+        });
+        temp.child(".checklist.yml")
+            .write_str("schema_version: bananas\ncommitting:\n- test")
+            .unwrap();
+        assert!(CheckListList::from_file(temp.child(".checklist.yml").path()).is_err())
+    }
+
     #[test]
     fn test_invalid_filename() {
         let t = assert_fs::TempDir::new().unwrap();


Test patch:
diff --git a/tests/rexpect_integration_tests.rs b/tests/rexpect_integration_tests.rs
index 2484828..8ad4998 100644
--- a/tests/rexpect_integration_tests.rs
+++ b/tests/rexpect_integration_tests.rs
@@ -28,7 +28,9 @@ fn get_command_path() -> String {
 fn with_checklist(tempdir: &TempDir, contents: &str) -> String {
     let checklist_file = tempdir.child(".checklist.yml");
 
-    checklist_file.write_str(contents).unwrap();
+    checklist_file
+        .write_str(&format!("schema_version: 1\n{}", contents))
+        .unwrap();
 
     checklist_file.path().to_str().unwrap().to_string()
 }