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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Copyright (c) 2018 Garrett Berg, vitiral@gmail.com
 *
 * Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
 * http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
 * http://opensource.org/licenses/MIT>, at your option. This file may not be
 * copied, modified, or distributed except according to those terms.
 */
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::string::ToString;
use std_prelude::*;
use stfu8;

use super::{PathMut, PathOps};

use std::ffi::{OsStr, OsString};
#[cfg(unix)]
use std::os::unix::ffi::{OsStrExt, OsStringExt};
#[cfg(windows)]
use std::os::windows::ffi::{OsStrExt, OsStringExt};

use super::{PathAbs, PathDir, PathFile};

#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct PathSer(Arc<PathBuf>);

pub trait ToStfu8 {
    fn to_stfu8(&self) -> String;
}

pub trait FromStfu8: Sized {
    fn from_stfu8(s: &str) -> Result<Self, stfu8::DecodeError>;
}

impl PathSer {
    pub fn new<P: Into<Arc<PathBuf>>>(path: P) -> Self {
        PathSer(path.into())
    }

    pub fn as_path(&self) -> &Path {
        self.as_ref()
    }
}

impl fmt::Debug for PathSer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl PathMut for PathSer {
    fn append<P: AsRef<Path>>(&mut self, path: P) -> crate::Result<()> {
        self.0.append(path)
    }
    fn pop_up(&mut self) -> crate::Result<()> {
        self.0.pop_up()
    }
    fn truncate_to_root(&mut self) {
        self.0.truncate_to_root()
    }
    fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
        self.0.set_file_name(file_name)
    }
    fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
        self.0.set_extension(extension)
    }
}

impl PathOps for PathSer {
    type Output = PathSer;

    fn concat<P: AsRef<Path>>(&self, path: P) -> crate::Result<Self::Output> {
        Ok(PathSer(self.0.concat(path)?))
    }

    fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output {
        let buf = Path::join(self.as_path(), path);
        Self::Output::new(buf)
    }

    fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> Self::Output {
        PathSer(self.0.with_file_name(file_name))
    }

    fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Self::Output {
        PathSer(self.0.with_extension(extension))
    }
}

impl AsRef<OsStr> for PathSer {
    fn as_ref(&self) -> &std::ffi::OsStr {
        self.0.as_ref().as_ref()
    }
}

impl AsRef<Path> for PathSer {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}

impl AsRef<PathBuf> for PathSer {
    fn as_ref(&self) -> &PathBuf {
        self.0.as_ref()
    }
}

impl Borrow<Path> for PathSer {
    fn borrow(&self) -> &Path {
        self.as_ref()
    }
}

impl Borrow<PathBuf> for PathSer {
    fn borrow(&self) -> &PathBuf {
        self.as_ref()
    }
}

impl<'a> Borrow<Path> for &'a PathSer {
    fn borrow(&self) -> &Path {
        self.as_ref()
    }
}

impl<'a> Borrow<PathBuf> for &'a PathSer {
    fn borrow(&self) -> &PathBuf {
        self.as_ref()
    }
}

impl<P: Into<PathBuf>> From<P> for PathSer {
    fn from(path: P) -> PathSer {
        PathSer::new(path.into())
    }
}

impl From<PathSer> for Arc<PathBuf> {
    fn from(path: PathSer) -> Arc<PathBuf> {
        path.0
    }
}

// impl From<PathAbs> for PathSer {
//     fn from(path: PathAbs) -> PathSer {
//         PathSer(path.0)
//     }
// }

impl<T> ToStfu8 for T
where
    T: Borrow<PathBuf>,
{
    #[cfg(unix)]
    fn to_stfu8(&self) -> String {
        let bytes = self.borrow().as_os_str().as_bytes();
        stfu8::encode_u8(bytes)
    }

    #[cfg(windows)]
    fn to_stfu8(&self) -> String {
        let wide: Vec<u16> = self.borrow().as_os_str().encode_wide().collect();
        stfu8::encode_u16(&wide)
    }
}

impl<T> FromStfu8 for T
where
    T: From<PathBuf>,
{
    #[cfg(unix)]
    fn from_stfu8(s: &str) -> Result<T, stfu8::DecodeError> {
        let raw_path = stfu8::decode_u8(s)?;
        let os_str = OsString::from_vec(raw_path);
        let pathbuf: PathBuf = os_str.into();
        Ok(pathbuf.into())
    }

    #[cfg(windows)]
    fn from_stfu8(s: &str) -> Result<T, stfu8::DecodeError> {
        let raw_path = stfu8::decode_u16(&s)?;
        let os_str = OsString::from_wide(&raw_path);
        let pathbuf: PathBuf = os_str.into();
        Ok(pathbuf.into())
    }
}

macro_rules! stfu8_serialize {
    ($name:ident) => {
        impl Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                serializer.serialize_str(&self.to_stfu8())
            }
        }
    };
}

stfu8_serialize!(PathSer);
stfu8_serialize!(PathAbs);
stfu8_serialize!(PathFile);
stfu8_serialize!(PathDir);

impl<'de> Deserialize<'de> for PathSer {
    fn deserialize<D>(deserializer: D) -> Result<PathSer, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let path =
            PathBuf::from_stfu8(&s).map_err(|err| serde::de::Error::custom(&err.to_string()))?;
        Ok(PathSer(Arc::new(path)))
    }
}

impl<'de> Deserialize<'de> for PathAbs {
    fn deserialize<D>(deserializer: D) -> Result<PathAbs, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let path =
            PathBuf::from_stfu8(&s).map_err(|err| serde::de::Error::custom(&err.to_string()))?;
        Ok(PathAbs(Arc::new(path)))
    }
}

impl<'de> Deserialize<'de> for PathFile {
    fn deserialize<D>(deserializer: D) -> Result<PathFile, D::Error>
    where
        D: Deserializer<'de>,
    {
        let abs = PathAbs::deserialize(deserializer)?;
        PathFile::try_from(abs).map_err(|err| serde::de::Error::custom(&err.to_string()))
    }
}

impl<'de> Deserialize<'de> for PathDir {
    fn deserialize<D>(deserializer: D) -> Result<PathDir, D::Error>
    where
        D: Deserializer<'de>,
    {
        let abs = PathAbs::deserialize(deserializer)?;
        PathDir::try_from(abs).map_err(|err| serde::de::Error::custom(&err.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::super::{PathDir, PathFile, PathInfo, PathMut, PathOps, PathType};
    use super::*;

    #[cfg(unix)]
    static SERIALIZED: &str = "[\
                               {\"type\":\"file\",\"path\":\"{0}/foo.txt\"},\
                               {\"type\":\"dir\",\"path\":\"{0}/bar\"},\
                               {\"type\":\"dir\",\"path\":\"{0}/foo/bar\"}\
                               ]";

    #[cfg(windows)]
    static SERIALIZED: &str = "[\
                               {\"type\":\"file\",\"path\":\"{0}\\\\foo.txt\"},\
                               {\"type\":\"dir\",\"path\":\"{0}\\\\bar\"},\
                               {\"type\":\"dir\",\"path\":\"{0}\\\\foo\\\\bar\"}\
                               ]";

    #[test]
    fn sanity_serde() {
        use serde_json;
        use tempdir::TempDir;

        let tmp_dir = TempDir::new("example").expect("create temp dir");
        let tmp_abs = PathDir::new(tmp_dir.path()).expect("tmp_abs");

        let ser_from_str = PathSer::from("example");
        let ser_from_tmp_abs = PathSer::from(tmp_abs.as_path());

        let foo = PathFile::create(tmp_abs.concat("foo.txt").unwrap()).expect("foo.txt");
        let bar_dir = PathDir::create(tmp_abs.concat("bar").unwrap()).expect("bar");
        let foo_bar_dir =
            PathDir::create_all(tmp_abs.concat("foo").unwrap().concat("bar").unwrap())
                .expect("foo/bar");

        let expected = vec![
            PathType::File(foo),
            PathType::Dir(bar_dir),
            PathType::Dir(foo_bar_dir),
        ];

        let expected_str = SERIALIZED
            .replace("{0}", &tmp_abs.to_stfu8())
            // JSON needs backslashes escaped. Be careful not to invoke BA'AL:
            // https://xkcd.com/1638/)
            .replace(r"\", r"\\");

        println!("### EXPECTED:\n{}", expected_str);
        let result_str = serde_json::to_string(&expected).unwrap();
        println!("### RESULT:\n{}", result_str);
        assert_eq!(expected_str, result_str);

        let result: Vec<PathType> = serde_json::from_str(&result_str).unwrap();
        assert_eq!(expected, result);
    }

    #[test]
    /// Just test that it has all the methods.
    fn sanity_ser() {
        let mut path = PathSer::from("example/path");
        assert_eq!(
            path.join("joined").as_path(),
            Path::new("example/path/joined")
        );
        assert_eq!(path.is_absolute(), false);

        path.append("appended").unwrap();
        assert_eq!(path.as_path(), Path::new("example/path/appended"));
        path.pop_up().unwrap();
        assert_eq!(path.as_path(), Path::new("example/path"));

        assert_eq!(
            path.concat("/concated").unwrap().as_path(),
            Path::new("example/path/concated")
        );
    }
}