Skip to content

Commit

Permalink
Merge pull request #115 from KodrAus/chore/re-usable-filter-builder
Browse files Browse the repository at this point in the history
Prevent filter::Builder from being reused
  • Loading branch information
KodrAus committed Nov 6, 2018
2 parents 299713a + 0002fdd commit 051d5e9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/filter/mod.rs
Expand Up @@ -116,6 +116,7 @@ pub struct Filter {
pub struct Builder {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
built: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -183,6 +184,7 @@ impl Builder {
Builder {
directives: Vec::new(),
filter: None,
built: false,
}
}

Expand Down Expand Up @@ -239,6 +241,9 @@ impl Builder {

/// Build a log filter.
pub fn build(&mut self) -> Filter {
assert!(!self.built, "attempt to re-use consumed builder");
self.built = true;

if self.directives.is_empty() {
// Adds the default filter if none exist
self.directives.push(Directive {
Expand Down Expand Up @@ -279,10 +284,16 @@ impl fmt::Debug for Filter {

impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Filter")
if self.built {
f.debug_struct("Filter")
.field("built", &true)
.finish()
} else {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}
}

Expand Down

0 comments on commit 051d5e9

Please sign in to comment.