Echo Writes Code

handlers.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
use crate::errors::{ Result };
use crate::git::{ Oid };
use crate::state::{ Gitten };
use crate::templates::{
	blob_id_context,
	blob_path_context,
	index_context,
	repository_context,
	tree_id_context,
	tree_path_context,
};

use axum::extract::{ Path, State };
use axum::response::{ Html, };

use std::sync::{ Arc };

pub async fn index_handler(State(application_state): State<Arc<Gitten>>) -> Result<Html<String>> {
	let context = index_context(&application_state)?;
	let html = application_state.tera.render("index.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn repository_handler(
	State(application_state): State<Arc<Gitten>>,
	Path(repository_name): Path<String>,
) -> Result<Html<String>> {
	let repository = application_state.git.find_repository_by_name(&repository_name)?;
	let context = repository_context(&application_state, &repository)?;

	let html = if repository.is_empty()? {
		application_state.tera.render("empty_repository.tera.html", &context)?
	} else {
		application_state.tera.render("repository.tera.html", &context)?
	};

	Ok(Html(html))
}

pub async fn tree_path_handler(
	State(application_state): State<Arc<Gitten>>,
	Path((repository_name, tree_path)): Path<(String, String)>
) -> Result<Html<String>> {
	let repository = application_state.git.find_repository_by_name(&repository_name)?;
	let context = tree_path_context(&application_state, &repository, &tree_path)?;
	let html = application_state.tera.render("tree_path.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn tree_id_handler(
	State(application_state): State<Arc<Gitten>>,
	Path((repository_name, tree_id)): Path<(String, String)>,
) -> Result<Html<String>> {
	let repository = application_state.git.find_repository_by_name(&repository_name)?;
	let tree_id = Oid::from_str(&tree_id)?;
	let context = tree_id_context(&application_state, &repository, tree_id)?;
	let html = application_state.tera.render("tree_id.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn blob_path_handler(
	State(application_state): State<Arc<Gitten>>,
	Path((repository_name, blob_path)): Path<(String, String)>
) -> Result<Html<String>> {
	let repository = application_state.git.find_repository_by_name(&repository_name)?;
	let context = blob_path_context(&application_state, &repository, &blob_path)?;
	let html = application_state.tera.render("blob_path.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn blob_id_handler(
	State(application_state): State<Arc<Gitten>>,
	Path((repository_name, blob_id)): Path<(String, String)>,
) -> Result<Html<String>> {
	let repository = application_state.git.find_repository_by_name(&repository_name)?;
	let blob_id = Oid::from_str(&blob_id)?;
	let context = blob_id_context(&application_state, &repository, blob_id)?;
	let html = application_state.tera.render("blob_id.tera.html", &context)?;
	Ok(Html(html))
}