///|
/// Typed diagnostics raised when compiling Rego v1 modules or queries into HIR
/// and structured Plan IR.
#derive(Eq, Debug)
pub(all) suberror CompileError {
  /// Two imports in the same module bind the same alias name.
  DuplicateImportAlias(alias_name~ : String, span~ : @syntax.SourceSpan)
  /// Import path does not start with `data`, `input`, `rego.v1`, or `future.keywords`,
  /// or uses non-static bracket segments.
  InvalidImport(path~ : String, span~ : @syntax.SourceSpan)
  /// Rules at the same path disagree in kind (complete, partial set, object, or function)
  /// or function arity.
  ConflictingRuleKind(path~ : String, span~ : @syntax.SourceSpan)
  /// Variable is not grounded by any positive expression in scope.
  UnsafeVar(name~ : String, span~ : @syntax.SourceSpan)
  /// Direct or mutual recursion detected among rules or functions.
  CyclicRule(cycle~ : ReadOnlyArray[String], span~ : @syntax.SourceSpan)
  /// Recursion through negation detected among rules.
  UnstratifiedNegation(
    cycle~ : ReadOnlyArray[String],
    span~ : @syntax.SourceSpan,
  )
  /// More than one `default` rule targets the same path.
  DuplicateDefault(path~ : String, span~ : @syntax.SourceSpan)
  /// `default` rule has a non-ground value or conflicts with a partial rule.
  InvalidDefault(message~ : String, span~ : @syntax.SourceSpan)
  /// Rule head structure is invalid in Rego v1 (for example `else` on a partial rule).
  InvalidRuleHead(message~ : String, span~ : @syntax.SourceSpan)
  /// Call targets an unknown function or built-in name.
  UndefinedFunction(name~ : String, span~ : @syntax.SourceSpan)
  /// Call passes the wrong number of arguments.
  WrongArity(
    name~ : String,
    expected~ : Int32,
    actual~ : Int32,
    span~ : @syntax.SourceSpan,
  )
  /// Numeric literal could not be parsed as an exact decimal.
  InvalidNumber(text~ : String, span~ : @syntax.SourceSpan)
  /// Local variable assigned or declared more than once in the same scope.
  DuplicateLocalVar(name~ : String, span~ : @syntax.SourceSpan)
  /// `with` modifier targets an unsupported path or non-callable replacement.
  InvalidWithTarget(target~ : String, span~ : @syntax.SourceSpan)
}

///|
pub impl Show for CompileError with fn output(self, logger) {
  match self {
    DuplicateImportAlias(alias_name~, ..) =>
      logger.write_string("duplicate import alias `\{alias_name}`")
    InvalidImport(path~, ..) =>
      logger.write_string("invalid import path `\{path}`")
    ConflictingRuleKind(path~, ..) =>
      logger.write_string("conflicting rule kind at `\{path}`")
    UnsafeVar(name~, ..) => logger.write_string("var `\{name}` is unsafe")
    CyclicRule(cycle~, ..) => {
      let joined = cycle.iter().collect().join(" -> ")
      logger.write_string("rule recursion detected: \{joined}")
    }
    UnstratifiedNegation(cycle~, ..) => {
      let joined = cycle.iter().collect().join(" -> ")
      logger.write_string("unstratified negation cycle: \{joined}")
    }
    DuplicateDefault(path~, ..) =>
      logger.write_string("duplicate default rule at `\{path}`")
    InvalidDefault(message~, ..) =>
      logger.write_string("invalid default rule: \{message}")
    InvalidRuleHead(message~, ..) =>
      logger.write_string("invalid rule head: \{message}")
    UndefinedFunction(name~, ..) =>
      logger.write_string("undefined function `\{name}`")
    WrongArity(name~, expected~, actual~, ..) =>
      logger.write_string(
        "wrong arity for `\{name}`: expected \{expected}, got \{actual}",
      )
    InvalidNumber(text~, ..) =>
      logger.write_string("invalid number literal `\{text}`")
    DuplicateLocalVar(name~, ..) =>
      logger.write_string("var `\{name}` assigned above")
    InvalidWithTarget(target~, ..) =>
      logger.write_string("invalid `with` target `\{target}`")
  }
}