use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::num::NonZeroU32;
use crate::domain::{ChangeKind, CommitBaseline, ObjectId, RepoPath};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum WorktreeDiffKind {
Tracked,
Untracked,
}
impl From<ChangeKind> for WorktreeDiffKind {
fn from(value: ChangeKind) -> Self {
if value == ChangeKind::Untracked {
Self::Untracked
} else {
Self::Tracked
}
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum DiffTarget {
Worktree {
path: RepoPath,
kind: WorktreeDiffKind,
},
Commit {
commit: ObjectId,
baseline: CommitBaseline,
path: RepoPath,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LineNumber(NonZeroU32);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LineNumberError;
impl Display for LineNumberError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("line number must be one or greater")
}
}
impl Error for LineNumberError {}
impl LineNumber {
#[must_use]
pub const fn new(value: u32) -> Option<Self> {
match NonZeroU32::new(value) {
Some(value) => Some(Self(value)),
None => None,
}
}
#[must_use]
pub fn value(self) -> u32 {
self.0.get()
}
}
impl Display for LineNumber {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
impl From<NonZeroU32> for LineNumber {
fn from(value: NonZeroU32) -> Self {
Self(value)
}
}
impl From<LineNumber> for u32 {
fn from(value: LineNumber) -> Self {
value.value()
}
}
impl TryFrom<u32> for LineNumber {
type Error = LineNumberError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Self::new(value).ok_or(LineNumberError)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DiffLineKind {
Header,
Hunk,
Added,
Removed,
Context,
Meta,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DiffLine {
kind: DiffLineKind,
old_line: Option<LineNumber>,
new_line: Option<LineNumber>,
text: String,
}
impl DiffLine {
#[must_use]
pub fn new(
kind: DiffLineKind,
old_line: Option<LineNumber>,
new_line: Option<LineNumber>,
text: String,
) -> Self {
Self {
kind,
old_line,
new_line,
text,
}
}
#[must_use]
pub fn kind(&self) -> DiffLineKind {
self.kind
}
#[must_use]
pub fn old_line(&self) -> Option<LineNumber> {
self.old_line
}
#[must_use]
pub fn new_line(&self) -> Option<LineNumber> {
self.new_line
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DiffDocument {
Text {
lines: Vec<DiffLine>,
bytes: usize,
},
Binary {
summary: String,
},
Empty {
message: String,
},
Truncated {
lines: Vec<DiffLine>,
bytes: usize,
},
}
impl DiffDocument {
#[must_use]
pub fn lines(&self) -> &[DiffLine] {
match self {
Self::Text { lines, .. } | Self::Truncated { lines, .. } => lines,
Self::Binary { .. } | Self::Empty { .. } => &[],
}
}
#[must_use]
pub fn message(&self) -> Option<&str> {
match self {
Self::Binary { summary } => Some(summary),
Self::Empty { message } => Some(message),
Self::Text { .. } | Self::Truncated { .. } => None,
}
}
#[must_use]
pub fn approximate_bytes(&self) -> usize {
match self {
Self::Text { bytes, .. } | Self::Truncated { bytes, .. } => *bytes,
Self::Binary { summary } => summary.len(),
Self::Empty { message } => message.len(),
}
}
#[must_use]
pub fn is_truncated(&self) -> bool {
matches!(self, Self::Truncated { .. })
}
}