wayver's git archive


a simple self-hosted git server
git clone https://git.wayver.dev/bile

src/utils/filters.rs@71b17dd92232913d023854951d629e9876076719

raw
Date Commit Message Author Files + -
2026-02-19 17:51 large refactoring wayverd 53 2153 1683
...

1#![allow(
2    unreachable_pub,
3    clippy::inline_always,
4    clippy::unused_self,
5    clippy::unnecessary_wraps,
6    reason = "generated by askama filter_fn"
7)]
8
9use git2::{Commit, Signature, Time};
10use jiff::{
11    Timestamp,
12    tz::{self, TimeZone},
13};
14use num_conv::Truncate as _;
15
16use crate::git::Repository;
17
18#[askama::filter_fn]
19#[tracing::instrument(skip_all)]
20pub(crate) fn format_datetime<'f>(
21    time: Time,
22    _: &dyn askama::Values,
23    format: &'f str,
24) -> askama::Result<jiff::fmt::strtime::Display<'f>> {
25    let tz = TimeZone::fixed(tz::offset((time.offset_minutes() / 60).truncate::<i8>()));
26
27    let timestamp = Timestamp::from_second(time.seconds()).map_err(|err| {
28        tracing::error!(err=%err, "failed to convert seconds into a timestamp");
29
30        askama::Error::Fmt
31    })?;
32
33    let zoned = timestamp.to_zoned(tz);
34
35    let format = zoned.strftime(format);
36
37    Ok(format)
38}
39
40#[askama::filter_fn]
41pub(crate) fn unix_perms(m: i32, _: &dyn askama::Values) -> askama::Result<String> {
42    // https://unix.stackexchange.com/questions/450480/file-permission-with-six-bytes-in-git-what-does-it-mean
43    // Git doesn't store arbitrary modes, only a subset of the values are
44    // allowed. Since the number of possible values is quite small, it is
45    // easiest to exhaustively match them.
46    Ok(match m {
47        0o040_000 => "drwxr-xr-x", // directory
48        0o100_755 => "-rwxr-xr-x", // regular file, executable
49        0o100_644 => "-rw-r--r--", // regular file, default umask
50        0o120_000 => "lrwxrwxrwx", // symlink
51        0o160_000 => "m---------", // submodule
52        _ => unreachable!("unknown file mode"),
53    }
54    .into())
55}
56
57#[askama::filter_fn]
58pub(crate) fn repo_name<'r>(
59    repo: &'r Repository,
60    _: &dyn askama::Values,
61) -> askama::Result<&'r str> {
62    repo.name().ok_or(askama::Error::Fmt)
63}
64
65#[askama::filter_fn]
66pub(crate) fn description(repo: &Repository, _: &dyn askama::Values) -> askama::Result<String> {
67    Ok(repo.description())
68}
69
70#[askama::filter_fn]
71#[tracing::instrument(skip_all)]
72pub(crate) fn last_modified(repo: &Repository, _: &dyn askama::Values) -> askama::Result<Time> {
73    repo.last_modified().map_err(|err| {
74        tracing::error!(err=?err, "failed to get repo's last modified date");
75        askama::Error::Fmt
76    })
77}
78
79#[askama::filter_fn]
80pub(crate) fn repo_owner(repo: &Repository, _: &dyn askama::Values) -> askama::Result<String> {
81    Ok(repo.owner())
82}
83
84#[askama::filter_fn]
85pub(crate) fn signature_email_link(
86    signature: &Signature<'_>,
87    _: &dyn askama::Values,
88) -> askama::Result<String> {
89    let Some(email) = signature.email() else {
90        return Ok(signature.to_string());
91    };
92
93    Ok(format!(
94        "<a href=\"mailto:{}\">{}</a>",
95        email,
96        signature.name().unwrap_or("&#65533;")
97    ))
98}
99
100#[askama::filter_fn]
101#[tracing::instrument(skip_all)]
102pub(crate) fn short_id(commit: &Commit<'_>, _: &dyn askama::Values) -> askama::Result<String> {
103    let id = match commit.as_object().short_id() {
104        Ok(id) => id,
105        Err(err) => {
106            tracing::error!(err=?err, "failed to get short id of commit");
107
108            return Err(askama::Error::Fmt);
109        }
110    };
111
112    id.as_str().map(str::to_string).ok_or(askama::Error::Fmt)
113}
114