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                        "bfree": s.bfree,
25                        "bavail": s.bavail,
26                        "size": units.fmt(s.size()),
27                        "used": units.fmt(s.used()),
28                        "used-percent": format!("{:.0}%", 100.0*s.use_share()),
29                        "available": units.fmt(s.available()),
30                        "inodes": inodes,
31                    })
32                });
33                let disk = mount.disk.as_ref().map(|d| {
34                    json!({
35                        "type": d.disk_type(),
36                        "rotational": d.rotational,
37                        "removable": d.removable,
38                        "crypted": d.crypted,
39                        "ram": d.ram,
40                    })
41                });
42                json!({
43                    "id": mount.info.id,
44                    "dev": {
45                        "major": mount.info.dev.major,
46                        "minor": mount.info.dev.minor,
47                    },
48                    "fs": mount.info.fs,
49                    "fs-label": mount.fs_label,
50                    "fs-type": mount.info.fs_type,
51                    "mount-point": mount.info.mount_point,
52                    "disk": disk,
53                    "stats": stats,
54                    "bound": mount.info.bound,
55                    "remote": mount.info.is_remote(),
56                    "unreachable": mount.is_unreachable(),
57                })
58            })
59            .collect(),
60    )
61}