ipwhois-rust 1.2.0

Official Rust client for the ipwhois.io IP Geolocation API. Simple, supports single and bulk IP lookups.
Documentation
//! Per-call options for [`crate::IpWhois::lookup_with`] and
//! [`crate::IpWhois::bulk_lookup_with`], and the same shape used to set
//! defaults on the client.
//!
//! Four fields, exposed as a typed builder: `lang`, `fields`, `security`,
//! `rate`.

/// Options accepted by lookup calls and by the client as defaults.
///
/// Every field is optional. Per-call options always override the defaults
/// stored on the client.
///
/// # Example
///
/// ```
/// use ipwhois::Options;
///
/// let opts = Options::new()
///     .with_lang("en")
///     .with_fields(["success", "country", "city", "flag.emoji"])
///     .with_security(true)
///     .with_rate(true);
/// ```
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Options {
    pub(crate) lang: Option<String>,
    pub(crate) fields: Option<Vec<String>>,
    pub(crate) security: Option<bool>,
    pub(crate) rate: Option<bool>,
}

impl Options {
    /// Create an empty options set with no overrides.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the response language.
    ///
    /// Must be one of [`crate::SUPPORTED_LANGUAGES`]. Validated when the
    /// call is made — invalid values surface as
    /// [`crate::Error::InvalidArgument`], no request is sent.
    pub fn with_lang(mut self, lang: impl Into<String>) -> Self {
        self.lang = Some(lang.into());
        self
    }

    /// Restrict the response to a specific set of fields.
    ///
    /// Include `"success"` if you rely on the success flag for error
    /// checking — when `fields` is set, the API only returns the fields
    /// you ask for.
    pub fn with_fields<I, S>(mut self, fields: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.fields = Some(fields.into_iter().map(Into::into).collect());
        self
    }

    /// Toggle the threat-detection (`security`) block in responses.
    /// Requires the Business plan or above.
    pub fn with_security(mut self, enabled: bool) -> Self {
        self.security = Some(enabled);
        self
    }

    /// Toggle the `rate` block in responses (`limit`, `remaining`).
    /// Requires the Basic plan or above.
    pub fn with_rate(mut self, enabled: bool) -> Self {
        self.rate = Some(enabled);
        self
    }

    /// Merge `other` on top of `self` — every field set on `other` wins,
    /// otherwise `self`'s value is kept.
    pub(crate) fn merged_with(&self, other: &Options) -> Options {
        Options {
            lang: other.lang.clone().or_else(|| self.lang.clone()),
            fields: other.fields.clone().or_else(|| self.fields.clone()),
            security: other.security.or(self.security),
            rate: other.rate.or(self.rate),
        }
    }
}