View Source Handle.Domain.Rule (Handle.Domain v0.1.0)

Provides functionality for parsing and matching domain rules defined by the Public Suffix List and as described at https://github.com/publicsuffix/list/wiki/Format#format

This module defines a struct representing a domain rule with its type, length, parts, and associated domain type (public, private, or test). It provides functions to parse string representations of rules and to match domain parts against these rules.

Parsing Rules

The parse/2 function processes a string rule and a domain type to produce a rule struct or special values:

  • Returns nil for empty or comment lines.
  • Returns :begin_private_domains for the specific marker line.
  • Parses rules starting with "!" as exceptions.
  • Parses rules starting with "*." as wildcards.
  • All other rules are parsed as standard.

Matching

The match?/2 function checks if a given list of domain parts starts with the parts of the rule, determining if the rule matches the domain.

Examples

iex> rule = Handle.Domain.Rule.parse("*.example.com", :public)
iex> Handle.Domain.Rule.match?(rule, ["com", "example", "subdomain"])
true

Summary

Functions

Checks if a given list of domain parts matches the rule.

Parses a rule line defined in the Public Suffix List format and returns a rule struct.

Types

domain_type()

@type domain_type() :: :public | :private | :test

t()

@type t() :: %Handle.Domain.Rule{
  domain_type: domain_type(),
  length: integer() | nil,
  parts: [String.t()] | nil,
  rule_type: :standard | :wildcard | :exception
}

Functions

match?(rule, domain_parts)

@spec match?(t(), [String.t()]) :: boolean()

Checks if a given list of domain parts matches the rule.

Examples

iex> rule = Handle.Domain.Rule.parse("*.example.com", :public)
iex> Handle.Domain.Rule.match?(rule, ["com", "example", "subdomain"])
true
iex> Handle.Domain.Rule.match?(rule, ["org", "example", "subdomain"])
false

parse(rule, domain_type)

@spec parse(binary(), domain_type()) :: :begin_private_domains | nil | t()

Parses a rule line defined in the Public Suffix List format and returns a rule struct.

The function takes a rule line and a domain type (:public, :private, or :test) and returns:

  • nil for empty or comment lines.
  • :begin_private_domains for the specific marker line when transitioning to the private domain list.
  • A rule struct for standard, wildcard, or exception rules.