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
|
use std::path::PathBuf;
use just_fmt::kebab_case;
use mingling::{
Groupped,
macros::{chain, dispatcher, renderer},
parser::AsPicker,
};
use serde::Serialize;
use crate::Constants;
const SUGGEST_READ: &str = "Please use `memm read {}` to view the memory again";
const SUGGEST_EXPLORE: &str = "Please use `memm explore` to view available memories";
dispatcher!("remember", CMDRemember => EntryRemember);
dispatcher!("rewrite", CMDRewrite => EntryRewrite);
dispatcher!("forget", CMDForget => EntryForget);
dispatcher!("explore", CMDExplore => EntryExplore);
dispatcher!("read", CMDRead => EntryRead);
dispatcher!("dumpall", CMDDumpAll => EntryDumpAll);
#[chain]
pub fn handle_remember(args: EntryRemember, constants: &Constants) -> Next {
let (title, content) = args.pick(()).pick::<Vec<String>>(()).unpack();
remember(constants, title, content.join(" ")).to_render()
}
#[chain]
pub fn handle_rewrite(args: EntryRewrite, constants: &Constants) -> Next {
let (title, content) = args.pick(()).pick::<Vec<String>>(()).unpack();
rewrite(constants, title, content.join(" ")).to_render()
}
#[chain]
pub fn handle_forget(args: EntryForget, constants: &Constants) -> Next {
let title = args.pick(()).unpack();
forget(constants, title).to_render()
}
#[chain]
pub fn handle_explore(_p: EntryExplore, constants: &Constants) -> Next {
explore(constants).to_render()
}
#[chain]
pub fn handle_read(args: EntryRead, constants: &Constants) -> Next {
let title = args.pick(()).unpack();
read(constants, title).to_render()
}
#[chain]
pub fn handle_dumpall(args: EntryDumpAll, constants: &Constants) -> Next {
let about = args.pick::<String>("--about").unpack();
dumpall(constants, about).to_render()
}
#[derive(Serialize, Groupped)]
pub struct ResultExplore {
pub titles: Vec<String>,
}
#[renderer]
pub fn phantom_render_result_explore(_p: ResultExplore) {}
#[derive(Serialize, Groupped)]
pub struct ResultRead {
pub exist: bool,
pub read_success: bool,
pub content_lines: Vec<String>,
}
#[renderer]
pub fn phantom_render_result_read(_p: ResultRead) {}
#[derive(Serialize, Groupped)]
pub struct ResultWritten {
pub exist: bool,
pub write_success: bool,
pub suggest: String,
}
#[renderer]
pub fn phantom_render_result_written(_p: ResultWritten) {}
#[derive(Serialize, Groupped)]
pub struct ResultForgotten {
pub exist: bool,
pub forget_success: bool,
pub suggest: String,
}
#[renderer]
pub fn phantom_render_result_forgotten(_p: ResultForgotten) {}
#[derive(Serialize, Groupped)]
pub struct ResultDumpAll {
#[serde(rename = "IMPORTANT")]
pub important: Vec<ImportantEntry>,
pub entries: Vec<DumpEntry>,
}
#[derive(Serialize)]
pub struct DumpEntry {
pub title: String,
pub content: String,
}
#[derive(Serialize)]
pub struct ImportantEntry {
pub title: String,
pub reason: String,
}
#[renderer]
pub fn phantom_render_result_dumpall(_p: ResultDumpAll) {}
fn explore(constants: &Constants) -> ResultExplore {
let store_root = &constants.store_root;
let mut titles = Vec::new();
if let Ok(entries) = std::fs::read_dir(store_root) {
for entry in entries.flatten() {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "md" {
if let Some(stem) = path.file_stem() {
titles.push(stem.to_string_lossy().to_string());
}
}
}
}
}
ResultExplore { titles }
}
fn read(constants: &Constants, title: String) -> ResultRead {
let target_file = item_path(constants, &title);
if !target_file.exists() {
return ResultRead {
exist: false,
read_success: false,
content_lines: Vec::new(),
};
}
match std::fs::read_to_string(&target_file) {
Ok(content) => ResultRead {
exist: true,
read_success: true,
content_lines: content.lines().map(|line| line.to_string()).collect(),
},
Err(_) => ResultRead {
exist: true,
read_success: false,
content_lines: Vec::new(),
},
}
}
fn rewrite(constants: &Constants, title: String, content: String) -> ResultWritten {
let content = content.replace("\\n", "\n");
let target_file = item_path(constants, &title);
match std::fs::write(&target_file, content) {
Ok(_) => ResultWritten {
exist: true,
write_success: true,
suggest: SUGGEST_READ.replace("{}", &title),
},
Err(_) => ResultWritten {
exist: true,
write_success: false,
suggest: SUGGEST_READ.replace("{}", &title),
},
}
}
fn remember(constants: &Constants, title: String, content: String) -> ResultWritten {
let content = content.replace("\\n", "\n");
let target_file = item_path(constants, &title);
match std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(&target_file)
{
Ok(mut file) => {
use std::io::Write;
match writeln!(file, "{}", content) {
Ok(_) => ResultWritten {
exist: true,
write_success: true,
suggest: SUGGEST_READ.replace("{}", &title),
},
Err(_) => ResultWritten {
exist: true,
write_success: false,
suggest: SUGGEST_READ.replace("{}", &title),
},
}
}
Err(_) => ResultWritten {
exist: false,
write_success: false,
suggest: String::new(),
},
}
}
fn forget(constants: &Constants, title: String) -> ResultForgotten {
let target_file = item_path(constants, &title);
if !target_file.exists() {
return ResultForgotten {
exist: false,
forget_success: false,
suggest: SUGGEST_EXPLORE.to_string(),
};
}
match std::fs::remove_file(&target_file) {
Ok(_) => ResultForgotten {
exist: true,
forget_success: true,
suggest: SUGGEST_EXPLORE.to_string(),
},
Err(_) => ResultForgotten {
exist: true,
forget_success: false,
suggest: SUGGEST_EXPLORE.to_string(),
},
}
}
fn dumpall(constants: &Constants, about: String) -> ResultDumpAll {
let store_root = &constants.store_root;
let mut entries = Vec::new();
if let Ok(dir_entries) = std::fs::read_dir(store_root) {
for entry in dir_entries.flatten() {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "md" {
if let Some(stem) = path.file_stem() {
let title = stem.to_string_lossy().to_string();
let content = std::fs::read_to_string(&path).unwrap_or_default();
// If about is provided and not empty, do fuzzy matching on lowercase alphanumeric
if !about.is_empty() {
let filter: String = about
.chars()
.filter(|c| c.is_alphanumeric())
.collect::<String>()
.to_lowercase();
let title_clean: String = title
.chars()
.filter(|c| c.is_alphanumeric())
.collect::<String>()
.to_lowercase();
if !title_clean.contains(&filter) {
continue;
}
}
entries.push(DumpEntry { title, content });
}
}
}
}
}
// Scan for IMPORTANT entries
let important: Vec<ImportantEntry> = entries
.iter()
.filter_map(|entry| {
let title_upper = entry.title.to_uppercase();
let content_upper = entry.content.to_uppercase();
if title_upper.contains("IMPORTANT") {
Some(ImportantEntry {
title: entry.title.clone(),
reason: "title contains IMPORTANT".to_string(),
})
} else if content_upper.contains("IMPORTANT") {
Some(ImportantEntry {
title: entry.title.clone(),
reason: "content contains IMPORTANT".to_string(),
})
} else {
None
}
})
.collect();
ResultDumpAll { entries, important }
}
fn item_path(constants: &Constants, title: &String) -> PathBuf {
constants
.store_root
.join(format!("{}.md", kebab_case!(title)))
}
|