use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ChangeScope(u16);
impl ChangeScope {
pub const NONE: Self = Self(0);
pub const INDEX: Self = Self(1);
pub const WORKTREE: Self = Self(1 << 1);
pub const REFS: Self = Self(1 << 2);
pub const LOG: Self = Self(1 << 3);
pub const STASHES: Self = Self(1 << 4);
pub const WORKTREES: Self = Self(1 << 5);
pub const CONFIG: Self = Self(1 << 6);
pub const LFS: Self = Self(1 << 7);
pub const CONFLICTS: Self = Self(1 << 8);
pub const ALL: Self = Self((1 << 9) - 1);
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
pub const fn intersects(self, other: Self) -> bool {
self.0 & other.0 != 0
}
pub const fn touches_lfs(self) -> bool {
self.intersects(Self::LFS.union(Self::CONFIG))
|| self.contains(Self::REFS.union(Self::WORKTREE))
}
}
impl Default for ChangeScope {
fn default() -> Self {
Self::ALL
}
}
impl std::ops::BitOr for ChangeScope {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
impl std::ops::BitOrAssign for ChangeScope {
fn bitor_assign(&mut self, rhs: Self) {
*self = self.union(rhs);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lfs_relevance() {
let stage = ChangeScope::INDEX | ChangeScope::WORKTREE | ChangeScope::CONFLICTS;
assert!(!stage.touches_lfs());
assert!(ChangeScope::CONFIG.touches_lfs());
assert!(ChangeScope::ALL.touches_lfs());
}
}