|
1 |
| -use std::collections::HashSet; |
2 |
| -use std::fs::File; |
3 | 1 | use std::path::PathBuf;
|
4 | 2 | use std::{fs, io, path::Path};
|
5 | 3 |
|
6 |
| -use serde::{Deserialize, Serialize}; |
7 |
| -use serde_json as json; |
8 |
| - |
9 | 4 | // An object that represents a file or a whole directory.
|
10 | 5 | // note: can not be root directory
|
11 |
| -#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 6 | +#[derive(Clone, PartialEq, Eq, Hash)] |
12 | 7 | pub(crate) struct Obj(PathBuf);
|
13 | 8 |
|
14 | 9 | impl Obj {
|
@@ -69,65 +64,3 @@ impl AsRef<Path> for Obj {
|
69 | 64 | &self.0
|
70 | 65 | }
|
71 | 66 | }
|
72 |
| - |
73 |
| -// An object that represents a file or a whole directory, which has a UTF-8 name. |
74 |
| -#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
75 |
| -pub(crate) struct ObjName(String); |
76 |
| - |
77 |
| -impl ObjName { |
78 |
| - pub(crate) fn base<P: AsRef<Path>>(&self, path: P) -> Obj { |
79 |
| - Obj(path.as_ref().join(&self.0)) |
80 |
| - } |
81 |
| -} |
82 |
| - |
83 |
| -#[derive(Serialize, Deserialize)] |
84 |
| -pub(crate) struct ShareDir { |
85 |
| - path: PathBuf, |
86 |
| - used_by: HashSet<String>, |
87 |
| -} |
88 |
| - |
89 |
| -impl ShareDir { |
90 |
| - pub(crate) fn get<P: AsRef<Path>>(path: P) -> io::Result<Self> { |
91 |
| - let file = path.as_ref().join(".tytm.fsx.lock"); |
92 |
| - |
93 |
| - Ok(if file.is_file() { |
94 |
| - let dir: ShareDir = json::from_reader(File::open(&file)?)?; |
95 |
| - if dir.path != path.as_ref() { |
96 |
| - panic!("Got a broken ShareDir at {:?}", path.as_ref()) |
97 |
| - } |
98 |
| - dir |
99 |
| - } else { |
100 |
| - if !path.as_ref().is_dir() { |
101 |
| - fs::create_dir_all(&path)?; |
102 |
| - } |
103 |
| - |
104 |
| - Self { |
105 |
| - path: path.as_ref().to_path_buf(), |
106 |
| - used_by: HashSet::new(), |
107 |
| - } |
108 |
| - }) |
109 |
| - } |
110 |
| - |
111 |
| - pub(crate) fn used_by<S: ToString>(&mut self, by: S) -> io::Result<()> { |
112 |
| - self.used_by.insert(by.to_string()); |
113 |
| - self.save() |
114 |
| - } |
115 |
| - |
116 |
| - pub(crate) fn removed_by<S: AsRef<str>>(&mut self, by: S) -> io::Result<()> { |
117 |
| - self.used_by.retain(|x| x != by.as_ref()); |
118 |
| - if self.used_by.is_empty() { |
119 |
| - Obj::from(self.path.clone()).remove() |
120 |
| - } else { |
121 |
| - self.save() |
122 |
| - } |
123 |
| - } |
124 |
| - |
125 |
| - // this will return error if you tend to remove a deleted directory |
126 |
| - fn save(&self) -> io::Result<()> { |
127 |
| - let file = self.path.join(".tytm.fsx.lock"); |
128 |
| - if file.is_file() { |
129 |
| - fs::remove_file(&file)?; |
130 |
| - } |
131 |
| - json::to_writer(File::create(&file)?, self).map_err(Into::into) |
132 |
| - } |
133 |
| -} |
0 commit comments