dysk_cli/
json.rs

1use {
2    crate::units::Units,
3    lfs_core::*,
4    serde_json::{json, Value},
5};
6
7pub fn output_value(mounts: &[&Mount], units: Units) -> Value {
8    Value::Array(
9        mounts
10            .iter()
11            .map(|mount| {
12                let stats = mount.stats().map(|s| {
13                    let inodes = s.inodes.as_ref().map(|inodes| {
14                        json!({
15                            "files": inodes.files,
16                            "free": inodes.ffree,
17                            "avail": inodes.favail,
18                            "used-percent": format!("{:.0}%", 100.0*inodes.use_share()),
19                        })
20                    });
21                    json!({
22                        "bsize": s.bsize,
23                        "blocks": s.blocks,
24                        "bused": s.bused,
25                        "bfree": s.bfree,
26                        "bavail": s.bavail,
27                        "size": units.fmt(s.size()),
28                        "used": units.fmt(s.used()),
29                        "used-percent": format!("{:.0}%", 100.0*s.use_share()),
30                        "available": units.fmt(s.available()),
31                        "inodes": inodes,
32                    })
33                });
34                let disk = mount.disk.as_ref().map(|d| {
35                    json!({
36                        "type": d.disk_type(),
37                        "rotational": d.rotational,
38                        "removable": d.removable,
39                        "crypted": d.crypted,
40                        "ram": d.ram,
41                    })
42                });
43                json!({
44                    "id": mount.info.id,
45                    "dev": {
46                        "major": mount.info.dev.major,
47                        "minor": mount.info.dev.minor,
48                    },
49                    "fs": mount.info.fs,
50                    "fs-label": mount.fs_label,
51                    "fs-type": mount.info.fs_type,
52                    "mount-point": mount.info.mount_point,
53                    "disk": disk,
54                    "stats": stats,
55                    "bound": mount.info.bound,
56                    "remote": mount.info.is_remote(),
57                    "unreachable": mount.is_unreachable(),
58                })
59            })
60            .collect(),
61    )
62}