// JSON output helpers for robots-tool.
///|
fn q(s : String) -> String {
"\"" + @robots.json_string(s) + "\""
}
///|
fn bool_json(v : Bool) -> String {
if v {
"true"
} else {
"false"
}
}
///|
fn error_json(e : @robots.RobotsError) -> String {
"{\"ok\":false,\"error\":{\"stage\":\{q(e.stage().to_string())},\"kind\":\{q(e.kind().to_string())},\"line\":\{e.line()},\"offset\":\{e.byte_offset()},\"context\":\{q(e.context())}}}"
}
///|
fn parse_json(file : @robots.RobotsFile) -> String {
"{\"ok\":true,\"groups\":\{file.group_count()},\"rules\":\{file.rule_count()},\"other_records\":\{file.other_record_count()}}"
}
///|
fn check_json(decision : @robots.AccessDecision) -> String {
match decision.matched_rule() {
None =>
"{\"allowed\":\{bool_json(decision.allowed())},\"matched\":null,\"rule\":null,\"matched_length\":0}"
Some(rule) =>
"{\"allowed\":\{bool_json(decision.allowed())},\"matched\":\{q(rule.pattern())},\"rule\":\{q(rule.kind().json_name())},\"matched_length\":\{decision.matched_length()}}"
}
}
///|
fn stats_json(file : @robots.RobotsFile) -> String {
let s = file.stats()
"{\"ok\":true,\"groups\":\{s.groups()},\"user_agents\":\{s.user_agents()},\"rules\":\{s.rules()},\"allows\":\{s.allows()},\"disallows\":\{s.disallows()},\"other_records\":\{s.other_records()},\"comments\":\{s.comments()},\"blank_lines\":\{s.blank_lines()},\"source_lines\":\{s.source_lines()}}"
}
///|
fn audit_json(issues : Array[@robots.AuditIssue]) -> String {
let sb = StringBuilder::new()
sb.write_string("{\"ok\":true,\"issues\":[")
for i, issue in issues {
if i > 0 {
sb.write_string(",")
}
sb.write_string("{\"severity\":")
sb.write_string(q(issue.severity().label()))
sb.write_string(",\"kind\":")
sb.write_string(q(issue.kind().label()))
sb.write_string(",\"line\":")
sb.write_string("\{issue.line()}")
sb.write_string(",\"message\":")
sb.write_string(q(issue.message()))
sb.write_string("}")
}
sb.write_string("]}")
sb.to_string()
}