minos_codex/scanner/
mod.rs

1use crate::config::Config;
2use crate::regex::RegexCache;
3use crate::secret_type::SecretType;
4use crate::MinosCodexError;
5use std::collections::HashMap;
6
7pub struct Scanner {
8    config: Config,
9    regex_cache: RegexCache,
10}
11
12#[derive(Debug)]
13pub struct FoundSecret {
14    pub secret_type: String,
15    pub value: String,
16    pub start: usize,
17    pub end: usize,
18}
19
20impl Scanner {
21    pub fn new(config: Config) -> Self {
22        Scanner {
23            config,
24            regex_cache: RegexCache::new(),
25        }
26    }
27
28    pub fn scan(&mut self, input: &str) -> Result<Vec<FoundSecret>, MinosCodexError> {
29        let mut found_secrets = Vec::new();
30
31        for secret_type in self.config.secret_types() {
32            let regex = self.regex_cache.get_or_insert(&secret_type.regex)?;
33            for capture in regex.captures_iter(input) {
34                let matched = capture.get(0).unwrap();
35                found_secrets.push(FoundSecret {
36                    secret_type: secret_type.name.clone(),
37                    value: matched.as_str().to_string(),
38                    start: matched.start(),
39                    end: matched.end(),
40                });
41            }
42        }
43
44        Ok(found_secrets)
45    }
46}