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
|
use std::{
collections::HashMap,
fs::{self, File},
io::{BufRead, BufReader},
path::Path,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::PageUuid;
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct Page {
pub uuid: PageUuid,
pub author: Uuid,
pub title: String,
pub current_version: DateTime<Utc>,
pub prev_versions: Vec<DateTime<Utc>>,
content_offset: usize,
}
pub struct Pages {
pub pages: HashMap<PageUuid, Page>,
}
const METADATA_DIVIDER: &'static str = "<!-- trans rights ~ath&+ -->";
#[cfg(feature = "ssr")]
impl Pages {
pub fn init(pages_dir: &Path) -> Result<Self, String> {
// Read dir
let page_dirs = fs::read_dir(&pages_dir)
.map_err(|_| "Could not open pages data directory".to_string())?;
// Parse each
let pages = page_dirs
.map(|dir_entry| -> Result<Page, String> {
let page_dir_path = dir_entry.as_ref().unwrap().path();
Pages::read_page(&page_dir_path)
})
.collect::<Result<Vec<Page>, String>>()?;
// Build lookup
Ok(Self {
pages: pages
.into_iter()
.map(|page| (page.uuid.clone(), page))
.collect::<HashMap<_, _>>(),
})
}
fn read_page(page_dir: &Path) -> Result<Page, String> {
let current_page = page_dir
.join("current")
.canonicalize()
.map_err(|_| "Could not canonicalize page location".to_string())?;
let mut reader = BufReader::new(
File::open(¤t_page).map_err(|_| "Could not open page file".to_string())?,
);
let page_uuid = PageUuid(
Uuid::try_parse(
&page_dir
.file_name()
.ok_or("Could not read page directory".to_string())?
.to_str()
.unwrap(),
)
.map_err(|_| "Could not parse page UUID".to_string())?,
);
let mut metadata_string = String::new();
let mut current_line = String::new();
let mut content_offset = 0;
'readloop: while let Ok(size) = reader.read_line(&mut current_line) {
content_offset += size;
if size == 0 {
return Err("Page file is invalid".to_string());
}
if current_line.trim() == METADATA_DIVIDER {
break 'readloop;
}
metadata_string.push_str(¤t_line);
current_line.truncate(0);
}
#[derive(Deserialize)]
struct PageMetadata {
title: String,
author: String,
prev_versions: Option<Vec<String>>,
}
let metadata: PageMetadata = toml::from_str(&metadata_string).map_err(|err| {
println!("{err:?}");
"Page metadata is invalid".to_string()
})?;
let current_version = DateTime::parse_from_rfc3339(
current_page
.file_name()
.unwrap()
.to_str()
.unwrap()
.replace("_", ":")
.as_str(),
)
.map_err(|_| "Invalid date format".to_string())?
.to_utc();
let prev_versions = metadata
.prev_versions
.unwrap_or(Vec::new())
.iter()
.filter_map(|str| {
DateTime::parse_from_rfc3339(str.replace("_", ":").as_str())
.ok()
.map(|timestamp| timestamp.to_utc())
})
.collect::<Vec<_>>();
Ok(Page {
uuid: page_uuid,
author: Uuid::try_parse(&metadata.author)
.map_err(|_| "Could not parse author UUID".to_string())?,
title: metadata.title,
current_version,
prev_versions,
content_offset,
})
}
pub fn get_page(&self, uuid: PageUuid) -> Option<Page> {
todo!()
}
pub fn create_page(&self, page: Page) {
todo!()
}
pub fn update_page(&self, page: Page) {
todo!()
}
pub fn delete_page(&self, uuid: PageUuid) -> Result<(), String> {
todo!()
}
}
|