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

Code before:
impl TryFrom<&str> for PgCube {
    type Error = Error;

    fn try_from(input: &str) -> Result<Self, Self::Error> {
        let content = &input.get(1..input.len() - 1).ok_or(Error::Decode(
            format!("Could not decode cube string: {}", input).into(),
        ))?;

        if !content.contains('(') && !content.contains(',') {
            return parse_point(content);
        }

        if !content.contains("),(") {
            return parse_zero_volume(content);
        }

        let point_vecs = content.split("),(").collect::<Vec<&str>>();
        if point_vecs.len() == 2 && !point_vecs.iter().any(|pv| pv.contains(',')) {
            return parse_one_dimensional_interval(point_vecs);
        }

        parse_multidimensional_interval(point_vecs)
    }
}

impl TryFrom<&[u8]> for PgCube {
    type Error = Error;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        let cube_type = bytes
            .get(0)
            .map(|&byte| byte as usize)
            .ok_or(Error::Decode(
                format!("Could not decode cube bytes: {:?}", bytes).into(),
            ))?;

        let dimensionality = bytes
            .get(DIMENSIONALITY_POSITION)
            .map(|&byte| byte as usize)
            .ok_or(Error::Decode(
                format!("Could not decode cube bytes: {:?}", bytes).into(),
            ))?;

        match (cube_type, dimensionality) {
            (CUBE_TYPE_ZERO_VOLUME, CUBE_DIMENSION_ONE) => {
                let point = get_f64_from_bytes(&bytes, 4)?;
                Ok(PgCube::Point(point))
            }
            (CUBE_TYPE_ZERO_VOLUME, _) => {
                Ok(PgCube::ZeroVolume(deserialize_vector(&bytes, START_INDEX)?))
            }

Review comment (suggestion):
Binary format parsing shouldn't be exposed as a `TryFrom<&[u8]>` impl. 

There's no reason I can see that someone would want to use this outside of this module. It's not obvious what format this is meant to be; someone might look at this and assume it's the same as the `TryFrom<&str>` impl but bypassing UTF-8 validation. And there's no inverse operation exposed to serialize it as bytes (not that there should be).

Code after:

    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
        self.serialize(buf)?;
        Ok(IsNull::No)
    }
}

impl FromStr for PgCube {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let content = s
            .trim_start_matches('(')
            .trim_start_matches('[')
            .trim_end_matches(')')
            .trim_end_matches(']')
            .replace(' ', "");

        if !content.contains('(') && !content.contains(',') {
            return parse_point(&content);
        }

        if !content.contains("),(") {
            return parse_zero_volume(&content);
        }

        let point_vecs = content.split("),(").collect::<Vec<&str>>();
        if point_vecs.len() == 2 && !point_vecs.iter().any(|pv| pv.contains(',')) {
            return parse_one_dimensional_interval(point_vecs);
        }

        parse_multidimensional_interval(point_vecs)
    }
}

fn pg_cube_from_bytes(bytes: &[u8]) -> Result<PgCube, Error> {
    let cube_type = bytes
        .first()
        .map(|&byte| byte as usize)
        .ok_or(Error::Decode(
            format!("Could not decode cube bytes: {:?}", bytes).into(),
        ))?;

    let dimensionality = bytes
        .get(DIMENSIONALITY_POSITION)
        .map(|&byte| byte as usize)
        .ok_or(Error::Decode(
            format!("Could not decode cube bytes: {:?}", bytes).into(),
        ))?;

    match (cube_type, dimensionality) {