summaryrefslogtreecommitdiffstats
path: root/src/commit/commit.rs
blob: 4da3fb25d4e99287b0267bcf73b2fa03d09c14d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::commit::file_stat::FileStat;
use crate::commit::user::User;
use crate::commit::utils::load_commit_state;
use chrono::{DateTime, Local};

#[derive(Debug, PartialEq)]
pub struct Commit {
	author: User,
	body: Option<String>,
	committer: User,
	date: DateTime<Local>,
	file_stats: Option<Vec<FileStat>>,
	hash: String,
}

impl Commit {
	pub fn new(
		hash: String,
		author: User,
		committer: User,
		date: DateTime<Local>,
		file_stats: Option<Vec<FileStat>>,
		body: Option<String>,
	) -> Self
	{
		Commit {
			author,
			body,
			committer,
			date,
			file_stats,
			hash,
		}
	}

	pub fn from_commit_hash(hash: &str) -> Result<Self, String> {
		load_commit_state(hash).map_err(|e| String::from(e.message()))
	}

	pub fn get_author(&self) -> &User {
		&self.author
	}

	pub fn get_committer(&self) -> &User {
		&self.committer
	}

	pub fn get_date(&self) -> &DateTime<Local> {
		&self.date
	}

	pub fn get_hash(&self) -> &String {
		&self.hash
	}

	pub fn get_body(&self) -> &Option<String> {
		&self.body
	}

	pub fn get_file_stats(&self) -> &Option<Vec<FileStat>> {
		&self.file_stats
	}

	pub fn get_file_stats_length(&self) -> usize {
		match &self.file_stats {
			Some(s) => s.len(),
			None => 0,
		}
	}
}