Mercurial > hg-stable
annotate rust/hg-core/src/errors.rs @ 46744:3d692e724d06
rhg: Align config file parse error formatting with Python
Differences can cause tests to fail
Differential Revision: https://phab.mercurial-scm.org/D10110
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Wed, 03 Mar 2021 19:47:48 +0100 |
parents | 1f55cd5b292f |
children | 12d59eec7f1d |
rev | line source |
---|---|
46637
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
1 use crate::config::ConfigValueParseError; |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
2 use std::fmt; |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
3 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
4 /// Common error cases that can happen in many different APIs |
46637
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
5 #[derive(Debug, derive_more::From)] |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
6 pub enum HgError { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
7 IoError { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
8 error: std::io::Error, |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
9 context: IoErrorContext, |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
10 }, |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
11 |
46544
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
12 /// A file under `.hg/` normally only written by Mercurial is not in the |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
13 /// expected format. This indicates a bug in Mercurial, filesystem |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
14 /// corruption, or hardware failure. |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
15 /// |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
16 /// The given string is a short explanation for users, not intended to be |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
17 /// machine-readable. |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
18 CorruptedRepository(String), |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
19 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
20 /// The respository or requested operation involves a feature not |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
21 /// supported by the Rust implementation. Falling back to the Python |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
22 /// implementation may or may not work. |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
23 /// |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
24 /// The given string is a short explanation for users, not intended to be |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
25 /// machine-readable. |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
26 UnsupportedFeature(String), |
46544
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
27 |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
28 /// Operation cannot proceed for some other reason. |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
29 /// |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
30 /// The given string is a short explanation for users, not intended to be |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
31 /// machine-readable. |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
32 Abort(String), |
46637
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
33 |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
34 /// A configuration value is not in the expected syntax. |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
35 /// |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
36 /// These errors can happen in many places in the code because values are |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
37 /// parsed lazily as the file-level parser does not know the expected type |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
38 /// and syntax of each value. |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
39 #[from] |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
40 ConfigValueParseError(ConfigValueParseError), |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
41 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
42 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
43 /// Details about where an I/O error happened |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
44 #[derive(Debug)] |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
45 pub enum IoErrorContext { |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
46 ReadingFile(std::path::PathBuf), |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
47 WritingFile(std::path::PathBuf), |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
48 RemovingFile(std::path::PathBuf), |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
49 RenamingFile { |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
50 from: std::path::PathBuf, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
51 to: std::path::PathBuf, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
52 }, |
46542
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46524
diff
changeset
|
53 /// `std::env::current_dir` |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
54 CurrentDir, |
46542
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46524
diff
changeset
|
55 /// `std::env::current_exe` |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46524
diff
changeset
|
56 CurrentExe, |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
57 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
58 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
59 impl HgError { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
60 pub fn corrupted(explanation: impl Into<String>) -> Self { |
46511
43d63979a75e
rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents:
46506
diff
changeset
|
61 // TODO: capture a backtrace here and keep it in the error value |
43d63979a75e
rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents:
46506
diff
changeset
|
62 // to aid debugging? |
43d63979a75e
rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents:
46506
diff
changeset
|
63 // https://doc.rust-lang.org/std/backtrace/struct.Backtrace.html |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
64 HgError::CorruptedRepository(explanation.into()) |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
65 } |
46524
d03b0601e0eb
rhg: initial support for shared repositories
Simon Sapin <simon.sapin@octobus.net>
parents:
46511
diff
changeset
|
66 |
d03b0601e0eb
rhg: initial support for shared repositories
Simon Sapin <simon.sapin@octobus.net>
parents:
46511
diff
changeset
|
67 pub fn unsupported(explanation: impl Into<String>) -> Self { |
d03b0601e0eb
rhg: initial support for shared repositories
Simon Sapin <simon.sapin@octobus.net>
parents:
46511
diff
changeset
|
68 HgError::UnsupportedFeature(explanation.into()) |
d03b0601e0eb
rhg: initial support for shared repositories
Simon Sapin <simon.sapin@octobus.net>
parents:
46511
diff
changeset
|
69 } |
46544
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
70 pub fn abort(explanation: impl Into<String>) -> Self { |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
71 HgError::Abort(explanation.into()) |
f031fe1c6ede
rhg: Abort based on config on share-safe mismatch
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
72 } |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
73 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
74 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
75 // TODO: use `DisplayBytes` instead to show non-Unicode filenames losslessly? |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
76 impl fmt::Display for HgError { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
78 match self { |
46637
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
79 HgError::Abort(explanation) => write!(f, "{}", explanation), |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
80 HgError::IoError { error, context } => { |
46744
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
81 write!(f, "abort: {}: {}", context, error) |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
82 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
83 HgError::CorruptedRepository(explanation) => { |
46744
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
84 write!(f, "abort: corrupted repository: {}", explanation) |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
85 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
86 HgError::UnsupportedFeature(explanation) => { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
87 write!(f, "unsupported feature: {}", explanation) |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
88 } |
46637
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
89 HgError::ConfigValueParseError(ConfigValueParseError { |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
90 origin: _, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
91 line: _, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
92 section, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
93 item, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
94 value, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
95 expected_type, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
96 }) => { |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
97 // TODO: add origin and line number information, here and in |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
98 // corresponding python code |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
99 write!( |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
100 f, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
101 "config error: {}.{} is not a {} ('{}')", |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
102 String::from_utf8_lossy(section), |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
103 String::from_utf8_lossy(item), |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
104 expected_type, |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
105 String::from_utf8_lossy(value) |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
106 ) |
bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Simon Sapin <simon.sapin@octobus.net>
parents:
46544
diff
changeset
|
107 } |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
108 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
109 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
110 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
111 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
112 // TODO: use `DisplayBytes` instead to show non-Unicode filenames losslessly? |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
113 impl fmt::Display for IoErrorContext { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
114 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
115 match self { |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
116 IoErrorContext::ReadingFile(path) => { |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
117 write!(f, "when reading {}", path.display()) |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
118 } |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
119 IoErrorContext::WritingFile(path) => { |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
120 write!(f, "when writing {}", path.display()) |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
121 } |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
122 IoErrorContext::RemovingFile(path) => { |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
123 write!(f, "when removing {}", path.display()) |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
124 } |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
125 IoErrorContext::RenamingFile { from, to } => write!( |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
126 f, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
127 "when renaming {} to {}", |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
128 from.display(), |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
129 to.display() |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
130 ), |
46744
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
131 IoErrorContext::CurrentDir => { |
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
132 write!(f, "error getting current working directory") |
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
133 } |
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
134 IoErrorContext::CurrentExe => { |
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
135 write!(f, "error getting current executable") |
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46638
diff
changeset
|
136 } |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
137 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
138 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
139 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
140 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
141 pub trait IoResultExt<T> { |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
142 /// Annotate a possible I/O error as related to a reading a file at the |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
143 /// given path. |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
144 /// |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
145 /// This allows printing something like “File not found when reading |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
146 /// example.txt” instead of just “File not found”. |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
147 /// |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
148 /// Converts a `Result` with `std::io::Error` into one with `HgError`. |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
149 fn when_reading_file(self, path: &std::path::Path) -> Result<T, HgError>; |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
150 |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
151 fn with_context( |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
152 self, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
153 context: impl FnOnce() -> IoErrorContext, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
154 ) -> Result<T, HgError>; |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
155 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
156 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
157 impl<T> IoResultExt<T> for std::io::Result<T> { |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
158 fn when_reading_file(self, path: &std::path::Path) -> Result<T, HgError> { |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
159 self.with_context(|| IoErrorContext::ReadingFile(path.to_owned())) |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
160 } |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
161 |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
162 fn with_context( |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
163 self, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
164 context: impl FnOnce() -> IoErrorContext, |
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
165 ) -> Result<T, HgError> { |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
166 self.map_err(|error| HgError::IoError { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
167 error, |
46638
1f55cd5b292f
rust: Add a log file rotation utility
Simon Sapin <simon.sapin@octobus.net>
parents:
46637
diff
changeset
|
168 context: context(), |
46506
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
169 }) |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
170 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
171 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
172 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
173 pub trait HgResultExt<T> { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
174 /// Handle missing files separately from other I/O error cases. |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
175 /// |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
176 /// Wraps the `Ok` type in an `Option`: |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
177 /// |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
178 /// * `Ok(x)` becomes `Ok(Some(x))` |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
179 /// * An I/O "not found" error becomes `Ok(None)` |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
180 /// * Other errors are unchanged |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
181 fn io_not_found_as_none(self) -> Result<Option<T>, HgError>; |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
182 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
183 |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
184 impl<T> HgResultExt<T> for Result<T, HgError> { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
185 fn io_not_found_as_none(self) -> Result<Option<T>, HgError> { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
186 match self { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
187 Ok(x) => Ok(Some(x)), |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
188 Err(HgError::IoError { error, .. }) |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
189 if error.kind() == std::io::ErrorKind::NotFound => |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
190 { |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
191 Ok(None) |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
192 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
193 Err(other_error) => Err(other_error), |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
194 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
195 } |
39e9407820ac
rust: Introduce an `HgError` enum for common error cases
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
196 } |