Skip to main content

clankerdiff_core/models/
diff_scope.rs

1use crate::ParseDiffScopeError;
2use serde::{Deserialize, Serialize};
3use std::{fmt, str::FromStr};
4
5/// The snapshot included in a document.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
7pub enum DiffScope {
8    Unstaged,
9    Staged,
10    #[default]
11    Both,
12}
13
14impl DiffScope {
15    #[must_use]
16    pub const fn as_str(self) -> &'static str {
17        match self {
18            Self::Unstaged => "unstaged",
19            Self::Staged => "staged",
20            Self::Both => "both",
21        }
22    }
23
24    #[must_use]
25    pub const fn next(self) -> Self {
26        match self {
27            Self::Unstaged => Self::Staged,
28            Self::Staged => Self::Both,
29            Self::Both => Self::Unstaged,
30        }
31    }
32}
33
34impl fmt::Display for DiffScope {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        f.write_str(self.as_str())
37    }
38}
39
40impl FromStr for DiffScope {
41    type Err = ParseDiffScopeError;
42    fn from_str(value: &str) -> Result<Self, Self::Err> {
43        match value {
44            "unstaged" => Ok(Self::Unstaged),
45            "staged" => Ok(Self::Staged),
46            "both" => Ok(Self::Both),
47            other => Err(ParseDiffScopeError(other.to_owned())),
48        }
49    }
50}