infinitree/tree/
commit.rs1use crate::Id;
2use serde::{Deserialize, Serialize};
3use serde_with::serde_as;
4use std::{sync::Arc, time::SystemTime};
5
6pub type CommitList<CustomData> = Vec<Arc<Commit<CustomData>>>;
8
9pub type CommitId = Id;
11
12#[serde_as]
14#[derive(Serialize, Deserialize, Debug)]
15pub struct Commit<CustomData>
16where
17 CustomData: Serialize,
18{
19 pub id: CommitId,
21
22 pub metadata: CommitMetadata<CustomData>,
24}
25
26#[serde_as]
29#[derive(Serialize, Deserialize, Debug)]
30pub struct CommitMetadata<CustomData>
31where
32 CustomData: Serialize,
33{
34 pub previous: Option<CommitId>,
36
37 pub message: Option<String>,
39
40 #[serde_as(as = "serde_with::TimestampSecondsWithFrac<f64>")]
42 pub time: SystemTime,
43
44 pub custom_data: CustomData,
46}
47
48impl<CustomData: Serialize + Default> Default for CommitMetadata<CustomData> {
49 fn default() -> Self {
50 Self {
51 time: SystemTime::now(),
52 previous: None,
53 message: None,
54 custom_data: CustomData::default(),
55 }
56 }
57}
58
59pub enum CommitFilter {
61 All,
64
65 Single(CommitId),
67
68 UpTo(CommitId),
70
71 Range(CommitId, CommitId),
75}
76
77impl Default for CommitFilter {
78 fn default() -> Self {
79 Self::All
80 }
81}
82
83pub enum Message {
89 Empty,
91 Some(String),
93}
94
95impl From<&str> for Message {
96 fn from(from: &str) -> Self {
97 Self::Some(from.to_string())
98 }
99}
100
101impl From<Option<String>> for Message {
102 fn from(from: Option<String>) -> Self {
103 match from {
104 Some(s) => Self::Some(s),
105 None => Self::Empty,
106 }
107 }
108}
109
110impl From<String> for Message {
111 fn from(from: String) -> Self {
112 Self::Some(from)
113 }
114}
115
116impl From<Message> for Option<String> {
117 fn from(from: Message) -> Option<String> {
118 match from {
119 Message::Empty => None,
120 Message::Some(s) => Some(s),
121 }
122 }
123}