///|
fn append_if_some(out : String, prefix : String, value : String?) -> String {
  match value {
    Some(text) => out + prefix + text
    None => out
  }
}

///|
fn joined_lines(lines : Array[String]) -> String {
  let mut out = ""
  for index, line in lines {
    if index > 0 {
      out = out + "\n"
    }
    out = out + line
  }
  out
}

///|
fn unit_suffix(unit : String?) -> String {
  match unit {
    Some(text) => " " + text
    None => ""
  }
}

///|
fn escape_json(text : String) -> String {
  let mut out = ""
  for char in text {
    if char == '"' {
      out = out + "\\\""
    } else if char == '\\' {
      out = out + "\\\\"
    } else if char == '\n' {
      out = out + "\\n"
    } else {
      out = out + char.to_string()
    }
  }
  out
}

///|
fn json_string(text : String) -> String {
  "\"" + escape_json(text) + "\""
}

///|
fn json_option(value : String?) -> String {
  match value {
    Some(text) => json_string(text)
    None => "null"
  }
}

///|
fn json_array(items : Array[String]) -> String {
  "[" + items.join(",") + "]"
}

///|
fn escape_html(text : String) -> String {
  let mut out = ""
  for char in text {
    if char == '&' {
      out = out + "&"
    } else if char == '<' {
      out = out + "<"
    } else if char == '>' {
      out = out + ">"
    } else if char == '"' {
      out = out + """
    } else {
      out = out + char.to_string()
    }
  }
  out
}

///|
fn severity_name(severity : WarningSeverity) -> String {
  match severity {
    Info => "info"
    Warning => "warning"
    Critical => "critical"
  }
}

///|
fn result_status_name(status : ResultStatus) -> String {
  match status {
    Normal => "normal"
    Flagged => "flagged"
    Estimated => "estimated"
  }
}

///|
fn section_kind_name(kind : SectionKind) -> String {
  match kind {
    Context => "context"
    Method => "method"
    Safety => "safety"
    Notes => "notes"
  }
}

///|
fn source_kind_name(kind : SourceKind) -> String {
  match kind {
    Standard => "standard"
    Datasheet => "datasheet"
    Literature => "literature"
    Internal => "internal"
  }
}