Echo Writes Code

breadcrumbs.rs

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::errors::{ Error, Result };
use crate::git::{ CommitReference };

use serde::{ Serialize };

use std::path::{ Component, Path };

#[derive(Clone, Serialize)]
pub(crate) struct Breadcrumb {
	link: String,
	name: String,
}

pub(crate) fn for_index() -> Vec<Breadcrumb> {
	vec![
		Breadcrumb {
			link: "/".to_string(),
			name: "Repositories".to_string(),
		},
	]
}

pub(crate) fn for_repository(repository_name: &str, commit: &CommitReference) -> Vec<Breadcrumb> {
	let mut breadcrumbs = for_index();

	breadcrumbs.push(Breadcrumb {
		link: format!("/repository/{}/head", repository_name),
		name: repository_name.to_string(),
	});

	breadcrumbs.push(Breadcrumb {
		link: format!("/repository/{}/{}", repository_name, commit.slug()),
		name: commit.to_string(),
	});

	breadcrumbs
}

pub(crate) fn for_tree(repository_name: &str, commit: &CommitReference, tree_path: &str) -> Result<Vec<Breadcrumb>> {
	let mut breadcrumbs = for_repository(repository_name, commit);

	// There should always be more than zero entries at this point
	let previous = breadcrumbs.last().unwrap();
	let mut link = format!("{}/tree", previous.link);

	for component in Path::new(tree_path).components() {
		let Component::Normal(component) = component else {
			return Err(Error::InvalidTreePath(tree_path.to_string()));
		};

		let Some(component) = component.to_str() else {
			return Err(Error::InvalidTreePath(tree_path.to_string()));
		};

		link = format!("{}/{}", link, component);
		breadcrumbs.push(Breadcrumb {
			link: link.clone(),
			name: component.to_string(),
		});
	}

	Ok(breadcrumbs)
}

pub(crate) fn for_blob(repository_name: &str, commit: &CommitReference, blob_path: &str) -> Result<Vec<Breadcrumb>> {
	let mut breadcrumbs = for_repository(repository_name, commit);

	// There should always be more than zero entries at this point
	let previous = breadcrumbs.last().unwrap().clone();
	let mut link = format!("{}/tree", previous.link);

	if let Some(parent) = Path::new(blob_path).parent() {
		for component in parent.components() {
			let Component::Normal(component) = component else {
				return Err(Error::InvalidBlobPath(blob_path.to_string()));
			};

			let Some(component) = component.to_str() else {
				return Err(Error::InvalidBlobPath(blob_path.to_string()));
			};

			link = format!("{}/{}", link, component);
			breadcrumbs.push(Breadcrumb {
				link: link.clone(),
				name: component.to_string(),
			});
		}
	}

	let Some(blob_name) = Path::new(blob_path).file_name() else {
		return Err(Error::InvalidBlobPath(blob_path.to_string()))
	};

	let Some(blob_name) = blob_name.to_str() else {
		return Err(Error::InvalidBlobPath(blob_path.to_string()))
	};

	breadcrumbs.push(Breadcrumb {
		link: format!("{}/blob/{}", previous.link, blob_path),
		name: blob_name.to_string(),
	});

	Ok(breadcrumbs)
}

pub(crate) fn for_history(repository_name: &str, commit: &CommitReference) -> Vec<Breadcrumb> {
	let mut breadcrumbs = for_repository(repository_name, commit);

	// There should always be more than zero entries at this point
	let previous = breadcrumbs.last().unwrap();

	breadcrumbs.push(Breadcrumb {
		link: format!("{}/history", previous.link),
		name: "History".to_string(),
	});

	breadcrumbs
}

pub(crate) fn for_branches(repository_name: &str) -> Vec<Breadcrumb> {
	let mut breadcrumbs = for_repository(repository_name, &CommitReference::Head);

	breadcrumbs.push(Breadcrumb {
		link: format!("/repository/{}/branches", repository_name),
		name: "Branches".to_string(),
	});

	breadcrumbs
}

pub(crate) fn for_tags(repository_name: &str) -> Vec<Breadcrumb> {
	let mut breadcrumbs = for_repository(repository_name, &CommitReference::Head);

	breadcrumbs.push(Breadcrumb {
		link: format!("/repository/{}/tags", repository_name),
		name: "Tags".to_string(),
	});

	breadcrumbs
}