git/testutil/
build_commit.rs

1use chrono::{Local, TimeZone};
2
3use crate::{testutil::JAN_2021_EPOCH, Commit, Reference, User};
4
5/// Builder for creating a new commit.
6#[derive(Debug)]
7pub struct CommitBuilder {
8	commit: Commit,
9}
10
11impl CommitBuilder {
12	/// Create a new instance of the builder with the provided hash. The new instance will default
13	/// to a committed date of Jan 1, 2021 UTC. All other fields are `None`.
14	#[inline]
15	#[must_use]
16	#[allow(clippy::missing_panics_doc)]
17	pub fn new(hash: &str) -> Self {
18		Self {
19			commit: Commit {
20				hash: String::from(hash),
21				reference: None,
22				author: User::new(None, None),
23				authored_date: None,
24				committed_date: Local.timestamp_opt(JAN_2021_EPOCH, 0).unwrap(),
25				committer: None,
26				message: None,
27				summary: None,
28			},
29		}
30	}
31
32	/// Set the hash.
33	#[inline]
34	#[must_use]
35	pub fn hash(mut self, hash: &str) -> Self {
36		self.commit.hash = String::from(hash);
37		self
38	}
39
40	/// Set the reference, use `create::testutil::ReferenceBuilder` to build a `Reference`.
41	#[inline]
42	#[must_use]
43	#[allow(clippy::missing_const_for_fn)]
44	pub fn reference(mut self, reference: Reference) -> Self {
45		self.commit.reference = Some(reference);
46		self
47	}
48
49	/// Set the author name and related email address.
50	#[inline]
51	#[must_use]
52	#[allow(clippy::missing_const_for_fn)]
53	pub fn author(mut self, author: User) -> Self {
54		self.commit.author = author;
55		self
56	}
57
58	/// Set the authored commit time from number of seconds since unix epoch.
59	#[inline]
60	#[must_use]
61	pub fn authored_time(mut self, time: i64) -> Self {
62		self.commit.authored_date = Some(Local.timestamp_opt(time, 0).unwrap());
63		self
64	}
65
66	/// Set the committer name and related email address.
67	#[inline]
68	#[must_use]
69	#[allow(clippy::missing_const_for_fn)]
70	pub fn committer(mut self, committer: User) -> Self {
71		self.commit.committer = Some(committer);
72		self
73	}
74
75	/// Set the committed commit time from number of seconds since unix epoch.
76	#[inline]
77	#[must_use]
78	pub fn commit_time(mut self, time: i64) -> Self {
79		self.commit.committed_date = Local.timestamp_opt(time, 0).unwrap();
80		self
81	}
82
83	/// Set the commit summary.
84	#[inline]
85	#[must_use]
86	pub fn summary(mut self, summary: &str) -> Self {
87		self.commit.summary = Some(String::from(summary));
88		self
89	}
90
91	/// Set the commit message.
92	#[inline]
93	#[must_use]
94	pub fn message(mut self, message: &str) -> Self {
95		self.commit.message = Some(String::from(message));
96		self
97	}
98
99	/// Build the `Commit`.
100	#[inline]
101	#[must_use]
102	#[allow(clippy::missing_const_for_fn)]
103	pub fn build(self) -> Commit {
104		self.commit
105	}
106}