Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial pass on Rust bindings #597

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resolver = "2"
members = [
"bindings/java",
"bindings/python",
"bindings/rust",
"bindings/wasm",
"cli",
"sqlite3",
Expand Down
16 changes: 16 additions & 0 deletions bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2025 the Limbo authors. All rights reserved. MIT license.

[package]
name = "limbo_libsql"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
limbo_core = { path = "../../core" }
thiserror = "2.0.9"

[dev-dependencies]
tokio = { version = "1.29.1", features = ["full"] }
128 changes: 128 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
pub mod params;
mod value;

pub use params::params_from_iter;

use crate::params::*;
use crate::value::*;
use std::rc::Rc;
use std::sync::Arc;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("SQL conversion failure: `{0}`")]
ToSqlConversionFailure(crate::BoxError),
}

impl From<limbo_core::LimboError> for Error {
fn from(_err: limbo_core::LimboError) -> Self {
todo!();
}
}

pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;

pub type Result<T> = std::result::Result<T, Error>;
pub struct Builder {
path: String,
}

impl Builder {
pub fn new_local(path: &str) -> Self {
Self {
path: path.to_string(),
}
}

#[allow(unused_variables, clippy::arc_with_non_send_sync)]
pub async fn build(self) -> Result<Database> {
match self.path.as_str() {
":memory:" => {
let io: Arc<dyn limbo_core::IO> = Arc::new(limbo_core::MemoryIO::new()?);
let db = limbo_core::Database::open_file(io, self.path.as_str())?;
Ok(Database { inner: db })
}
_ => todo!(),
}
}
}

pub struct Database {
inner: Arc<limbo_core::Database>,
}

impl Database {
pub fn connect(self) -> Result<Connection> {
let conn = self.inner.connect();
Ok(Connection { inner: conn })
}
}

pub struct Connection {
inner: Rc<limbo_core::Connection>,
}

impl Connection {
pub async fn query(&self, sql: &str, params: impl IntoParams) -> Result<Rows> {
let mut stmt = self.prepare(sql).await?;
stmt.query(params).await
}

pub async fn execute(&self, sql: &str, params: impl IntoParams) -> Result<u64> {
let mut stmt = self.prepare(sql).await?;
stmt.execute(params).await
}

pub async fn prepare(&self, sql: &str) -> Result<Statement> {
let stmt = self.inner.prepare(sql)?;
Ok(Statement {
_inner: Rc::new(stmt),
})
}
}

pub struct Statement {
_inner: Rc<limbo_core::Statement>,
}

impl Statement {
pub async fn query(&mut self, params: impl IntoParams) -> Result<Rows> {
let _params = params.into_params()?;
todo!();
}

pub async fn execute(&mut self, params: impl IntoParams) -> Result<u64> {
let _params = params.into_params()?;
todo!();
}
}

pub trait IntoValue {
fn into_value(self) -> Result<Value>;
}

#[derive(Debug, Clone)]
pub enum Params {
None,
Positional(Vec<Value>),
Named(Vec<(String, Value)>),
}
pub struct Transaction {}

pub struct Rows {
_inner: Rc<limbo_core::Rows>,
}

impl Rows {
pub async fn next(&mut self) -> Result<Option<Row>> {
todo!();
}
}

pub struct Row {}

impl Row {
pub fn get_value(&self, _index: usize) -> Result<Value> {
todo!();
}
}
Loading
Loading