pub struct DatabaseBuilder { /* private fields */ }Expand description
Unified database builder
Implementations§
Source§impl DatabaseBuilder
impl DatabaseBuilder
pub fn new(match_mode: MatchMode) -> Self
Sourcepub fn with_database_type(self, db_type: impl Into<String>) -> Self
pub fn with_database_type(self, db_type: impl Into<String>) -> Self
Set a custom database type name
If not set, defaults to “Paraglob-Combined-IP-Pattern” or “Paraglob-IP”
§Example
use matchy_format::DatabaseBuilder;
use matchy_match_mode::MatchMode;
let builder = DatabaseBuilder::new(MatchMode::CaseSensitive)
.with_database_type("MyCompany-ThreatIntel");Sourcepub fn with_description(
self,
language: impl Into<String>,
text: impl Into<String>,
) -> Self
pub fn with_description( self, language: impl Into<String>, text: impl Into<String>, ) -> Self
Add a description in a specific language
Can be called multiple times for different languages. If not called, defaults to English description.
§Example
use matchy_format::DatabaseBuilder;
use matchy_match_mode::MatchMode;
let builder = DatabaseBuilder::new(MatchMode::CaseSensitive)
.with_description("en", "My custom threat database")
.with_description("es", "Mi base de datos de amenazas personalizada");Sourcepub fn with_validator(self, validator: Box<dyn EntryValidator>) -> Self
pub fn with_validator(self, validator: Box<dyn EntryValidator>) -> Self
Set an entry validator for schema validation
Sourcepub fn with_update_url(self, url: impl Into<String>) -> Self
pub fn with_update_url(self, url: impl Into<String>) -> Self
Set the URL where updates to this database can be downloaded
When set, this URL is stored in the database metadata. Applications can use
Database::update_url() to retrieve it and implement auto-update functionality.
§Example
use matchy_format::DatabaseBuilder;
use matchy_match_mode::MatchMode;
let builder = DatabaseBuilder::new(MatchMode::CaseSensitive)
.with_update_url("https://example.com/threats.mxy");Sourcepub fn with_match_mode(self, match_mode: MatchMode) -> Self
pub fn with_match_mode(self, match_mode: MatchMode) -> Self
Set the match mode for pattern matching
This controls whether literal and glob pattern matching is case-sensitive or case-insensitive. IP address matching is always case-insensitive regardless of this setting.
§Example
use matchy_format::DatabaseBuilder;
use matchy_match_mode::MatchMode;
let builder = DatabaseBuilder::new(MatchMode::CaseSensitive)
.with_match_mode(MatchMode::CaseInsensitive);Sourcepub fn set_match_mode(&mut self, match_mode: MatchMode)
pub fn set_match_mode(&mut self, match_mode: MatchMode)
Set the match mode (mutable borrow version)
This is useful when you need to change the match mode after construction without consuming the builder.
Sourcepub fn add_entry(
&mut self,
key: &str,
data: HashMap<String, DataValue>,
) -> Result<(), FormatError>
pub fn add_entry( &mut self, key: &str, data: HashMap<String, DataValue>, ) -> Result<(), FormatError>
Add an entry with auto-detection
Automatically detects whether the key is an IP address, literal string, or glob pattern.
For explicit control, use add_ip(), add_literal(), or add_glob().
If a validator is configured via with_validator, the entry
data will be validated before insertion. Returns an error if validation fails.
Sourcepub fn add_literal(
&mut self,
pattern: &str,
data: HashMap<String, DataValue>,
) -> Result<(), FormatError>
pub fn add_literal( &mut self, pattern: &str, data: HashMap<String, DataValue>, ) -> Result<(), FormatError>
Add a literal string pattern (exact match only, no wildcards)
Use this when the string contains characters like ‘*’, ‘?’, or ‘[’ that should be matched literally rather than as glob wildcards.
If a validator is configured via with_validator, the entry
data will be validated before insertion. Returns an error if validation fails.
§Example
let mut builder = DatabaseBuilder::new(MatchMode::CaseSensitive);
let mut data = HashMap::new();
data.insert("note".to_string(), DataValue::String("literal".to_string()));
// This has '[' but we want to match it literally
builder.add_literal("file[1].txt", data)?;Sourcepub fn add_glob(
&mut self,
pattern: &str,
data: HashMap<String, DataValue>,
) -> Result<(), FormatError>
pub fn add_glob( &mut self, pattern: &str, data: HashMap<String, DataValue>, ) -> Result<(), FormatError>
Add a glob pattern (with wildcard matching)
Use this to explicitly mark a pattern for glob matching, even if it doesn’t contain obvious wildcard characters.
If a validator is configured via with_validator, the entry
data will be validated before insertion. Returns an error if validation fails.
§Example
let mut builder = DatabaseBuilder::new(MatchMode::CaseSensitive);
let mut data = HashMap::new();
data.insert("category".to_string(), DataValue::String("malware".to_string()));
builder.add_glob("*.evil.com", data)?;Sourcepub fn add_ip(
&mut self,
ip_or_cidr: &str,
data: HashMap<String, DataValue>,
) -> Result<(), FormatError>
pub fn add_ip( &mut self, ip_or_cidr: &str, data: HashMap<String, DataValue>, ) -> Result<(), FormatError>
Add an IP address or CIDR block
Use this to explicitly mark an entry as an IP address. Will return an error if the string is not a valid IP address or CIDR notation.
If a validator is configured via with_validator, the entry
data will be validated before insertion. Returns an error if validation fails.
§Arguments
ip_or_cidr- IP address or CIDR range (e.g., “192.168.1.0/24”)data- HashMap of key-value pairs to associate with the IP
§Errors
Returns an error if the IP address or CIDR format is invalid, or if validation fails.
§Example
let mut builder = DatabaseBuilder::new(MatchMode::CaseSensitive);
let mut data = HashMap::new();
data.insert("country".to_string(), DataValue::String("US".to_string()));
builder.add_ip("192.168.1.0/24", data)?;Sourcepub fn detect_entry_type(key: &str) -> Result<EntryType, FormatError>
pub fn detect_entry_type(key: &str) -> Result<EntryType, FormatError>
Auto-detect if key is an IP/CIDR, literal, or glob pattern
Supports explicit type prefixes for disambiguation:
literal:- Force literal string matching (strips prefix)glob:- Force glob pattern matching (strips prefix)ip:- Force IP address parsing (strips prefix)
Without a prefix, auto-detection is used:
- Try parsing as IP address/CIDR
- If contains glob chars (*, ?, [), validate as glob pattern
- Otherwise treat as literal string
§Examples
// Auto-detection
assert!(matches!(DatabaseBuilder::detect_entry_type("1.2.3.4"), Ok(EntryType::IpAddress { .. })));
assert!(matches!(DatabaseBuilder::detect_entry_type("*.example.com"), Ok(EntryType::Glob(_))));
assert!(matches!(DatabaseBuilder::detect_entry_type("evil.com"), Ok(EntryType::Literal(_))));
// Explicit type control
assert!(matches!(DatabaseBuilder::detect_entry_type("literal:*.not-a-glob.com"), Ok(EntryType::Literal(_))));
assert!(matches!(DatabaseBuilder::detect_entry_type("glob:no-wildcards.com"), Ok(EntryType::Glob(_))));Sourcepub fn stats(&self) -> BuilderStats
pub fn stats(&self) -> BuilderStats
Get statistics about the builder
Auto Trait Implementations§
impl Freeze for DatabaseBuilder
impl !RefUnwindSafe for DatabaseBuilder
impl Send for DatabaseBuilder
impl Sync for DatabaseBuilder
impl Unpin for DatabaseBuilder
impl !UnwindSafe for DatabaseBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more