git.rs, render.rs, test_render.rs: updates rendering code and exposes rendering API

This commit is contained in:
2026-04-27 18:26:37 -06:00
parent 5d0b1ab208
commit 4d33e4fe43
3 changed files with 143 additions and 154 deletions

View File

@@ -21,27 +21,27 @@
use std::{
error::Error,
fmt::{ self, Display, Formatter },
path::Path,
io::Write,
};
use tera::{ Context, Tera };
use gix::open;
use tera::Tera;
/* TODO: silt needs to export the HTTP error types */
#[non_exhaustive]
pub enum PageError {
NotFound,
}
use crate::backend::git::ToContext;
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum PageKind {
Code,
Dashboard,
Tickets,
Project,
RawCode,
Repo,
RepoSubDir,
Settings,
Tickets,
User,
Invalid(PageError),
/* TODO: silt exports a generic frontend non-success status trait */
Invalid,
}
impl Display for PageKind {
@@ -51,37 +51,56 @@ impl Display for PageKind {
let path = match self {
Code => "repo/code.html",
Dashboard => "dashboard.html",
Project => "project.html",
Repo => "repo/repo.html",
RepoSubDir => "repo/repo.html",
Settings => "user/settings.html",
Tickets => "repo/tickets.html",
Repo => "repo/code.html",
User => "user.html",
_ => "",
Invalid => todo!("generate error pages based on error type"),
};
write!(f, "./assets/templates/{}", path)
write!(f, "{}", path)
}
}
#[derive(Debug, Clone)]
pub struct Page {
kind: PageKind,
path: String,
branch: Option<String>,
commit: Option<String>,
tag: Option<String>,
pub kind: PageKind,
pub path: String,
pub branch: Option<String>,
pub commit: Option<String>,
pub tag: Option<String>,
pub user: Option<String>,
}
/*
impl Page {
pub fn render(&self) -> Result<String, Box<dyn Error>> {
let template_dir = self.kind.to_string();
let template = String::from_utf8(Path::new(&template_dir)
.to_path_buf()
.as_mut_os_string()
.as_encoded_bytes()
.to_vec()
)?;
pub fn render(
&self, mut dest: Box<dyn Write>
) -> Result<(), Box<dyn Error>> {
/* TODO: replace ./assets/ with the actual templates directory,
* i.e. /usr/local/share/mintee/ */
let tera = Tera::new("./assets/templates/**/*")?;
let tera = Tera::new(&page_dir)?;
Ok(tera.render(&template, &ctx)?)
use PageKind::*;
let ctx = match self.kind {
Code => todo!(),
Dashboard | Project | Settings | Tickets | User | Invalid => {
todo!()
},
Repo => {
open(&self.path)?
/*.rev_parse_single("@")?*/
.head_tree()?
.id()
.object()?
.to_context()?
},
RepoSubDir => todo!(),
};
Ok(dest.write_all(
tera.render(&self.kind.to_string(), &ctx)?.as_bytes()
)?)
}
}
*/