Skip to main content

sokf_core/report/
fix_op.rs

1//! `FixOp` records one change that `--fix` made. It is plain data that
2//! passes through the cache.
3
4use serde::{Deserialize, Serialize};
5
6use crate::RepoPath;
7
8/// `FixOp` is one operation that `validate --fix` performed (CLI-4_AC-26).
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
10#[serde(tag = "op", rename_all = "lowercase")]
11pub enum FixOp {
12    /// `Moved` records a document that `--fix` moved to its derived directory.
13    Moved {
14        /// `from` is the path of the document before the move.
15        from: RepoPath,
16        /// `to` is the path of the document after the move.
17        to: RepoPath,
18    },
19    /// `Created` records an index that `--fix` wrote where none existed
20    /// (`SOKF_VALIDATION_33`).
21    Created {
22        /// `path` is the path of the index that `--fix` wrote.
23        path: RepoPath,
24    },
25    /// `Rewrote` records a file whose bytes `--fix` changed.
26    Rewrote {
27        /// `path` is the path of the file that `--fix` rewrote.
28        path: RepoPath,
29    },
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    fn p(s: &str) -> RepoPath {
37        RepoPath::parse(s).unwrap()
38    }
39
40    #[test]
41    fn fix_ops_serialize_with_an_op_tag() {
42        let moved = FixOp::Moved {
43            from: p("/a.md"),
44            to: p("/b.md"),
45        };
46        assert_eq!(
47            serde_json::to_string(&moved).unwrap(),
48            r#"{"op":"moved","from":"/a.md","to":"/b.md"}"#
49        );
50        let rewrote = FixOp::Rewrote { path: p("/a.md") };
51        assert_eq!(
52            serde_json::to_string(&rewrote).unwrap(),
53            r#"{"op":"rewrote","path":"/a.md"}"#
54        );
55        let created = FixOp::Created {
56            path: p("/k/index.md"),
57        };
58        assert_eq!(
59            serde_json::to_string(&created).unwrap(),
60            r#"{"op":"created","path":"/k/index.md"}"#
61        );
62    }
63}