Repository: launchbadge/sqlx
File: sqlx-postgres/src/types/int.rs (Rust)

Code before:
        PgTypeInfo::CHAR
    }
}

impl PgHasArrayType for i8 {
    fn array_type_info() -> PgTypeInfo {
        PgTypeInfo::CHAR_ARRAY
    }
}

impl Encode<'_, Postgres> for i8 {
    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
        buf.extend(&self.to_be_bytes());

        IsNull::No
    }
}

impl Decode<'_, Postgres> for i8 {
    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
        // note: in the TEXT encoding, a value of "0" here is encoded as an empty string
        if (value.format() == PgValueFormat::Text) && (value.as_bytes()?.is_empty()) {
            return Ok(Default::default());
        }

        int_decode(value)?.try_into().map_err(Into::into)
    }
}

impl Type<Postgres> for i16 {
    fn type_info() -> PgTypeInfo {
        PgTypeInfo::INT2
    }
}

impl PgHasArrayType for i16 {
    fn array_type_info() -> PgTypeInfo {
        PgTypeInfo::INT2_ARRAY
    }
}

impl Encode<'_, Postgres> for i16 {
    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
        buf.extend(&self.to_be_bytes());

        IsNull::No
    }
}

impl Decode<'_, Postgres> for i16 {
    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {

Review comment (bug):
So this behavior isn't the same as what was there before. When the value is in text, it's a single ASCII character. That's why the test is failing.

This is for decoding the `"char"` type: https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/char.c#L58-L60

Even what was there before wasn't technically correct either, for values between `0x80` and `0xFF`.

I think this is what I would do:

```suggestion
        // note: decoding here is for the `"char"` type as Postgres does not have a native 1-byte integer type.
        // https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/char.c#L58-L60
        match value.format() {
            PgValueFormat::Binary => int_decode(value)?.try_into().map_err(Into::into),
            PgValueFormat::Text => {
                let text = value.as_str()?;
                
                // A value of 0 is represented with the empty string.
                if text.is_empty() {
                    return Ok(0);
                }
                
                if text.starts_with('\\') {
                    // For values between 0x80 and 0xFF, it's encoded in octal.
                    return Ok(i8::from_str_radix(text.trim_start_matches('\\'), 8)?);
                }
                
                Ok(text.as_bytes()[0] as i8)
            }
        }
```

Code after:
        PgTypeInfo::CHAR
    }
}

impl PgHasArrayType for i8 {
    fn array_type_info() -> PgTypeInfo {
        PgTypeInfo::CHAR_ARRAY
    }
}

impl Encode<'_, Postgres> for i8 {
    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
        buf.extend(&self.to_be_bytes());

        IsNull::No
    }
}

impl Decode<'_, Postgres> for i8 {
    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
        // note: decoding here is for the `"char"` type as Postgres does not have a native 1-byte integer type.
        // https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/char.c#L58-L60
        match value.format() {
            PgValueFormat::Binary => int_decode(value)?.try_into().map_err(Into::into),
            PgValueFormat::Text => {
                let text = value.as_str()?;

                // A value of 0 is represented with the empty string.
                if text.is_empty() {
                    return Ok(0);
                }

                if text.starts_with('\\') {
                    // For values between 0x80 and 0xFF, it's encoded in octal.
                    return Ok(i8::from_str_radix(text.trim_start_matches('\\'), 8)?);
                }

                Ok(text.as_bytes()[0] as i8)
            }
        }
    }
}

impl Type<Postgres> for i16 {
    fn type_info() -> PgTypeInfo {
        PgTypeInfo::INT2
    }
}

impl PgHasArrayType for i16 {
    fn array_type_info() -> PgTypeInfo {