forked from rojo-rbx/rojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_serializer.rs
38 lines (30 loc) · 993 Bytes
/
path_serializer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! Path serializer is used to serialize absolute paths in a cross-platform way,
//! by replacing all directory separators with /.
use std::path::Path;
use serde::{ser::SerializeSeq, Serialize, Serializer};
pub fn serialize_absolute<S, T>(path: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
let as_str = path
.as_ref()
.as_os_str()
.to_str()
.expect("Invalid Unicode in file path, cannot serialize");
let replaced = as_str.replace('\\', "/");
serializer.serialize_str(&replaced)
}
#[derive(Serialize)]
struct WithAbsolute<'a>(#[serde(serialize_with = "serialize_absolute")] &'a Path);
pub fn serialize_vec_absolute<S, T>(paths: &[T], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
let mut seq = serializer.serialize_seq(Some(paths.len()))?;
for path in paths {
seq.serialize_element(&WithAbsolute(path.as_ref()))?;
}
seq.end()
}