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::{ CommitReference };
use crate::state::{ Gitten };
use crate::templates::{
	blob_context,
	index_context,
	repo_context,
	tree_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 head_repo_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 = repo_context(&application_state, &repository, CommitReference::Head)?;
	let html = application_state.tera.render("repo.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn head_tree_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_context(&application_state, &repository, CommitReference::Head, &tree_path)?;
	let html = application_state.tera.render("tree.tera.html", &context)?;
	Ok(Html(html))
}

pub async fn head_blob_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_context(&application_state, &repository, CommitReference::Head, &blob_path)?;
	let html = application_state.tera.render("blob.tera.html", &context)?;
	Ok(Html(html))
}

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

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

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