Skip to content

Commit

Permalink
types: introduce read_value
Browse files Browse the repository at this point in the history
Introduce the `read_value` function which is able to read a [value], as
specified in the CQL protocol. It will be used in the next commit, in
order to make the interface of the SerializedValue iterators more
correct.
  • Loading branch information
piodul committed Oct 26, 2023
1 parent 9965289 commit 864375a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions scylla-cql/src/frame/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ impl From<std::array::TryFromSliceError> for ParseError {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RawValue<'a> {
Null,
Unset,
Value(&'a [u8]),
}

impl<'a> RawValue<'a> {
#[inline]
pub fn as_value(&self) -> Option<&'a [u8]> {
match self {
RawValue::Value(v) => Some(v),
RawValue::Null | RawValue::Unset => None,
}
}
}

fn read_raw_bytes<'a>(count: usize, buf: &mut &'a [u8]) -> Result<&'a [u8], ParseError> {
if buf.len() < count {
return Err(ParseError::BadIncomingData(format!(
Expand Down Expand Up @@ -218,6 +235,22 @@ pub fn read_bytes<'a>(buf: &mut &'a [u8]) -> Result<&'a [u8], ParseError> {
Ok(v)
}

pub fn read_value<'a>(buf: &mut &'a [u8]) -> Result<RawValue<'a>, ParseError> {
let len = read_int(buf)?;
match len {
-2 => Ok(RawValue::Unset),
-1 => Ok(RawValue::Null),
len if len >= 0 => {
let v = read_raw_bytes(len as usize, buf)?;
Ok(RawValue::Value(v))
}
len => Err(ParseError::BadIncomingData(format!(
"invalid value length: {}",
len,
))),
}
}

pub fn read_short_bytes<'a>(buf: &mut &'a [u8]) -> Result<&'a [u8], ParseError> {
let len = read_short_length(buf)?;
let v = read_raw_bytes(len, buf)?;
Expand Down

0 comments on commit 864375a

Please sign in to comment.