https://dev.kronoz.win/r/rgit.git
1// sorry clippy, we don't have a choice. askama forces this on us
2#![allow(clippy::unnecessary_wraps, clippy::trivially_copy_pass_by_ref)]
3
4use std::borrow::Borrow;
5
6use time::format_description::well_known::Rfc3339;
7
8pub fn format_time(s: impl Borrow<time::OffsetDateTime>) -> Result<String, askama::Error> {
9 (*s.borrow())
10 .format(&Rfc3339)
11 .map_err(Box::from)
12 .map_err(askama::Error::Custom)
13}
14
15pub fn timeago(s: impl Borrow<time::OffsetDateTime>) -> Result<String, askama::Error> {
16 Ok(timeago::Formatter::new()
17 .convert((time::OffsetDateTime::now_utc() - *s.borrow()).unsigned_abs()))
18}
19
20pub fn file_perms(s: &i32) -> Result<String, askama::Error> {
21 Ok(unix_mode::to_string(s.unsigned_abs()))
22}
23
24pub fn hex(s: &[u8]) -> Result<String, askama::Error> {
25 Ok(hex::encode(s))
26}
27
28pub fn md5(s: &str) -> Result<String, askama::Error> {
29 Ok(hex::encode(md5::compute(s).0))
30}
31
32#[allow(dead_code)]
33pub fn md(md: &str) -> Result<String, askama::Error> {
34 Ok(comrak::markdown_to_html(
35 md,
36 &comrak::ComrakOptions::default(),
37 ))
38}