Mountain/IPC/WindServiceHandlers/Utilities/MetadataEncoding.rs
1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Converts `std::fs::Metadata` to VS Code's `IStat` wire shape. The
4//! `FileType` bits are VS Code's enum (File=1, Directory=2,
5//! SymbolicLink=64); the readdir atoms also emit these values.
6
7use serde_json::{Value, json};
8
9pub fn metadata_to_istat(Metadata:&std::fs::Metadata) -> Value {
10 let FileType = if Metadata.is_symlink() {
11 64
12 } else if Metadata.is_dir() {
13 2
14 } else {
15 1
16 };
17
18 let Size = Metadata.len();
19
20 let Mtime = Metadata
21 .modified()
22 .ok()
23 .and_then(|T| T.duration_since(std::time::UNIX_EPOCH).ok())
24 .map(|D| D.as_millis() as u64)
25 .unwrap_or(0);
26
27 let Ctime = Metadata
28 .created()
29 .ok()
30 .and_then(|T| T.duration_since(std::time::UNIX_EPOCH).ok())
31 .map(|D| D.as_millis() as u64)
32 .unwrap_or(Mtime);
33
34 json!({
35 "type": FileType,
36 "size": Size,
37 "mtime": Mtime,
38 "ctime": Ctime
39 })
40}