Skip to main content

Writer

Struct Writer 

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

Builds a MaxMind DB file from inserted networks.

The writer accumulates encoded data records, deduplicating equal payloads, and serializes a search tree plus metadata when Writer::write_to is called.

§Example

use maxminddb::writer::{Writer, WriterOptions};

let opts = WriterOptions {
    database_type: "Test".to_string(),
    ip_version: 4,
    record_size: 24,
    ..Default::default()
};
let mut writer = Writer::new(opts).unwrap();
writer.insert("10.0.0.0/8".parse().unwrap(), "example").unwrap();

let mut buf = Vec::new();
writer.write_to(&mut buf).unwrap();
let reader = maxminddb::Reader::from_source(buf).unwrap();
assert_eq!(reader.lookup::<String>("10.0.0.1".parse().unwrap()).unwrap(), "example");

Implementations§

Source§

impl Writer

Source

pub fn new(opts: WriterOptions) -> WriterResult<Self>

Create a writer.

§Errors

Returns an error if ip_version is not 4 or 6, or if record_size is not 24, 28, or 32.

Source

pub fn insert_value( &mut self, network: IpNetwork, value: Value, ) -> WriterResult<()>

Insert a fully encoded Value for network.

Equal values are stored once in the data section and referenced by every network that uses them.

§Errors

Returns an error if an IPv6 network is inserted into an IPv4 database or if the value cannot be encoded.

§Overwriting

Inserting a network replaces the subtree at that prefix. Inserting a less specific network after a more specific one therefore discards the more specific entries, so insert least specific networks first.

Source

pub fn insert<T: Serialize>( &mut self, network: IpNetwork, value: T, ) -> WriterResult<()>

Insert a Serialize value for network.

This is the same as Writer::insert_value but encodes value for you.

§Errors

Returns an error if value cannot be encoded, if an IPv6 network is inserted into an IPv4 database, or if the value is out of range for the MaxMind DB format (for example a negative integer below i32::MIN).

§Overwriting

See Writer::insert_value: insert least specific networks first.

Source

pub fn write_to<W: Write>(&mut self, writer: &mut W) -> WriterResult<u64>

Serialize the database into writer, returning the number of bytes written.

This finalizes node numbering, so call it once, after all inserts.

§Errors

Returns an error if a record value does not fit the configured record size, if the node count exceeds u32, or if writing to writer fails.

Trait Implementations§

Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.