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(url: "https://github.com/Xenoglyphiq/robotstxt-swift", from: "0.1.0"),
// in your targetdependencies: [.product(name: "RobotsTxt", package: "robotstxt-swift"), .product(name: "RobotsTxtIO", package: "robotstxt-swift")]zig fetch --save git+https://github.com/Xenoglyphiq/robotstxt-zig#v0.1.0const robotstxt = b.dependency("robotstxt", .{ .target = target, .optimize = optimize });exe.root_module.addImport("robotstxt", robotstxt.module("robotstxt"));exe.root_module.addImport("robotstxt_io", robotstxt.module("robotstxt_io"));using PkgPkg.add("RobotsTxt")Examples
The same three examples exist in every port, runnable as-is from each repository's examples/ folder.
Check one path
// 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)")}//! Canonical example `check_path`: parse a sample robots.txt and print//! whether `NowhereToBeBot` may fetch `/private/page`.const std = @import("std");const robotstxt = @import("robotstxt");
const sample = \\# Sample robots.txt \\User-agent: * \\Disallow: /private/ \\Allow: /private/press-kit \\Crawl-delay: 2 \\ \\User-agent: NowhereToBeBot \\User-agent: OtherBot/1.0 \\Disallow: /private/ \\Disallow: /drafts/*.html$ \\Allow: /drafts/public/ \\ \\Sitemap: https://www.example.com/sitemap.xml \\Sitemap: https://www.example.com/news/sitemap.xml \\;
pub fn main(init: std.process.Init) !void { const gpa = init.gpa; var robots = try robotstxt.parse(gpa, sample, .{}); defer robots.deinit();
var diag: robotstxt.Diagnostics = .{}; const allowed = robotstxt.isAllowed(gpa, &robots, "NowhereToBeBot", "/private/page", &diag) catch |err| { std.debug.print("{s}: {s}\n", .{ @errorName(err), diag.code }); return err; }; std.debug.print("NowhereToBeBot {s} fetch /private/page\n", .{if (allowed) "may" else "may not"});}# Canonical example `check_path`: parse a sample robots.txt and print whether# NowhereToBeBot may fetch /private/page.using RobotsTxt
const SAMPLE = """User-agent: *Disallow: /private/Allow: /private/press-kit
User-agent: NowhereToBeBotDisallow: /private/Crawl-delay: 5
Sitemap: https://example.com/sitemap.xmlSitemap: https://example.com/news/sitemap.xml"""
robots = RobotsTxt.parse(SAMPLE)allowed = RobotsTxt.is_allowed(robots, "NowhereToBeBot", "/private/page")println("NowhereToBeBot ", allowed ? "may" : "may not", " fetch /private/page")List the sitemaps
// Canonical example `list_sitemaps`: parse a sample robots.txt and print its sitemap// URLs in file order.// Usage: swift run list_sitemapsimport 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)}//! Canonical example `list_sitemaps`: parse a sample robots.txt and print its//! sitemap URLs in file order.const std = @import("std");const robotstxt = @import("robotstxt");
const sample = \\Sitemap: https://www.example.com/sitemap.xml \\ \\User-agent: * \\Disallow: /private/ \\Sitemap: https://www.example.com/news/sitemap.xml \\ \\User-agent: NowhereToBeBot \\Disallow: / \\ \\# Sitemap lines belong to no group, wherever they appear. \\sitemap: https://cdn.example.com/sitemaps/products.xml \\;
pub fn main(init: std.process.Init) !void { var robots = try robotstxt.parse(init.gpa, sample, .{}); defer robots.deinit(); for (robots.sitemaps) |url| std.debug.print("{s}\n", .{url});}# Canonical example `list_sitemaps`: parse a sample robots.txt and print its# sitemap URLs in file order.using RobotsTxt
const SAMPLE = """Sitemap: https://example.com/sitemap.xml
User-agent: *Disallow: /private/
Sitemap: https://example.com/news/sitemap.xmlSitemap: https://example.com/products/sitemap.xml"""
robots = RobotsTxt.parse(SAMPLE)for url in robots.sitemaps println(url)endExplain a decision
// 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)")}//! Canonical example `explain_decision`: for a few paths, print the rule//! that decides whether `NowhereToBeBot` may fetch it (allow or disallow, the//! pattern and its line), or that no rule applies.const std = @import("std");const robotstxt = @import("robotstxt");
const sample = \\User-agent: * \\Disallow: / \\ \\User-agent: NowhereToBeBot \\Disallow: /private/ \\Allow: /private/press-kit \\Disallow: /*.pdf$ \\Allow: /drafts/public/ \\Disallow: /drafts/ \\;
pub fn main(init: std.process.Init) !void { const gpa = init.gpa; var robots = try robotstxt.parse(gpa, sample, .{}); defer robots.deinit();
const paths = [_][]const u8{ "/private/page", "/private/press-kit/logo.png", "/reports/2026.pdf", "/reports/2026.pdf?download=1", "/drafts/public/notes", "/about", "/robots.txt", }; var diag: robotstxt.Diagnostics = .{}; for (paths) |path| { const rule = robotstxt.matchingRule(gpa, &robots, "NowhereToBeBot", path, &diag) catch |err| { std.debug.print("{s}: {s}\n", .{ @errorName(err), diag.code }); return err; }; if (rule) |r| { std.debug.print("{s}: {s} by line {d}, \"{s}: {s}\"\n", .{ path, if (r.allow) "allowed" else "disallowed", r.line, if (r.allow) "Allow" else "Disallow", r.pattern, }); } else std.debug.print("{s}: allowed, no rule applies\n", .{path}); }}# Canonical example `explain_decision`: print the rule that decides a path,# with its line number, or that no rule applies.using RobotsTxt
const SAMPLE = """User-agent: *Disallow: /private/Allow: /private/press-kitDisallow: /*.pdf\$"""
robots = RobotsTxt.parse(SAMPLE)for path in ("/private/page", "/private/press-kit/logo.png", "/docs/guide.pdf", "/about") rule = try RobotsTxt.matching_rule(robots, "NowhereToBeBot", path) catch e e isa RobotsError && println(stderr, sprint(showerror, e)) rethrow() end if rule === nothing println("$path: allowed, no rule applies") else verdict = rule.allow ? "allowed" : "disallowed" println("$path: $verdict by line $(rule.line): $(rule.allow ? "Allow" : "Disallow"): $(rule.pattern)") endendPorts
| Language | Package | Release | Spec | Conformance |
|---|---|---|---|---|
| Swift | RobotsTxt | 0.1.0 | v0.1.2 | core ✓ io ✓ full ✓ (130/130) |
| Zig | robotstxt | v0.1.0 | v0.1.2 | core ✓ io ✓ full ✓ (130/130) |
| Julia | RobotsTxt | registration pending | v0.1.2 | core ✓ 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×.
| Port | parse + isAllowed pass | parse + is_allowed pass |
|---|---|---|
| Reference | Rust texting_robots 0.2.2: 13.10 ms | Rust texting_robots 0.2.2: 13.10 ms |
| Swift | 24.33 ms (1.86×) | — |
| Zig | 14.26 ms (1.09×) | — |
| Julia | — | 19.60 ms (1.50×) |
Spec and source
- Behavior spec and machine-readable contract
- Conformance cases (130)
- Decisions and benchmark method
- Swift port: Xenoglyphiq/robotstxt-swift
- Zig port: Xenoglyphiq/robotstxt-zig
- Julia port: Xenoglyphiq/RobotsTxt.jl