DatabaseBuilder

Struct DatabaseBuilder 

Source
pub struct DatabaseBuilder { /* private fields */ }
Expand description

Unified database builder

Implementations§

Source§

impl DatabaseBuilder

Source

pub fn new(match_mode: MatchMode) -> Self

Source

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");
Source

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");
Source

pub fn with_validator(self, validator: Box<dyn EntryValidator>) -> Self

Set an entry validator for schema validation

Source

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");
Source

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);
Source

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.

Source

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.

Source

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)?;
Source

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)?;
Source

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)?;
Source

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:

  1. Try parsing as IP address/CIDR
  2. If contains glob chars (*, ?, [), validate as glob pattern
  3. 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(_))));
Source

pub fn build(self) -> Result<Vec<u8>, FormatError>

Build the unified MMDB database

Source

pub fn stats(&self) -> BuilderStats

Get statistics about the builder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.