///|
/// COSE algorithm identifier. Known algorithms use their RFC 9053 integer
/// values on the wire (`ES256` = -7, `EdDSA` = -8, `HMAC_256_256` = 5,
/// `A128GCM` = 1, `A256GCM` = 3). Text names are accepted on decode.
pub(all) enum CoseAlg {
  ES256
  EdDSA
  HMAC_256_64
  HMAC_256_256
  HMAC_384_384
  HMAC_512_512
  AesMac128_64
  AesMac256_64
  AesMac128_128
  AesMac256_128
  A128GCM
  A192GCM
  A256GCM
  ChaCha20Poly1305
  Direct
  DirectHkdfSha256
  DirectHkdfSha512
  DirectHkdfAes128
  DirectHkdfAes256
  A128KW
  A192KW
  A256KW
  EcdhEsHkdf256
  EcdhEsHkdf512
  EcdhSsHkdf256
  EcdhSsHkdf512
  EcdhEsA128Kw
  EcdhEsA192Kw
  EcdhEsA256Kw
  EcdhSsA128Kw
  EcdhSsA192Kw
  EcdhSsA256Kw
  RsaesOaepSha1
  RsaesOaepSha256
  RsaesOaepSha512
  AesCcm16_64_128
  AesCcm16_64_256
  AesCcm64_64_128
  AesCcm64_64_256
  AesCcm16_128_128
  AesCcm16_128_256
  AesCcm64_128_128
  AesCcm64_128_256
  Int(Int32)
  Text(String)
} derive(Eq)

///|
pub extend CoseAlg with Eq::{equal}

///|
#deprecated("Use `!=` instead", skip_current_package=true)
#doc(hidden)
pub extend CoseAlg with Eq::{not_equal}

///|
/// Map integer and text aliases onto the named algorithm variants.
pub fn CoseAlg::normalize(self : CoseAlg) -> CoseAlg {
  match self {
    CoseAlg::Int(-7) | CoseAlg::Text("ES256") => CoseAlg::ES256
    CoseAlg::Int(-8) | CoseAlg::Text("EdDSA") => CoseAlg::EdDSA
    CoseAlg::Int(4) | CoseAlg::Text("HS256/64") => CoseAlg::HMAC_256_64
    CoseAlg::Int(5) | CoseAlg::Text("HS256") => CoseAlg::HMAC_256_256
    CoseAlg::Int(6) | CoseAlg::Text("HS384") => CoseAlg::HMAC_384_384
    CoseAlg::Int(7) | CoseAlg::Text("HS512") => CoseAlg::HMAC_512_512
    CoseAlg::Int(14)
    | CoseAlg::Text("AES-MAC-128/64")
    | CoseAlg::Text("AES-CBC-MAC-128/64") => CoseAlg::AesMac128_64
    CoseAlg::Int(15)
    | CoseAlg::Text("AES-MAC-256/64")
    | CoseAlg::Text("AES-CBC-MAC-256/64") => CoseAlg::AesMac256_64
    CoseAlg::Int(25)
    | CoseAlg::Text("AES-MAC-128/128")
    | CoseAlg::Text("AES-CBC-MAC-128/128") => CoseAlg::AesMac128_128
    CoseAlg::Int(26)
    | CoseAlg::Text("AES-MAC-256/128")
    | CoseAlg::Text("AES-CBC-MAC-256/128") => CoseAlg::AesMac256_128
    CoseAlg::Int(1) | CoseAlg::Text("A128GCM") => CoseAlg::A128GCM
    CoseAlg::Int(2) | CoseAlg::Text("A192GCM") => CoseAlg::A192GCM
    CoseAlg::Int(3) | CoseAlg::Text("A256GCM") => CoseAlg::A256GCM
    CoseAlg::Int(24)
    | CoseAlg::Text("ChaCha20/Poly1305")
    | CoseAlg::Text("ChaCha-Poly1305") => CoseAlg::ChaCha20Poly1305
    CoseAlg::Int(-6) | CoseAlg::Text("direct") => CoseAlg::Direct
    CoseAlg::Int(-10) | CoseAlg::Text("direct+HKDF-SHA-256") =>
      CoseAlg::DirectHkdfSha256
    CoseAlg::Int(-11) | CoseAlg::Text("direct+HKDF-SHA-512") =>
      CoseAlg::DirectHkdfSha512
    CoseAlg::Int(-12) | CoseAlg::Text("direct+HKDF-AES-128") =>
      CoseAlg::DirectHkdfAes128
    CoseAlg::Int(-13) | CoseAlg::Text("direct+HKDF-AES-256") =>
      CoseAlg::DirectHkdfAes256
    CoseAlg::Int(-3) | CoseAlg::Text("A128KW") => CoseAlg::A128KW
    CoseAlg::Int(-4) | CoseAlg::Text("A192KW") => CoseAlg::A192KW
    CoseAlg::Int(-5) | CoseAlg::Text("A256KW") => CoseAlg::A256KW
    CoseAlg::Int(-25) | CoseAlg::Text("ECDH-ES+HKDF-256") =>
      CoseAlg::EcdhEsHkdf256
    CoseAlg::Int(-26) | CoseAlg::Text("ECDH-ES+HKDF-512") =>
      CoseAlg::EcdhEsHkdf512
    CoseAlg::Int(-27) | CoseAlg::Text("ECDH-SS+HKDF-256") =>
      CoseAlg::EcdhSsHkdf256
    CoseAlg::Int(-28) | CoseAlg::Text("ECDH-SS+HKDF-512") =>
      CoseAlg::EcdhSsHkdf512
    CoseAlg::Int(-29) | CoseAlg::Text("ECDH-ES+A128KW") => CoseAlg::EcdhEsA128Kw
    CoseAlg::Int(-30) | CoseAlg::Text("ECDH-ES+A192KW") => CoseAlg::EcdhEsA192Kw
    CoseAlg::Int(-31) | CoseAlg::Text("ECDH-ES+A256KW") => CoseAlg::EcdhEsA256Kw
    CoseAlg::Int(-32) | CoseAlg::Text("ECDH-SS+A128KW") => CoseAlg::EcdhSsA128Kw
    CoseAlg::Int(-33) | CoseAlg::Text("ECDH-SS+A192KW") => CoseAlg::EcdhSsA192Kw
    CoseAlg::Int(-34) | CoseAlg::Text("ECDH-SS+A256KW") => CoseAlg::EcdhSsA256Kw
    CoseAlg::Int(-40)
    | CoseAlg::Text("RSAES-OAEP")
    | CoseAlg::Text("RSAES-OAEP-SHA-1") => CoseAlg::RsaesOaepSha1
    CoseAlg::Int(-41) | CoseAlg::Text("RSAES-OAEP-SHA-256") =>
      CoseAlg::RsaesOaepSha256
    CoseAlg::Int(-42) | CoseAlg::Text("RSAES-OAEP-SHA-512") =>
      CoseAlg::RsaesOaepSha512
    CoseAlg::Int(10)
    | CoseAlg::Text("AES-CCM-16-64-128")
    | CoseAlg::Text("AES-CCM-16-128/64") => CoseAlg::AesCcm16_64_128
    CoseAlg::Int(11)
    | CoseAlg::Text("AES-CCM-16-64-256")
    | CoseAlg::Text("AES-CCM-16-256/64") => CoseAlg::AesCcm16_64_256
    CoseAlg::Int(12)
    | CoseAlg::Text("AES-CCM-64-64-128")
    | CoseAlg::Text("AES-CCM-64-128/64") => CoseAlg::AesCcm64_64_128
    CoseAlg::Int(13)
    | CoseAlg::Text("AES-CCM-64-64-256")
    | CoseAlg::Text("AES-CCM-64-256/64") => CoseAlg::AesCcm64_64_256
    CoseAlg::Int(30)
    | CoseAlg::Text("AES-CCM-16-128-128")
    | CoseAlg::Text("AES-CCM-16-128/128") => CoseAlg::AesCcm16_128_128
    CoseAlg::Int(31)
    | CoseAlg::Text("AES-CCM-16-128-256")
    | CoseAlg::Text("AES-CCM-16-256/128") => CoseAlg::AesCcm16_128_256
    CoseAlg::Int(32)
    | CoseAlg::Text("AES-CCM-64-128-128")
    | CoseAlg::Text("AES-CCM-64-128/128") => CoseAlg::AesCcm64_128_128
    CoseAlg::Int(33)
    | CoseAlg::Text("AES-CCM-64-128-256")
    | CoseAlg::Text("AES-CCM-64-256/128") => CoseAlg::AesCcm64_128_256
    other => other
  }
}

///|
/// Encode the algorithm as its RFC 9053 integer, or as text if it has no integer form.
pub fn CoseAlg::to_cbor(self : CoseAlg) -> @cbor.CborValue {
  match self.normalize() {
    CoseAlg::ES256 => @cbor.Integer(-7L)
    CoseAlg::EdDSA => @cbor.Integer(-8L)
    CoseAlg::HMAC_256_64 => @cbor.Integer(4L)
    CoseAlg::HMAC_256_256 => @cbor.Integer(5L)
    CoseAlg::HMAC_384_384 => @cbor.Integer(6L)
    CoseAlg::HMAC_512_512 => @cbor.Integer(7L)
    CoseAlg::AesMac128_64 => @cbor.Integer(14L)
    CoseAlg::AesMac256_64 => @cbor.Integer(15L)
    CoseAlg::AesMac128_128 => @cbor.Integer(25L)
    CoseAlg::AesMac256_128 => @cbor.Integer(26L)
    CoseAlg::A128GCM => @cbor.Integer(1L)
    CoseAlg::A192GCM => @cbor.Integer(2L)
    CoseAlg::A256GCM => @cbor.Integer(3L)
    CoseAlg::ChaCha20Poly1305 => @cbor.Integer(24L)
    CoseAlg::Direct => @cbor.Integer(-6L)
    CoseAlg::DirectHkdfSha256 => @cbor.Integer(-10L)
    CoseAlg::DirectHkdfSha512 => @cbor.Integer(-11L)
    CoseAlg::DirectHkdfAes128 => @cbor.Integer(-12L)
    CoseAlg::DirectHkdfAes256 => @cbor.Integer(-13L)
    CoseAlg::A128KW => @cbor.Integer(-3L)
    CoseAlg::A192KW => @cbor.Integer(-4L)
    CoseAlg::A256KW => @cbor.Integer(-5L)
    CoseAlg::EcdhEsHkdf256 => @cbor.Integer(-25L)
    CoseAlg::EcdhEsHkdf512 => @cbor.Integer(-26L)
    CoseAlg::EcdhSsHkdf256 => @cbor.Integer(-27L)
    CoseAlg::EcdhSsHkdf512 => @cbor.Integer(-28L)
    CoseAlg::EcdhEsA128Kw => @cbor.Integer(-29L)
    CoseAlg::EcdhEsA192Kw => @cbor.Integer(-30L)
    CoseAlg::EcdhEsA256Kw => @cbor.Integer(-31L)
    CoseAlg::EcdhSsA128Kw => @cbor.Integer(-32L)
    CoseAlg::EcdhSsA192Kw => @cbor.Integer(-33L)
    CoseAlg::EcdhSsA256Kw => @cbor.Integer(-34L)
    CoseAlg::RsaesOaepSha1 => @cbor.Integer(-40L)
    CoseAlg::RsaesOaepSha256 => @cbor.Integer(-41L)
    CoseAlg::RsaesOaepSha512 => @cbor.Integer(-42L)
    CoseAlg::AesCcm16_64_128 => @cbor.Integer(10L)
    CoseAlg::AesCcm16_64_256 => @cbor.Integer(11L)
    CoseAlg::AesCcm64_64_128 => @cbor.Integer(12L)
    CoseAlg::AesCcm64_64_256 => @cbor.Integer(13L)
    CoseAlg::AesCcm16_128_128 => @cbor.Integer(30L)
    CoseAlg::AesCcm16_128_256 => @cbor.Integer(31L)
    CoseAlg::AesCcm64_128_128 => @cbor.Integer(32L)
    CoseAlg::AesCcm64_128_256 => @cbor.Integer(33L)
    CoseAlg::Int(n) => @cbor.Integer(n.to_int64())
    CoseAlg::Text(s) => @cbor.Text(s)
  }
}

///|
/// Read an algorithm from a CBOR int or text string.
pub fn CoseAlg::from_cbor(value : @cbor.CborValue) -> CoseAlg raise CoseError {
  match cbor_unwrap(value) {
    @cbor.Integer(n) => CoseAlg::Int(int_from_i64(n)).normalize()
    @cbor.Unsigned(n) =>
      if n > 2147483647UL {
        raise BadStructure("algorithm integer is too large")
      } else {
        CoseAlg::Int(n.to_int()).normalize()
      }
    @cbor.Text(s) => CoseAlg::Text(s).normalize()
    _ => raise BadStructure("algorithm must be an int or a text string")
  }
}

///|
/// Short name used in error messages.
pub fn CoseAlg::name(self : CoseAlg) -> String {
  match self.normalize() {
    CoseAlg::ES256 => "ES256"
    CoseAlg::EdDSA => "EdDSA"
    CoseAlg::HMAC_256_64 => "HS256/64"
    CoseAlg::HMAC_256_256 => "HS256"
    CoseAlg::HMAC_384_384 => "HS384"
    CoseAlg::HMAC_512_512 => "HS512"
    CoseAlg::AesMac128_64 => "AES-MAC-128/64"
    CoseAlg::AesMac256_64 => "AES-MAC-256/64"
    CoseAlg::AesMac128_128 => "AES-MAC-128/128"
    CoseAlg::AesMac256_128 => "AES-MAC-256/128"
    CoseAlg::A128GCM => "A128GCM"
    CoseAlg::A192GCM => "A192GCM"
    CoseAlg::A256GCM => "A256GCM"
    CoseAlg::ChaCha20Poly1305 => "ChaCha20/Poly1305"
    CoseAlg::Direct => "direct"
    CoseAlg::DirectHkdfSha256 => "direct+HKDF-SHA-256"
    CoseAlg::DirectHkdfSha512 => "direct+HKDF-SHA-512"
    CoseAlg::DirectHkdfAes128 => "direct+HKDF-AES-128"
    CoseAlg::DirectHkdfAes256 => "direct+HKDF-AES-256"
    CoseAlg::A128KW => "A128KW"
    CoseAlg::A192KW => "A192KW"
    CoseAlg::A256KW => "A256KW"
    CoseAlg::EcdhEsHkdf256 => "ECDH-ES+HKDF-256"
    CoseAlg::EcdhEsHkdf512 => "ECDH-ES+HKDF-512"
    CoseAlg::EcdhSsHkdf256 => "ECDH-SS+HKDF-256"
    CoseAlg::EcdhSsHkdf512 => "ECDH-SS+HKDF-512"
    CoseAlg::EcdhEsA128Kw => "ECDH-ES+A128KW"
    CoseAlg::EcdhEsA192Kw => "ECDH-ES+A192KW"
    CoseAlg::EcdhEsA256Kw => "ECDH-ES+A256KW"
    CoseAlg::EcdhSsA128Kw => "ECDH-SS+A128KW"
    CoseAlg::EcdhSsA192Kw => "ECDH-SS+A192KW"
    CoseAlg::EcdhSsA256Kw => "ECDH-SS+A256KW"
    CoseAlg::RsaesOaepSha1 => "RSAES-OAEP"
    CoseAlg::RsaesOaepSha256 => "RSAES-OAEP-SHA-256"
    CoseAlg::RsaesOaepSha512 => "RSAES-OAEP-SHA-512"
    CoseAlg::AesCcm16_64_128 => "AES-CCM-16-128/64"
    CoseAlg::AesCcm16_64_256 => "AES-CCM-16-256/64"
    CoseAlg::AesCcm64_64_128 => "AES-CCM-64-128/64"
    CoseAlg::AesCcm64_64_256 => "AES-CCM-64-256/64"
    CoseAlg::AesCcm16_128_128 => "AES-CCM-16-128/128"
    CoseAlg::AesCcm16_128_256 => "AES-CCM-16-256/128"
    CoseAlg::AesCcm64_128_128 => "AES-CCM-64-128/128"
    CoseAlg::AesCcm64_128_256 => "AES-CCM-64-256/128"
    CoseAlg::Int(n) => "int(\{n})"
    CoseAlg::Text(s) => s
  }
}

///|
/// Header map label: a CBOR int or text string (RFC 9052).
pub(all) enum Label {
  Int(Int32)
  Text(String)
} derive(Eq)

///|
pub extend Label with Eq::{equal}

///|
#deprecated("Use `!=` instead", skip_current_package=true)
#doc(hidden)
pub extend Label with Eq::{not_equal}

///|
/// Encode a header label as a CBOR int or text string.
pub fn Label::to_cbor(self : Label) -> @cbor.CborValue {
  match self {
    Label::Int(n) => @cbor.Integer(n.to_int64())
    Label::Text(s) => @cbor.Text(s)
  }
}

///|
/// Read a header label from a CBOR int or text string.
pub fn Label::from_cbor(value : @cbor.CborValue) -> Label raise CoseError {
  match cbor_unwrap(value) {
    @cbor.Integer(n) => Label::Int(int_from_i64(n))
    @cbor.Unsigned(n) =>
      if n > 2147483647UL {
        raise BadStructure("header label integer is too large")
      } else {
        Label::Int(n.to_int())
      }
    @cbor.Text(s) => Label::Text(s)
    _ => raise BadStructure("header label must be an int or a text string")
  }
}

///|
fn label_is_known(label : Label) -> Bool {
  match label {
    Label::Int(1)
    | Label::Int(2)
    | Label::Int(3)
    | Label::Int(4)
    | Label::Int(5)
    | Label::Int(6)
    | Label::Int(-1)
    | Label::Int(-2)
    | Label::Int(-3)
    | Label::Int(-20)
    | Label::Int(-21)
    | Label::Int(-22)
    | Label::Int(-23)
    | Label::Int(-24)
    | Label::Int(-25)
    | Label::Int(-26) => true
    Label::Int(_) | Label::Text(_) => false
  }
}

///|
/// Content type header (`label` 3): an integer or a text media type.
pub(all) enum ContentType {
  Int(Int32)
  Text(String)
} derive(Eq)

///|
pub extend ContentType with Eq::{equal}

///|
#deprecated("Use `!=` instead", skip_current_package=true)
#doc(hidden)
pub extend ContentType with Eq::{not_equal}

///|
fn ContentType::to_cbor(self : ContentType) -> @cbor.CborValue {
  match self {
    ContentType::Int(n) => @cbor.Integer(n.to_int64())
    ContentType::Text(s) => @cbor.Text(s)
  }
}

///|
fn ContentType::from_cbor(
  value : @cbor.CborValue,
) -> ContentType raise CoseError {
  match cbor_unwrap(value) {
    @cbor.Integer(n) => ContentType::Int(int_from_i64(n))
    @cbor.Unsigned(n) =>
      if n > 2147483647UL {
        raise BadStructure("content type integer is too large")
      } else {
        ContentType::Int(n.to_int())
      }
    @cbor.Text(s) => ContentType::Text(s)
    _ => raise BadStructure("content type must be an int or a text string")
  }
}