///|
/// Strongly-typed URL path parameter decoder shared across `unmbt/ciallo/schema`,
/// `unmbt/ciallo/core` (`Context::param_as`), and 4-target pure ID packages like `unmbt/ciallo-id`.
pub(open) trait FromParam {
  fn from_param(name : String, raw : StringView) -> Self raise ValidationError
}

///|
pub impl FromParam for String with fn from_param(_name, raw) -> String raise ValidationError {
  raw.to_owned()
}

///|
pub impl FromParam for Int32 with fn from_param(name, raw) -> Int32 raise ValidationError {
  @string.parse_int(raw) catch {
    _ =>
      raise ValidationError::InvalidInput(issues=[
        {
          path: "/\{name}",
          message: "invalid Int32 path parameter '\{raw}'",
          expected: "Int32",
        },
      ])
  }
}

///|
pub impl FromParam for Int64 with fn from_param(name, raw) -> Int64 raise ValidationError {
  @string.parse_int64(raw) catch {
    _ =>
      raise ValidationError::InvalidInput(issues=[
        {
          path: "/\{name}",
          message: "invalid Int64 path parameter '\{raw}'",
          expected: "Int64",
        },
      ])
  }
}

///|
pub impl FromParam for UInt32 with fn from_param(name, raw) -> UInt32 raise ValidationError {
  @string.parse_uint(raw) catch {
    _ =>
      raise ValidationError::InvalidInput(issues=[
        {
          path: "/\{name}",
          message: "invalid UInt32 path parameter '\{raw}'",
          expected: "UInt32",
        },
      ])
  }
}

///|
pub impl FromParam for UInt64 with fn from_param(name, raw) -> UInt64 raise ValidationError {
  @string.parse_uint64(raw) catch {
    _ =>
      raise ValidationError::InvalidInput(issues=[
        {
          path: "/\{name}",
          message: "invalid UInt64 path parameter '\{raw}'",
          expected: "UInt64",
        },
      ])
  }
}

///|
pub impl FromParam for Bool with fn from_param(name, raw) -> Bool raise ValidationError {
  match raw.to_owned() {
    "true" | "1" => true
    "false" | "0" => false
    _ =>
      raise ValidationError::InvalidInput(issues=[
        {
          path: "/\{name}",
          message: "invalid Bool path parameter '\{raw}'",
          expected: "Bool",
        },
      ])
  }
}