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
use toml::{encode, Table};
use difference::Changeset;
use dev_prefix::*;
use types::*;
use user::types::*;
use user::name;
use user::serialize;
#[derive(Debug, PartialEq)]
pub struct ProjectText {
pub origin: PathBuf,
pub files: HashMap<PathBuf, String>,
}
pub enum PathDiff {
DoesNotExist,
NotUtf8,
Changeset(Changeset),
None,
}
impl Default for ProjectText {
fn default() -> ProjectText {
ProjectText {
origin: PathBuf::from("INVALID-ORIGIN"),
files: HashMap::default(),
}
}
}
impl ProjectText {
pub fn from_project(project: &Project) -> Result<ProjectText> {
let mut files = HashMap::new();
for (name, artifact) in &project.artifacts {
if !files.contains_key(&artifact.def) {
files.insert(artifact.def.clone(), Table::new());
}
let tbl = files.get_mut(&artifact.def).unwrap();
let partof = {
let mut auto_partof = name.named_partofs();
if !name.is_root() {
auto_partof.push(name.parent().expect("no parent"));
}
let auto_partof: HashSet<Name> = HashSet::from_iter(auto_partof.drain(0..));
let strs = artifact
.partof
.iter()
.filter(|p| !auto_partof.contains(p))
.map(|p| p.raw.clone())
.collect::<Vec<_>>();
if strs.is_empty() {
None
} else {
Some(name::collapse_names(strs))
}
};
let raw = UserArtifact {
partof: partof,
text: if artifact.text.is_empty() {
None
} else {
Some(artifact.text.clone())
},
done: if let Done::Defined(ref d) = artifact.done {
Some(d.clone())
} else {
None
},
};
tbl.insert(name.raw.clone(), encode(&raw));
}
let mut text: HashMap<PathBuf, String> = HashMap::new();
for (p, v) in files.drain() {
text.insert(p, serialize::pretty_toml(&v)?);
}
for p in &project.files {
if !text.contains_key(p) {
text.insert(p.clone(), String::with_capacity(0));
}
}
Ok(ProjectText {
files: text,
origin: project.origin.clone(),
})
}
pub fn dump(&self) -> Result<()> {
for (path, text) in &self.files {
debug!("writing to {}", path.display());
if let Err(err) = fs::create_dir_all(path.parent().expect("path not file")) {
match err.kind() {
io::ErrorKind::AlreadyExists => {}
_ => return Err(err.into()),
}
}
let mut f = fs::File::create(path)?;
f.write_all(text.as_bytes())?;
}
Ok(())
}
pub fn diff(&self) -> Result<HashMap<PathBuf, PathDiff>> {
let mut out: HashMap<PathBuf, PathDiff> = HashMap::new();
for (path, text) in &self.files {
debug!("diffing: {}", path.display());
let mut f = match fs::File::open(path) {
Ok(f) => f,
Err(_) => {
out.insert(path.clone(), PathDiff::DoesNotExist);
continue;
}
};
let mut bytes = Vec::new();
f.read_to_end(&mut bytes)?;
let original = match str::from_utf8(&bytes) {
Ok(s) => s,
Err(_) => {
out.insert(path.clone(), PathDiff::NotUtf8);
continue;
}
};
let ch = Changeset::new(original, text, "\n");
let d = if ch.distance == 0 {
PathDiff::None
} else {
PathDiff::Changeset(ch)
};
out.insert(path.clone(), d);
}
Ok(out)
}
}