-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52aadff
commit c7aa27e
Showing
6 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use core::fmt; | ||
|
||
use quick_js::Context; | ||
|
||
pub struct ConditionFilter { | ||
context: Context, | ||
condition: String, | ||
} | ||
|
||
impl ConditionFilter { | ||
pub fn new<S>(condition: S) -> Self | ||
where | ||
S: Into<String>, | ||
{ | ||
Self { | ||
context: Context::new().unwrap(), | ||
condition: condition.into(), | ||
} | ||
} | ||
pub fn filter(&self, line: &str) -> bool { | ||
self.context | ||
.eval_as::<bool>( | ||
format!("(function (){{return ({})}}).call({line})", self.condition).as_str(), | ||
) | ||
.unwrap() | ||
} | ||
} | ||
|
||
impl fmt::Debug for ConditionFilter { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "ConditionFilter [`{}`]", self.condition) | ||
} | ||
} | ||
|
||
impl Clone for ConditionFilter { | ||
fn clone(&self) -> Self { | ||
Self { | ||
context: Context::new().unwrap(), | ||
condition: self.condition.clone(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters