Skip to main content

diffs_rs/storage/
metadata.rs

1use std::ops::Range;
2
3use serde::Serialize;
4
5use crate::model;
6
7#[derive(Debug, Serialize)]
8pub struct HunkMetadata {
9    pub header: Box<str>,
10    pub old_start: u32,
11    pub new_start: u32,
12    pub lines: Range<usize>,
13}
14
15impl From<model::Hunk> for HunkMetadata {
16    fn from(hunk: model::Hunk) -> Self {
17        let model::Hunk {
18            header,
19            old_start,
20            new_start,
21            lines,
22        } = hunk;
23        Self {
24            header: header.into_boxed_str(),
25            old_start,
26            new_start,
27            lines,
28        }
29    }
30}
31
32#[derive(Debug, Serialize)]
33pub struct FileMetadata {
34    #[serde(skip)]
35    pub raw_path: Box<[u8]>,
36    pub path: Box<str>,
37    pub old_path: Box<str>,
38    pub status: Box<str>,
39    pub old_mode: Box<str>,
40    pub new_mode: Box<str>,
41    pub additions: usize,
42    pub deletions: usize,
43    pub digits: usize,
44    pub hunks: Box<[HunkMetadata]>,
45}
46
47impl From<model::FileDiff> for FileMetadata {
48    fn from(file: model::FileDiff) -> Self {
49        let model::FileDiff {
50            identity: _,
51            raw_path,
52            path,
53            old_path,
54            status,
55            old_mode,
56            new_mode,
57            additions,
58            deletions,
59            digits,
60            hunks,
61        } = file;
62        Self {
63            raw_path: raw_path.into_boxed_slice(),
64            path: path.into_boxed_str(),
65            old_path: old_path.into_boxed_str(),
66            status: status.into_boxed_str(),
67            old_mode: old_mode.into_boxed_str(),
68            new_mode: new_mode.into_boxed_str(),
69            additions,
70            deletions,
71            digits,
72            hunks: hunks.into_iter().map(HunkMetadata::from).collect(),
73        }
74    }
75}