win_subst/
lib.rs

1extern crate windows;
2use windows::{
3    core::{PCWSTR, HSTRING},
4    Win32::Storage::FileSystem::{DEFINE_DOS_DEVICE_FLAGS, DefineDosDeviceW},
5};
6
7pub fn add(link: &str, target: &str) -> bool {
8    unsafe {
9        return DefineDosDeviceW(DEFINE_DOS_DEVICE_FLAGS(0), &HSTRING::from(link), &HSTRING::from(target)).as_bool();
10    }
11}
12
13pub fn del(link: &str) -> bool {
14    unsafe {
15        return DefineDosDeviceW(DEFINE_DOS_DEVICE_FLAGS(0x2), &HSTRING::from(link), PCWSTR::null()).as_bool();
16    }
17}
18
19#[cfg(test)]
20mod it_works {
21    use super::*;
22    use std::env;
23    use std::path::Path;
24
25    #[test]
26    fn test_for_add() {
27        assert_eq!(add("T:", env::current_dir().unwrap().as_path().to_str().unwrap()), true);
28        assert_eq!(Path::new("T:").exists(), true);
29    }
30
31    #[test]
32    fn test_for_del() {
33        assert_eq!(del("T:"), true);
34        assert_eq!(Path::new("T:").exists(), false);
35    }
36}