1
1
use std:: fmt;
2
2
use std:: io:: { BufWriter , Read , Write } ;
3
+ use std:: slice:: Iter ;
3
4
#[ cfg( feature = "logging" ) ]
4
5
use std:: time:: Instant ;
5
6
@@ -20,7 +21,7 @@ use crate::compiler::{
20
21
} ;
21
22
use crate :: re:: { BckCodeLoc , FwdCodeLoc , RegexpAtom } ;
22
23
use crate :: string_pool:: { BStringPool , StringPool } ;
23
- use crate :: { re, types} ;
24
+ use crate :: { re, types, Rule } ;
24
25
25
26
/// A set of YARA rules in compiled form.
26
27
///
@@ -201,6 +202,29 @@ impl Rules {
201
202
Self :: deserialize ( bytes)
202
203
}
203
204
205
+ /// Returns an iterator that yields the compiled rules.
206
+ ///
207
+ /// ```rust
208
+ /// # use yara_x::Compiler;
209
+ /// let mut compiler = Compiler::new();
210
+ ///
211
+ /// assert!(compiler
212
+ /// .add_source("rule foo {condition: true}")
213
+ /// .unwrap()
214
+ /// .add_source("rule bar {condition: true}")
215
+ /// .is_ok());
216
+ ///
217
+ /// let rules = compiler.build();
218
+ /// let mut iter = rules.iter();
219
+ ///
220
+ /// assert_eq!(iter.len(), 2);
221
+ /// assert_eq!(iter.next().map(|r| r.identifier()), Some("foo"));
222
+ /// assert_eq!(iter.next().map(|r| r.identifier()), Some("bar"));
223
+ /// ```
224
+ pub fn iter ( & self ) -> RulesIter {
225
+ RulesIter { rules : self , iterator : self . rules . iter ( ) }
226
+ }
227
+
204
228
/// Returns a [`RuleInfo`] given its [`RuleId`].
205
229
///
206
230
/// # Panics
@@ -401,6 +425,32 @@ impl Rules {
401
425
}
402
426
}
403
427
428
+ /// Iterator that yields the of the compiled rules.
429
+ pub struct RulesIter < ' a > {
430
+ rules : & ' a Rules ,
431
+ iterator : Iter < ' a , RuleInfo > ,
432
+ }
433
+
434
+ impl < ' a > Iterator for RulesIter < ' a > {
435
+ type Item = Rule < ' a , ' a > ;
436
+
437
+ fn next ( & mut self ) -> Option < Self :: Item > {
438
+ Some ( Rule {
439
+ ctx : None ,
440
+ data : None ,
441
+ rules : self . rules ,
442
+ rule_info : self . iterator . next ( ) ?,
443
+ } )
444
+ }
445
+ }
446
+
447
+ impl ExactSizeIterator for RulesIter < ' _ > {
448
+ #[ inline]
449
+ fn len ( & self ) -> usize {
450
+ self . iterator . len ( )
451
+ }
452
+ }
453
+
404
454
fn serialize_wasm_mod < S > (
405
455
wasm_mod : & wasmtime:: Module ,
406
456
serializer : S ,
0 commit comments