Skip to content

Commit

Permalink
nits / docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kflansburg committed Mar 25, 2024
1 parent 4b29162 commit d36e6c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion worker-sandbox/src/d1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn prepared_statement(
assert_eq!(person.name, "John Smith");
assert_eq!(person.age, 92);

let prepared_argument = PreparedArgument::new(&D1Type::Text("Dorian Fischer"));
let prepared_argument = D1PreparedArgument::new(&D1Type::Text("Dorian Fischer"));
let stmt_3 = unbound_stmt.bind_refs(&prepared_argument)?;
let person = stmt_3.first::<Person>(None).await?.unwrap();
assert_eq!(person.name, "Dorian Fischer");
Expand Down
31 changes: 16 additions & 15 deletions worker/src/d1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ impl From<D1DatabaseSys> for D1Database {
}
}

/// Possible arguments that can be bound to [`D1PreparedStatement`]
/// Possible argument types that can be bound to [`D1PreparedStatement`]
/// See https://developers.cloudflare.com/d1/build-with-d1/d1-client-api/#type-conversion
pub enum D1Type<'a> {
Null,
Real(f64),
// I believe JS always casts to float. Documentation states it can accept up to 53 bits of signed precision
// so I went with i32 here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type
// D1 does not support `BigInt`
Integer(i32),
Text(&'a str),
Boolean(bool),
Expand All @@ -148,15 +149,15 @@ pub enum D1Type<'a> {
/// A pre-computed argument for `bind_refs`.
///
/// Arguments must be converted to `JsValue` when bound. If you plan to
/// re-use the same argument multiple times, consider using a `PreparedArgument`
/// re-use the same argument multiple times, consider using a `D1PreparedArgument`
/// which does this once on construction.
pub struct PreparedArgument<'a> {
pub struct D1PreparedArgument<'a> {
value: &'a D1Type<'a>,
js_value: JsValue,
}

impl<'a> PreparedArgument<'a> {
pub fn new(value: &'a D1Type) -> PreparedArgument<'a> {
impl<'a> D1PreparedArgument<'a> {
pub fn new(value: &'a D1Type) -> D1PreparedArgument<'a> {
Self {
value,
js_value: value.into(),
Expand All @@ -166,18 +167,18 @@ impl<'a> PreparedArgument<'a> {

impl<'a> From<&'a D1Type<'a>> for JsValue {
fn from(value: &'a D1Type<'a>) -> Self {
match value {
match *value {
D1Type::Null => JsValue::null(),
D1Type::Real(f) => JsValue::from_f64(*f),
D1Type::Integer(i) => JsValue::from_f64(*i as f64),
D1Type::Real(f) => JsValue::from_f64(f),
D1Type::Integer(i) => JsValue::from_f64(i as f64),
D1Type::Text(s) => JsValue::from_str(s),
D1Type::Boolean(b) => JsValue::from_bool(*b),
D1Type::Boolean(b) => JsValue::from_bool(b),
D1Type::Blob(a) => serde_wasm_bindgen::to_value(a).unwrap(),
}
}
}

impl<'a> Deref for PreparedArgument<'a> {
impl<'a> Deref for D1PreparedArgument<'a> {
type Target = D1Type<'a>;
fn deref(&self) -> &Self::Target {
self.value
Expand All @@ -193,10 +194,10 @@ impl<'a> IntoIterator for &'a D1Type<'a> {
}
}

impl<'a> IntoIterator for &'a PreparedArgument<'a> {
type Item = &'a PreparedArgument<'a>;
type IntoIter = Once<&'a PreparedArgument<'a>>;
/// Allows a single &PreparedArgument to be passed to `bind_refs`, without placing it in an array.
impl<'a> IntoIterator for &'a D1PreparedArgument<'a> {
type Item = &'a D1PreparedArgument<'a>;
type IntoIter = Once<&'a D1PreparedArgument<'a>>;
/// Allows a single &D1PreparedArgument to be passed to `bind_refs`, without placing it in an array.
fn into_iter(self) -> Self::IntoIter {
once(self)
}
Expand All @@ -212,7 +213,7 @@ impl<'a> D1Argument for D1Type<'a> {
}
}

impl<'a> D1Argument for PreparedArgument<'a> {
impl<'a> D1Argument for D1PreparedArgument<'a> {
fn js_value(&self) -> impl AsRef<JsValue> {
&self.js_value
}
Expand Down

0 comments on commit d36e6c2

Please sign in to comment.