use tramlines_md::grammar::{FormatRegistry, Grammar, RegexCache, lower, validate_schema};
use super::SokfSchema;
use crate::{Finding, RepoPath, Spanned, markdown::SOKF};
pub const SOKF_TYPES: [(&str, &str); 2] = [
(
"store",
"(?:(?:[a-z0-9]+(?:-[a-z0-9]+)*/)?[a-z0-9]+(?:-[a-z0-9]+)*:)?",
),
(
"ref",
"(?:(?:[a-z0-9]+(?:-[a-z0-9]+)*/)?[a-z0-9]+(?:-[a-z0-9]+)*:)?(?:[a-z0-9]+(?:-[a-z0-9]+)*(?:#[A-Za-z0-9_-]*)?|#[A-Za-z0-9_-]+)",
),
];
pub fn formats_text() -> &'static str {
include_str!(concat!(
env!("OUT_DIR"),
"/knowledge/formats/sokf.formats.yaml"
))
}
pub fn embedded_formats(grammar: &Grammar, regexes: &RegexCache) -> crate::Result<FormatRegistry> {
Ok(FormatRegistry::from_text(
formats_text(),
&SOKF_TYPES,
grammar,
regexes,
)?)
}
pub fn compile(
yaml: &Spanned,
grammar: &Grammar,
registry: &FormatRegistry,
regexes: &RegexCache,
path: &RepoPath,
id: &str,
) -> std::result::Result<SokfSchema, Vec<Finding>> {
let problems = validate_schema(yaml, grammar, regexes, path);
if !problems.is_empty() {
return Err(problems);
}
let (core, mut findings) = lower(yaml, grammar, registry, regexes, path, id, &SOKF);
let (schema, more) = SokfSchema::lower(core, yaml, path);
findings.extend(more);
if findings.is_empty() {
Ok(schema)
} else {
Err(findings)
}
}
pub fn extension_text() -> &'static str {
include_str!(concat!(env!("OUT_DIR"), "/sokf-spec/sokf.tramlines.yaml"))
}
pub fn load_grammar() -> crate::Result<Grammar> {
let mut grammar = tramlines_md::grammar::load_grammar()?;
grammar.extend(extension_text())?;
Ok(grammar)
}
pub fn grammar_text() -> String {
format!(
"{}---\n{}---\n{}",
tramlines_md::grammar::core_grammar_text(),
tramlines_md::grammar::grammar_text(),
extension_text()
)
}