about summary refs log tree commit diff
path: root/wikarden/src/error.rs
blob: d4bc9a4d46fbca1c725acbf112c7d6655ec184bd (plain)
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
use std::fmt;

use camino::Utf8PathBuf;

use crate::WikPath;

#[derive(Debug)]
pub enum FileOp {
	Open,
	Read,
	Write,
	Delete,
}

impl fmt::Display for FileOp {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let statstr = match self {
			Self::Open => "Open",
			Self::Read => "Read",
			Self::Write => "Write",
			Self::Delete => "Delete",
		};
		write!(f, "{statstr}")
	}
}

#[derive(Debug)]
pub enum WikError {
	PageExists {
		name: String,
	},
	PageNoexist {
		name: String,
	},
	SectionExists {
		name: String,
	},
	SectionNoexist {
		name: String,
	},
	InvalidName {
		name: String,
		reason: &'static str,
	},

	CanonicalizationFailed {
		path: Utf8PathBuf,
	},
	IllegalPath {
		wik: WikPath,
		canon: Utf8PathBuf,
	},

	FileOperation {
		op: FileOp,
		path: Utf8PathBuf,
		io: tokio::io::Error,
	},
	DirectoryCreation {
		path: Utf8PathBuf,
	},
	JournalError {
		reason: &'static str,
	},

	GitError {
		git: git2::Error,
	},
}

impl From<git2::Error> for WikError {
	fn from(value: git2::Error) -> Self {
		Self::GitError { git: value }
	}
}

impl WikError {
	pub fn page_exists(path: WikPath) -> Self {
		Self::PageExists {
			name: path.to_string(),
		}
	}

	pub fn page_noexists(path: WikPath) -> Self {
		Self::PageNoexist {
			name: path.to_string(),
		}
	}

	pub fn section_exists(path: WikPath) -> Self {
		Self::SectionExists {
			name: path.to_string(),
		}
	}

	pub fn section_noexist(path: WikPath) -> Self {
		Self::SectionNoexist {
			name: path.to_string(),
		}
	}

	pub fn invalid_name<P: Into<String>>(name: P, reason: &'static str) -> Self {
		Self::InvalidName {
			name: name.into(),
			reason,
		}
	}

	pub fn name_has_slashes<P: Into<String>>(name: P) -> Self {
		Self::InvalidName {
			name: name.into(),
			reason: "names cannot contain slashes (\"/\")",
		}
	}

	pub fn canonicalization_failed(path: Utf8PathBuf) -> Self {
		Self::CanonicalizationFailed { path }
	}

	pub fn illegal_path(wik: WikPath, canon: Utf8PathBuf) -> Self {
		Self::IllegalPath { wik, canon }
	}

	pub fn file_operation(
		op: FileOp,
		path: Utf8PathBuf,
		io: tokio::io::Error,
	) -> Result<(), WikError> {
		Err(Self::FileOperation { op, path, io })
	}

	pub fn journal(reason: &'static str) -> Self {
		Self::JournalError { reason }
	}
}

impl fmt::Display for WikError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::PageExists { name } => {
				write!(f, "page '{name}' already exists")
			}
			Self::PageNoexist { name } => {
				write!(f, "page '{name}' does not exist")
			}
			Self::SectionExists { name } => {
				write!(f, "section '{name}' already exists")
			}
			Self::SectionNoexist { name } => {
				write!(f, "section '{name}' does not exist")
			}
			Self::InvalidName { name, reason } => {
				write!(f, "the name '{name}' is invalid: {reason}")
			}
			Self::CanonicalizationFailed { path } => {
				write!(f, "failed to get absolute path for '{path}'")
			}
			Self::DirectoryCreation { path } => {
				write!(f, "failed to create directory '{path}'")
			}
			Self::FileOperation { op, path, io } => {
				write!(f, "failed to {op} '{path}': {io}")
			}
			Self::GitError { git } => {
				write!(f, "git error: {git}")
			}
			Self::IllegalPath { wik, canon } => {
				write!(f, "path '{wik}' is invalid. system_path='{canon}'")
			}
			Self::JournalError { reason } => {
				write!(f, "journal error: {reason}")
			}
		}
	}
}