Converts all try! macros to ? syntax.

Fixes #46
This commit is contained in:
rep-nop
2017-02-25 22:12:33 -05:00
committed by Jakob Stoklund Olesen
parent c8be39fa9d
commit 7459fee71a
22 changed files with 270 additions and 270 deletions

View File

@@ -38,7 +38,7 @@ impl Directive {
}
// All other commands are followed by a pattern.
let pat = try!(rest.parse());
let pat = rest.parse()?;
match cmd {
"check" => Ok(Directive::Check(pat)),
@@ -99,7 +99,7 @@ impl CheckerBuilder {
pub fn directive(&mut self, l: &str) -> Result<bool> {
match self.linerx.captures(l) {
Some(caps) => {
self.directives.push(try!(Directive::new(caps)));
self.directives.push(Directive::new(caps)?);
Ok(true)
}
None => Ok(false),
@@ -112,7 +112,7 @@ impl CheckerBuilder {
/// This method can be used to parse a whole test file containing multiple directives.
pub fn text(&mut self, t: &str) -> Result<&mut Self> {
for caps in self.linerx.captures_iter(t) {
self.directives.push(try!(Directive::new(caps)));
self.directives.push(Directive::new(caps)?);
}
Ok(self)
}
@@ -154,7 +154,7 @@ impl Checker {
/// Explain how directives are matched against the input text.
pub fn explain(&self, text: &str, vars: &VariableMap) -> Result<(bool, String)> {
let mut expl = Explainer::new(text);
let success = try!(self.run(text, vars, &mut expl));
let success = self.run(text, vars, &mut expl)?;
expl.finish();
Ok((success, expl.to_string()))
}
@@ -178,7 +178,7 @@ impl Checker {
// The `not:` directives test the same range as `unordered:` directives. In
// particular, if they refer to defined variables, their range is restricted to
// the text following the match that defined the variable.
nots.push((dct_idx, state.unordered_begin(pat), try!(pat.resolve(&state))));
nots.push((dct_idx, state.unordered_begin(pat), pat.resolve(&state)?));
continue;
}
Directive::Regex(ref var, ref rx) => {
@@ -192,7 +192,7 @@ impl Checker {
};
// Check if `pat` matches in `range`.
state.recorder.directive(dct_idx);
if let Some((match_begin, match_end)) = try!(state.match_positive(pat, range)) {
if let Some((match_begin, match_end)) = state.match_positive(pat, range)? {
if let &Directive::Unordered(_) = dct {
// This was an unordered unordered match.
// Keep track of the largest matched position, but leave `last_ordered` alone.
@@ -318,7 +318,7 @@ impl<'a> State<'a> {
// Search for `pat` in `range`, return the range matched.
// After a positive match, update variable definitions, if any.
fn match_positive(&mut self, pat: &Pattern, range: MatchRange) -> Result<Option<MatchRange>> {
let rx = try!(pat.resolve(self));
let rx = pat.resolve(self)?;
let txt = &self.text[range.0..range.1];
let defs = pat.defs();
let matched_range = if defs.is_empty() {
@@ -382,7 +382,7 @@ impl Display for Directive {
impl Display for Checker {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
for (idx, dir) in self.directives.iter().enumerate() {
try!(write!(f, "#{} {}", idx, dir));
write!(f, "#{} {}", idx, dir)?;
}
Ok(())
}

View File

@@ -89,34 +89,34 @@ impl<'a> Display for Explainer<'a> {
.map(|d| nextln + d + 1)
.unwrap_or(self.text.len());
assert!(newln > nextln);
try!(writeln!(f, "> {}", &self.text[nextln..newln - 1]));
writeln!(f, "> {}", &self.text[nextln..newln - 1])?;
curln = nextln;
nextln = newln;
}
// Emit ~~~ under the part of the match in curln.
if m.is_match {
try!(write!(f, " "));
write!(f, " ")?;
let mend = min(m.range.1, nextln - 1);
for pos in curln..mend {
try!(if pos < m.range.0 {
if pos < m.range.0 {
write!(f, " ")
} else if pos == m.range.0 {
write!(f, "^")
} else {
write!(f, "~")
});
}?;
}
try!(writeln!(f, ""));
writeln!(f, "")?;
}
// Emit the match message itself.
try!(writeln!(f,
writeln!(f,
"{} #{}{}: {}",
if m.is_match { "Matched" } else { "Missed" },
m.directive,
if m.is_not { " not" } else { "" },
m.regex));
m.regex)?;
// Emit any variable definitions.
if let Ok(found) = self.vardefs.binary_search_by_key(&m.directive, |v| v.directive) {
@@ -128,14 +128,14 @@ impl<'a> Display for Explainer<'a> {
if d.directive != m.directive {
break;
}
try!(writeln!(f, "Define {}={}", d.varname, d.value));
writeln!(f, "Define {}={}", d.varname, d.value)?;
}
}
}
// Emit trailing lines.
for line in self.text[nextln..].lines() {
try!(writeln!(f, "> {}", line));
writeln!(f, "> {}", line)?;
}
Ok(())
}

View File

@@ -147,7 +147,7 @@ impl Pattern {
let def = if varname.is_empty() {
None
} else {
Some(try!(self.add_def(&varname)))
Some(self.add_def(&varname)?)
};
// Match `$(var=$PAT)`.
@@ -270,7 +270,7 @@ impl FromStr for Pattern {
let mut pat = Pattern::new();
let mut pos = 0;
while pos < s.len() {
let (part, len) = try!(pat.parse_part(&s[pos..]));
let (part, len) = pat.parse_part(&s[pos..])?;
if let Some(v) = part.ref_var() {
if pat.defines_var(v) {
return Err(Error::Backref(format!("unsupported back-reference to '${}' \
@@ -353,7 +353,7 @@ impl Pattern {
}
}
Ok(try!(RegexBuilder::new(&out).multi_line(true).compile()))
Ok(RegexBuilder::new(&out).multi_line(true).compile()?)
}
}
@@ -361,7 +361,7 @@ impl Display for Pattern {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
for part in &self.parts {
use self::Part::*;
try!(match *part {
match *part {
Text(ref txt) if txt == "" => write!(f, "$()"),
Text(ref txt) if txt == "$" => write!(f, "$$"),
Text(ref txt) => write!(f, "{}", txt),
@@ -374,7 +374,7 @@ impl Display for Pattern {
write!(f, "$({}={})", defvar, litrx)
}
DefVar { def, ref var } => write!(f, "$({}=${})", self.defs[def], var),
});
}?;
}
Ok(())
}