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
141
142
143
144
145
146
147
148
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 index_breadcrumbs() -> Vec<Breadcrumb> {
	vec![
		Breadcrumb {
			link: "/".to_string(),
			name: "Repositories".to_string(),
		},
	]
}

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

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

	match commit {
		CommitReference::Branch(ref branch_name) => breadcrumbs.push(Breadcrumb {
			link: format!("/repository/{}/branch/{}", repository_name, branch_name),
			name: format!("Branch {}", branch_name),
		}),
		CommitReference::Tag(ref tag_name) => breadcrumbs.push(Breadcrumb {
			link: format!("/repository/{}/tag/{}", repository_name, tag_name),
			name: format!("Tag {}", tag_name),
		}),
		CommitReference::Direct(commit_id) => breadcrumbs.push(Breadcrumb {
			link: format!("/repository/{}/commit/{}", repository_name, commit_id),
			name: format!("Commit {}", commit_id),
		}),
		_ => (),
	};

	breadcrumbs
}

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

	// There should always be at least 2 entries at this point (`/` and the repository)
	let top_level = breadcrumbs.last().unwrap();
	let mut link_so_far = format!("{}/tree", top_level.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_so_far = format!("{}/{}", link_so_far, component);
		breadcrumbs.push(Breadcrumb {
			link: link_so_far.clone(),
			name: component.to_string(),
		});
	}

	Ok(breadcrumbs)
}

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

	// There should always be at least 2 entries at this point (`/` and the repository)
	let top_level = breadcrumbs.last().unwrap().clone();
	let mut link_so_far = format!("{}/tree", top_level.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_so_far = format!("{}/{}", link_so_far, component);
			breadcrumbs.push(Breadcrumb {
				link: link_so_far.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/{}", top_level.link, blob_path),
		name: blob_name.to_string(),
	});

	Ok(breadcrumbs)
}

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

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

	breadcrumbs
}

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

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

	breadcrumbs
}

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

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

	breadcrumbs
}