]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tools/src/format.rs
Set MMAP_THRESHOLD to a fixed value (128K)
[proxmox-backup.git] / pbs-tools / src / format.rs
1 use std::borrow::Borrow;
2
3 use anyhow::{Error};
4 use serde_json::Value;
5
6 use pbs_api_types::HumanByte;
7
8 pub fn strip_server_file_extension(name: &str) -> &str {
9 if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
10 &name[..name.len()-5]
11 } else {
12 name // should not happen
13 }
14 }
15
16 pub fn render_backup_file_list<S: Borrow<str>>(files: &[S]) -> String {
17 let mut files: Vec<&str> = files.iter()
18 .map(|v| strip_server_file_extension(v.borrow()))
19 .collect();
20
21 files.sort();
22
23 files.join(" ")
24 }
25
26 pub fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
27 if value.is_null() { return Ok(String::new()); }
28 let text = match value.as_i64() {
29 Some(epoch) => {
30 if let Ok(epoch_string) = proxmox_time::strftime_local("%c", epoch as i64) {
31 epoch_string
32 } else {
33 epoch.to_string()
34 }
35 },
36 None => {
37 value.to_string()
38 }
39 };
40 Ok(text)
41 }
42
43 pub fn render_task_status(value: &Value, record: &Value) -> Result<String, Error> {
44 if record["endtime"].is_null() {
45 Ok(value.as_str().unwrap_or("running").to_string())
46 } else {
47 Ok(value.as_str().unwrap_or("unknown").to_string())
48 }
49 }
50
51 pub fn render_bool_with_default_true(value: &Value, _record: &Value) -> Result<String, Error> {
52 let value = value.as_bool().unwrap_or(true);
53 Ok((if value { "1" } else { "0" }).to_string())
54 }
55
56 pub fn render_bytes_human_readable(value: &Value, _record: &Value) -> Result<String, Error> {
57 if value.is_null() { return Ok(String::new()); }
58 let text = match value.as_u64() {
59 Some(bytes) => {
60 HumanByte::from(bytes).to_string()
61 }
62 None => {
63 value.to_string()
64 }
65 };
66 Ok(text)
67 }