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
pub use {legacy::LegacyToolchainFile, toml::RustToolchainToml};
pub mod legacy;
pub mod toml;
#[cfg(test)]
mod tests;
#[derive(Debug, PartialEq)]
pub enum ToolchainFile {
Legacy(LegacyToolchainFile),
Toml(RustToolchainToml),
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Variant {
Legacy,
Toml,
}
impl Variant {
fn parse_with(&self, content: &str) -> Result<ToolchainFile, ParserError> {
match *self {
Self::Legacy => legacy::Parser::new(content)
.parse()
.map(ToolchainFile::Legacy)
.map_err(From::from),
Self::Toml => toml::Parser::new(content)
.parse()
.map(ToolchainFile::Toml)
.map_err(From::from),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ParseStrategy {
Only(Variant),
Fallback {
first: Variant,
fallback_to: Variant,
},
}
pub struct Parser<'content> {
content: &'content str,
parse_option: ParseStrategy,
}
impl<'content> Parser<'content> {
pub fn new(content: &'content str, parse_option: ParseStrategy) -> Self {
Self {
content,
parse_option,
}
}
}
impl Parser<'_> {
pub fn parse(&self) -> Result<ToolchainFile, ParserError> {
match self.parse_option {
ParseStrategy::Only(v) => v.parse_with(self.content),
ParseStrategy::Fallback { first, fallback_to } => {
first.parse_with(self.content).or_else(|original_err| {
fallback_to
.parse_with(self.content)
.map_err(|fallback_err| {
ParserError::FallbackError(FallbackError {
first: Box::new(original_err),
fallback_to: Box::new(fallback_err),
})
})
})
}
}
}
}
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum ParserError {
#[error("Failed to parse legacy toolchain-file variant: {0}")]
LegacyParseError(#[from] legacy::ParserError),
#[error("Failed to parse TOML toolchain-file variant: {0}")]
TomlParseError(#[from] toml::ParserError),
#[error("Both original and fallback parse attempts failed: {0}")]
FallbackError(FallbackError),
}
#[derive(Debug, PartialEq, thiserror::Error)]
#[error("Failed to parse: '{first}' and failed to fallback on '{fallback_to}'")]
pub struct FallbackError {
first: Box<ParserError>,
fallback_to: Box<ParserError>,
}
impl FallbackError {
pub fn first(&self) -> &ParserError {
self.first.as_ref()
}
pub fn fallback_to(&self) -> &ParserError {
self.fallback_to.as_ref()
}
}