Skip to content

robots.txt

Parse robots.txt files and decide whether a crawler may fetch a path, per RFC 9309 (the Robots Exclusion Protocol).

Implements RFC 9309 (Robots Exclusion Protocol). Spec v0.1.2; every port passes the same 130 conformance cases.

Swift 0.1.0Zig v0.1.0Julia registration pending

Install

Package.swift
.package(url: "https://github.com/Xenoglyphiq/robotstxt-swift", from: "0.1.0"),
// in your target
dependencies: [.product(name: "RobotsTxt", package: "robotstxt-swift"), .product(name: "RobotsTxtIO", package: "robotstxt-swift")]

Examples

The same three examples exist in every port, runnable as-is from each repository's examples/ folder.

Check one path

examples/check_path.swift
// Canonical example `check_path`: parse a sample robots.txt and print whether
// NowhereToBeBot may fetch /private/page.
// Usage: swift run check_path [path]
import RobotsTxt
let sample = """
# Sample robots.txt
User-agent: *
Disallow: /search
Allow: /private/press
User-agent: NowhereToBeBot
Disallow: /private/
Allow: /private/press/
Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/news/sitemap.xml
"""
let path = CommandLine.arguments.dropFirst().first ?? "/private/page"
let robots = parse(sample)
do {
let allowed = try isAllowed(robots, userAgent: "NowhereToBeBot", path: path)
print("NowhereToBeBot \(allowed ? "may" : "may not") fetch \(path)")
} catch {
print("error: \(error)")
}

List the sitemaps

examples/list_sitemaps.swift
// Canonical example `list_sitemaps`: parse a sample robots.txt and print its sitemap
// URLs in file order.
// Usage: swift run list_sitemaps
import RobotsTxt
let sample = """
# Sample robots.txt
Sitemap: https://example.com/sitemap.xml
User-agent: *
Disallow: /search
Sitemap: https://example.com/news/sitemap.xml
User-agent: NowhereToBeBot
Disallow: /private/
Sitemap: https://example.com/archive/sitemap.xml
"""
for url in parse(sample).sitemaps {
print(url)
}

Explain a decision

examples/explain_decision.swift
// Canonical example `explain_decision`: print the rule (allow or disallow, pattern,
// line) that decides a path for a crawler, or that no rule does.
// Usage: swift run explain_decision [path] [user-agent]
import RobotsTxt
let sample = """
# Sample robots.txt
User-agent: *
Disallow: /search
User-agent: NowhereToBeBot
Disallow: /private/
Allow: /private/press/
Disallow: /*.pdf$
"""
let args = Array(CommandLine.arguments.dropFirst())
let path = args.first ?? "/private/press/2026.html"
let agent = args.count > 1 ? args[1] : "NowhereToBeBot"
let robots = parse(sample)
do {
if let rule = try matchingRule(robots, userAgent: agent, path: path) {
print("\(agent) \(path): \(rule.allow ? "allow" : "disallow") \(rule.pattern) (line \(rule.line))")
} else {
print("\(agent) \(path): no rule applies, so it is allowed")
}
} catch {
print("error: \(error)")
}

Ports

LanguagePackageReleaseSpecConformance
SwiftRobotsTxt0.1.0v0.1.2core ✓ io ✓ full ✓ (130/130)
Zigrobotstxtv0.1.0v0.1.2core ✓ io ✓ full ✓ (130/130)
JuliaRobotsTxtregistration pendingv0.1.2core ✓ io ✓ full ✓ (130/130)

Performance

Median time, and ratio to the reference implementation measured in the same session. The spec's target is within 2×.

Portparse + isAllowed passparse + is_allowed pass
ReferenceRust texting_robots 0.2.2: 13.10 msRust texting_robots 0.2.2: 13.10 ms
Swift24.33 ms (1.86×)—
Zig14.26 ms (1.09×)—
Julia—19.60 ms (1.50×)

Spec and source