forked from Wulf/dsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.rs
108 lines (93 loc) · 3.5 KB
/
file.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
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
use std::path::PathBuf;
use crate::{Result, IOErrorToError, Error};
pub struct MarkedFile {
pub file_contents: String,
pub path: PathBuf,
}
impl MarkedFile {
pub fn new(path: PathBuf) -> Result<MarkedFile> {
Ok(MarkedFile {
path: path.clone(),
file_contents: if !path.exists() {
std::fs::write(&path, "").attach_path_err(&path)?;
"".to_string()
} else {
std::fs::read_to_string(&path).attach_path_err(&path)?
},
})
}
pub fn has_use_stmt(&self, use_name: &str) -> bool {
self.file_contents.contains(&format!("pub use {use_name};"))
}
pub fn has_mod_stmt(&self, mod_name: &str) -> bool {
self.file_contents.contains(&format!("pub mod {mod_name};"))
}
pub fn add_use_stmt(&mut self, use_name: &str) {
self.file_contents = self.file_contents.trim().to_string();
if !self.file_contents.is_empty() {
self.file_contents.push('\n');
}
self.file_contents
.push_str(&format!("pub use {use_name};\n"))
}
pub fn add_mod_stmt(&mut self, mod_name: &str) {
self.file_contents = self.file_contents.trim().to_string();
if !self.file_contents.is_empty() {
self.file_contents.push('\n');
}
self.file_contents
.push_str(&format!("pub mod {mod_name};\n"))
}
pub fn remove_use_stmt(&mut self, mod_name: &str) {
let content_to_remove = &format!("pub use {mod_name};");
if self.file_contents.contains(content_to_remove) {
self.file_contents = self
.file_contents
.replace(content_to_remove, "")
.trim()
.to_string();
}
}
pub fn remove_mod_stmt(&mut self, mod_name: &str) {
let content_to_remove = &format!("pub mod {mod_name};");
if self.file_contents.contains(content_to_remove) {
self.file_contents = self
.file_contents
.replace(content_to_remove, "")
.trim()
.to_string();
}
}
pub fn ensure_use_stmt(&mut self, use_name: &str) {
if !self.has_use_stmt(use_name) {
self.add_use_stmt(use_name)
}
}
pub fn ensure_mod_stmt(&mut self, mod_name: &str) {
if !self.has_mod_stmt(mod_name) {
self.add_mod_stmt(mod_name)
}
}
pub fn has_file_signature(&self) -> bool {
// the reason we consider filelength=0 as having a file signature is because
// the whole purpose of file signatures is to prevent writing to files which aren't generated
// and if a file's length is 0, then it's safe to write to this file!
// :)
self.file_contents.is_empty()
|| self
.file_contents
.starts_with(crate::parser::FILE_SIGNATURE)
}
pub fn ensure_file_signature(&self) -> Result<()> {
if !self.has_file_signature() {
return Err(Error::no_file_signature(format!("Expected file '{path:#?}' to have file signature ('{sig}') -- you might be accidentally overwriting files that weren't generated!", path=self.path, sig=crate::parser::FILE_SIGNATURE)));
}
Ok(())
}
pub fn write(&self) -> Result<()> {
std::fs::write(&self.path, &self.file_contents).attach_path_err(&self.path)
}
pub fn delete(self) -> Result<()> {
std::fs::remove_file(&self.path).attach_path_err(&self.path)
}
}