Skip to content

Commit

Permalink
Implement Value for Vec<u8> and String
Browse files Browse the repository at this point in the history
- Allow Vec<u8>, String, Cow<'a, str>, Cow<'a, [u8]> to be used as
  value types
  • Loading branch information
bluk committed Sep 7, 2023
1 parent 7ec1b85 commit 5643176
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ mod tests {
}

#[test]
fn query_only_one_edb() -> Result<()> {
fn query_exclusive_edb() -> Result<()> {
let mut data = Passdata::with_schema(&QUERY_ONLY_ONE_EDB_SCHEMA);

data.add_fact("a", true)?;
Expand Down Expand Up @@ -654,6 +654,18 @@ mod tests {
assert_eq!(data.query_exclusive_edb::<_, i64>("c", 5678), Ok(None));

assert_eq!(data.query_exclusive_edb("d", AnyStr), Ok(Some("xyz")));
assert_eq!(
data.query_exclusive_edb("d", AnyStr),
Ok(Some(String::from("xyz")))
);
assert_eq!(
data.query_exclusive_edb("d", AnyStr),
Ok(Some("xyz".as_bytes()))
);
assert_eq!(
data.query_exclusive_edb("d", AnyStr),
Ok(Some("xyz".as_bytes().to_vec()))
);
assert_eq!(
data.query_exclusive_edb("d", AnyBytes),
Ok(Some("xyz".as_bytes()))
Expand Down
24 changes: 24 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,36 @@ impl<'a> Value for &'a [u8] {
}
}

impl Value for Vec<u8> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for Cow<'a, [u8]> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for &'a str {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl Value for String {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for Cow<'a, str> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for values::Constant<'a> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Any
Expand Down
28 changes: 26 additions & 2 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
//! absolute limit to the amount of data that can be encoded.

#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::borrow::Cow;
use alloc::{borrow::Cow, string::String, vec::Vec};
use core::fmt;
#[cfg(feature = "std")]
use std::{borrow::Cow, error};
use std::{borrow::Cow, error, string::String, vec::Vec};

use crate::{
error::{Error, ErrorKind},
Expand Down Expand Up @@ -188,6 +188,17 @@ impl<'a> TryFrom<Constant<'a>> for &'a [u8] {
}
}

impl<'a> TryFrom<Constant<'a>> for Vec<u8> {
type Error = InvalidType;

fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
match value {
Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
Constant::Bytes(bytes) => Ok(bytes.to_vec()),
}
}
}

impl<'a> TryFrom<Constant<'a>> for Cow<'a, [u8]> {
type Error = InvalidType;

Expand All @@ -210,6 +221,19 @@ impl<'a> TryFrom<Constant<'a>> for &'a str {
}
}

impl<'a> TryFrom<Constant<'a>> for String {
type Error = InvalidType;

fn try_from(value: Constant<'a>) -> Result<Self, Self::Error> {
match value {
Constant::Bool(_) | Constant::Num(_) => Err(InvalidType),
Constant::Bytes(bytes) => {
Ok(String::from_utf8(bytes.to_vec()).map_err(|_| InvalidType)?)
}
}
}
}

impl<'a> TryFrom<Constant<'a>> for Cow<'a, str> {
type Error = InvalidType;

Expand Down

0 comments on commit 5643176

Please sign in to comment.