RFC 9309 robots.txt parser, matcher, serializer and audit toolkit for MoonBit.
robots.txt -> parse -> groups/rules -> product-token selection -> path matching -> access decisionlet text = "User-agent: *\nDisallow: /private/\nAllow: /private/public/\n"
match parse_robots(text) {
Ok(file) => {
let decision = evaluate(file, "ExampleBot", "/private/public/page").unwrap()
println(decision.summary())
}
Err(e) => println(e.to_display())
}robots-tool check --agent ExampleBot --path /private/page --text "User-agent: *\nDisallow: /private/\n"pub struct AccessDecision {
allowed : Bool
product_token : String
uri_path : String
normalized_path : String
matched_rule : RobotsRule?
matched_length : Int
matched_groups : Int
reason : String
} derive(Eq, Debug)pub struct AuditIssue {
severity : AuditSeverity
kind : AuditKind
line : Int
message : String
} derive(Eq, Debug)pub(all) enum RobotsErrorKind {
EmptyInput
MissingColon
EmptyUserAgent
InvalidUserAgent
InvalidRecordName
InvalidPattern
InvalidPercentEncoding
InvalidUtf8
LimitExceeded
RuleBeforeFirstGroup
ControlCharacter
} derive(Eq)pub struct RobotsFile {
groups : Array[RobotsGroup]
other_records : Array[OtherRecord]
comments : Int
blank_lines : Int
source_lines : Int
} derive(Eq, Debug)pub struct RobotsGroup {
user_agents : Array[String]
rules : Array[RobotsRule]
start_line : Int
end_line : Int
} derive(Eq, Debug)fn access_decision(allowed : Bool, product_token : String, uri_path : String, normalized_path : String, matched_rule : RobotsRule?, matched_length : Int, matched_groups : Int, reason : String) -> AccessDecisionfn evaluate(file : RobotsFile, product_token : String, uri_path : String) -> Result[AccessDecision, RobotsError]fn is_allowed(file : RobotsFile, product_token : String, uri_path : String) -> Result[AccessDecision, RobotsError]fn robots_error(stage : RobotsErrorStage, kind : RobotsErrorKind, line : Int, byte_offset : Int, context : String) -> RobotsErrorfn robots_file(groups : Array[RobotsGroup], other_records : Array[OtherRecord], comments : Int, blank_lines : Int, source_lines : Int) -> RobotsFilefn robots_group(user_agents : Array[String], rules : Array[RobotsRule], start_line : Int, end_line : Int) -> RobotsGroupRFC 9309 robots.txt parser, matcher, serializer and audit toolkit for MoonBit.