Skip to content
Permalink
Browse files

cleanup: replace try! with ?

  • Loading branch information
balajisivaraman authored and BurntSushi committed Jan 1, 2018
1 parent ba1023e commit b6177f0459044a7e3fb882ecda9c80e44e4d95de
@@ -36,7 +36,7 @@ This example shows how to match a single glob against a single file path.
```rust
use globset::Glob;
let glob = try!(Glob::new("*.rs")).compile_matcher();
let glob = Glob::new("*.rs")?.compile_matcher();
assert!(glob.is_match("foo.rs"));
assert!(glob.is_match("foo/bar.rs"));
@@ -51,8 +51,8 @@ semantics. In this example, we prevent wildcards from matching path separators.
```rust
use globset::GlobBuilder;
let glob = try!(GlobBuilder::new("*.rs")
.literal_separator(true).build()).compile_matcher();
let glob = GlobBuilder::new("*.rs")
.literal_separator(true).build()?.compile_matcher();
assert!(glob.is_match("foo.rs"));
assert!(!glob.is_match("foo/bar.rs")); // no longer matches
@@ -69,10 +69,10 @@ use globset::{Glob, GlobSetBuilder};
let mut builder = GlobSetBuilder::new();
// A GlobBuilder can be used to configure each glob's match semantics
// independently.
builder.add(try!(Glob::new("*.rs")));
builder.add(try!(Glob::new("src/lib.rs")));
builder.add(try!(Glob::new("src/**/foo.rs")));
let set = try!(builder.build());
builder.add(Glob::new("*.rs")?);
builder.add(Glob::new("src/lib.rs")?);
builder.add(Glob::new("src/**/foo.rs")?);
let set = builder.build()?;
assert_eq!(set.matches("src/bar/baz/foo.rs"), vec![0, 2]);
```
@@ -550,7 +550,7 @@ impl<'a> GlobBuilder<'a> {
prev: None,
cur: None,
};
try!(p.parse());
p.parse()?;
if p.stack.is_empty() {
Err(Error {
glob: Some(self.glob.to_string()),
@@ -720,18 +720,18 @@ impl<'a> Parser<'a> {
fn parse(&mut self) -> Result<(), Error> {
while let Some(c) = self.bump() {
match c {
'?' => try!(self.push_token(Token::Any)),
'*' => try!(self.parse_star()),
'[' => try!(self.parse_class()),
'{' => try!(self.push_alternate()),
'}' => try!(self.pop_alternate()),
',' => try!(self.parse_comma()),
'?' => self.push_token(Token::Any)?,
'*' => self.parse_star()?,
'[' => self.parse_class()?,
'{' => self.push_alternate()?,
'}' => self.pop_alternate()?,
',' => self.parse_comma()?,
c => {
if is_separator(c) {
// Normalize all patterns to use / as a separator.
try!(self.push_token(Token::Literal('/')))
self.push_token(Token::Literal('/'))?
} else {
try!(self.push_token(Token::Literal(c)))
self.push_token(Token::Literal(c))?
}
}
}
@@ -789,19 +789,19 @@ impl<'a> Parser<'a> {
fn parse_star(&mut self) -> Result<(), Error> {
let prev = self.prev;
if self.chars.peek() != Some(&'*') {
try!(self.push_token(Token::ZeroOrMore));
self.push_token(Token::ZeroOrMore)?;
return Ok(());
}
assert!(self.bump() == Some('*'));
if !try!(self.have_tokens()) {
try!(self.push_token(Token::RecursivePrefix));
if !self.have_tokens()? {
self.push_token(Token::RecursivePrefix)?;
let next = self.bump();
if !next.map(is_separator).unwrap_or(true) {
return Err(self.error(ErrorKind::InvalidRecursive));
}
return Ok(());
}
try!(self.pop_token());
self.pop_token()?;
if !prev.map(is_separator).unwrap_or(false) {
if self.stack.len() <= 1
|| (prev != Some(',') && prev != Some('{')) {
@@ -873,7 +873,7 @@ impl<'a> Parser<'a> {
// invariant: in_range is only set when there is
// already at least one character seen.
let r = ranges.last_mut().unwrap();
try!(add_to_last_range(&self.glob, r, '-'));
add_to_last_range(&self.glob, r, '-')?;
in_range = false;
} else {
assert!(!ranges.is_empty());
@@ -884,8 +884,8 @@ impl<'a> Parser<'a> {
if in_range {
// invariant: in_range is only set when there is
// already at least one character seen.
try!(add_to_last_range(
&self.glob, ranges.last_mut().unwrap(), c));
add_to_last_range(
&self.glob, ranges.last_mut().unwrap(), c)?;
} else {
ranges.push((c, c));
}
@@ -22,7 +22,7 @@ This example shows how to match a single glob against a single file path.
# fn example() -> Result<(), globset::Error> {
use globset::Glob;
let glob = try!(Glob::new("*.rs")).compile_matcher();
let glob = Glob::new("*.rs")?.compile_matcher();
assert!(glob.is_match("foo.rs"));
assert!(glob.is_match("foo/bar.rs"));
@@ -39,8 +39,8 @@ semantics. In this example, we prevent wildcards from matching path separators.
# fn example() -> Result<(), globset::Error> {
use globset::GlobBuilder;
let glob = try!(GlobBuilder::new("*.rs")
.literal_separator(true).build()).compile_matcher();
let glob = GlobBuilder::new("*.rs")
.literal_separator(true).build()?.compile_matcher();
assert!(glob.is_match("foo.rs"));
assert!(!glob.is_match("foo/bar.rs")); // no longer matches
@@ -59,10 +59,10 @@ use globset::{Glob, GlobSetBuilder};
let mut builder = GlobSetBuilder::new();
// A GlobBuilder can be used to configure each glob's match semantics
// independently.
builder.add(try!(Glob::new("*.rs")));
builder.add(try!(Glob::new("src/lib.rs")));
builder.add(try!(Glob::new("src/**/foo.rs")));
let set = try!(builder.build());
builder.add(Glob::new("*.rs")?);
builder.add(Glob::new("src/lib.rs")?);
builder.add(Glob::new("src/**/foo.rs")?);
let set = builder.build()?;
assert_eq!(set.matches("src/bar/baz/foo.rs"), vec![0, 2]);
# Ok(()) } example().unwrap();
@@ -412,8 +412,8 @@ impl GlobSet {
GlobSetMatchStrategy::Suffix(suffixes.suffix()),
GlobSetMatchStrategy::Prefix(prefixes.prefix()),
GlobSetMatchStrategy::RequiredExtension(
try!(required_exts.build())),
GlobSetMatchStrategy::Regex(try!(regexes.regex_set())),
required_exts.build()?),
GlobSetMatchStrategy::Regex(regexes.regex_set()?),
],
})
}
@@ -767,7 +767,7 @@ impl MultiStrategyBuilder {

fn regex_set(self) -> Result<RegexSetStrategy, Error> {
Ok(RegexSetStrategy {
matcher: try!(new_regex_set(self.literals)),
matcher: new_regex_set(self.literals)?,
map: self.map,
})
}
@@ -792,7 +792,7 @@ impl RequiredExtensionStrategyBuilder {
for (ext, regexes) in self.0.into_iter() {
exts.insert(ext.clone(), vec![]);
for (global_index, regex) in regexes {
let compiled = try!(new_regex(&regex));
let compiled = new_regex(&regex)?;
exts.get_mut(&ext).unwrap().push((global_index, compiled));
}
}
@@ -44,25 +44,23 @@ pub fn remove(expr: Expr, byte: u8) -> Result<Expr> {
}
Group { e, i, name } => {
Group {
e: Box::new(try!(remove(*e, byte))),
e: Box::new(remove(*e, byte)?),
i: i,
name: name,
}
}
Repeat { e, r, greedy } => {
Repeat {
e: Box::new(try!(remove(*e, byte))),
e: Box::new(remove(*e, byte)?),
r: r,
greedy: greedy,
}
}
Concat(exprs) => {
Concat(try!(
exprs.into_iter().map(|e| remove(e, byte)).collect()))
Concat(exprs.into_iter().map(|e| remove(e, byte)).collect::<Result<Vec<Expr>>>()?)
}
Alternate(exprs) => {
Alternate(try!(
exprs.into_iter().map(|e| remove(e, byte)).collect()))
Alternate(exprs.into_iter().map(|e| remove(e, byte)).collect::<Result<Vec<Expr>>>()?)
}
e => e,
})
@@ -141,11 +141,11 @@ impl GrepBuilder {
/// If there was a problem parsing or compiling the regex with the given
/// options, then an error is returned.
pub fn build(self) -> Result<Grep> {
let expr = try!(self.parse());
let expr = self.parse()?;
let literals = LiteralSets::create(&expr);
let re = try!(self.regex(&expr));
let re = self.regex(&expr)?;
let required = match literals.to_regex_builder() {
Some(builder) => Some(try!(self.regex_build(builder))),
Some(builder) => Some(self.regex_build(builder)?),
None => {
match strip_unicode_word_boundaries(&expr) {
None => None,
@@ -186,12 +186,12 @@ impl GrepBuilder {
/// the line terminator.
fn parse(&self) -> Result<syntax::Expr> {
let expr =
try!(syntax::ExprBuilder::new()
.allow_bytes(true)
.unicode(true)
.case_insensitive(try!(self.is_case_insensitive()))
.parse(&self.pattern));
let expr = try!(nonl::remove(expr, self.opts.line_terminator));
syntax::ExprBuilder::new()
.allow_bytes(true)
.unicode(true)
.case_insensitive(self.is_case_insensitive()?)
.parse(&self.pattern)?;
let expr = nonl::remove(expr, self.opts.line_terminator)?;
debug!("regex ast:\n{:#?}", expr);
Ok(expr)
}
@@ -322,13 +322,13 @@ impl GitignoreBuilder {
pub fn build(&self) -> Result<Gitignore, Error> {
let nignore = self.globs.iter().filter(|g| !g.is_whitelist()).count();
let nwhite = self.globs.iter().filter(|g| g.is_whitelist()).count();
let set = try!(
let set =
self.builder.build().map_err(|err| {
Error::Glob {
glob: None,
err: err.to_string(),
}
}));
})?;
Ok(Gitignore {
set: set,
root: self.root.clone(),
@@ -383,7 +383,7 @@ impl GitignoreBuilder {
gitignore: &str,
) -> Result<&mut GitignoreBuilder, Error> {
for line in gitignore.lines() {
try!(self.add_line(from.clone(), line));
self.add_line(from.clone(), line)?;
}
Ok(self)
}
@@ -465,7 +465,7 @@ impl GitignoreBuilder {
if glob.actual.ends_with("/**") {
glob.actual = format!("{}/*", glob.actual);
}
let parsed = try!(
let parsed =
GlobBuilder::new(&glob.actual)
.literal_separator(literal_separator)
.case_insensitive(self.case_insensitive)
@@ -475,7 +475,7 @@ impl GitignoreBuilder {
glob: Some(glob.original.clone()),
err: err.kind().to_string(),
}
}));
})?;
self.builder.add(parsed);
self.globs.push(glob);
Ok(self)
@@ -124,7 +124,7 @@ impl OverrideBuilder {
///
/// Once a matcher is built, no new globs can be added to it.
pub fn build(&self) -> Result<Override, Error> {
Ok(Override(try!(self.builder.build())))
Ok(Override(self.builder.build()?))
}

/// Add a glob to the set of overrides.
@@ -134,7 +134,7 @@ impl OverrideBuilder {
/// namely, `!` at the beginning of a glob will ignore a file. Without `!`,
/// all matches of the glob provided are treated as whitelist matches.
pub fn add(&mut self, glob: &str) -> Result<&mut OverrideBuilder, Error> {
try!(self.builder.add_line(None, glob));
self.builder.add_line(None, glob)?;
Ok(self)
}

@@ -144,7 +144,7 @@ impl OverrideBuilder {
pub fn case_insensitive(
&mut self, yes: bool
) -> Result<&mut OverrideBuilder, Error> {
try!(self.builder.case_insensitive(yes));
self.builder.case_insensitive(yes)?;
Ok(self)
}
}
@@ -526,7 +526,7 @@ impl TypesBuilder {
}
};
for (iglob, glob) in def.globs.iter().enumerate() {
build_set.add(try!(
build_set.add(
GlobBuilder::new(glob)
.literal_separator(true)
.build()
@@ -535,14 +535,14 @@ impl TypesBuilder {
glob: Some(glob.to_string()),
err: err.kind().to_string(),
}
})));
})?);
glob_to_selection.push((isel, iglob));
}
selections.push(selection.clone().map(move |_| def));
}
let set = try!(build_set.build().map_err(|err| {
let set = build_set.build().map_err(|err| {
Error::Glob { glob: None, err: err.to_string() }
}));
})?;
Ok(Types {
defs: defs,
selections: selections,
@@ -655,7 +655,7 @@ impl TypesBuilder {
for type_name in types {
let globs = self.types.get(type_name).unwrap().globs.clone();
for glob in globs {
try!(self.add(name, &glob));
self.add(name, &glob)?;
}
}
Ok(())
@@ -279,13 +279,13 @@ impl DirEntryRaw {
depth: usize,
ent: &fs::DirEntry,
) -> Result<DirEntryRaw, Error> {
let ty = try!(ent.file_type().map_err(|err| {
let ty = ent.file_type().map_err(|err| {
let err = Error::Io(io::Error::from(err)).with_path(ent.path());
Error::WithDepth {
depth: depth,
err: Box::new(err),
}
}));
})?;
Ok(DirEntryRaw::from_entry_os(depth, ent, ty))
}

@@ -322,9 +322,9 @@ impl DirEntryRaw {

#[cfg(not(unix))]
fn from_link(depth: usize, pb: PathBuf) -> Result<DirEntryRaw, Error> {
let md = try!(fs::metadata(&pb).map_err(|err| {
let md = fs::metadata(&pb).map_err(|err| {
Error::Io(err).with_path(&pb)
}));
})?;
Ok(DirEntryRaw {
path: pb,
ty: md.file_type(),
@@ -337,9 +337,9 @@ impl DirEntryRaw {
fn from_link(depth: usize, pb: PathBuf) -> Result<DirEntryRaw, Error> {
use std::os::unix::fs::MetadataExt;

let md = try!(fs::metadata(&pb).map_err(|err| {
let md = fs::metadata(&pb).map_err(|err| {
Error::Io(err).with_path(&pb)
}));
})?;
Ok(DirEntryRaw {
path: pb,
ty: md.file_type(),

0 comments on commit b6177f0

Please sign in to comment.
You can’t perform that action at this time.