From 28ab91329f0df964d6f960eda3b216a5df0921c9 Mon Sep 17 00:00:00 2001 From: Rémi Bardon Date: Fri, 30 May 2025 17:53:47 +0000 Subject: fix(filamento): fix non-WASM build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: cel 🌸 --- filamento/src/db.rs | 71 ++++++++++++++++---------------------------------- filamento/src/error.rs | 6 ++--- 2 files changed, 25 insertions(+), 52 deletions(-) (limited to 'filamento/src') diff --git a/filamento/src/db.rs b/filamento/src/db.rs index 1b5afe6..bac1b7c 100644 --- a/filamento/src/db.rs +++ b/filamento/src/db.rs @@ -58,53 +58,22 @@ impl Db { pub async fn create_connect_and_migrate( path: impl AsRef + Send, ) -> Result { - let (sender, receiver) = mpsc::channel(20); - let (result_send, result_recv) = oneshot::channel(); - spawn_blocking(move || { - let result = DbActor::new(path, receiver).await; - match result { - Ok(a) => { - result_send.send(Ok(())); - a.run() - } - Err(e) => { - result_send.send(Err(e)); - } - } - }); - match result_recv.await { - Ok(r) => match r { - Ok(o) => Ok(Self { sender }), - Err(e) => return Err(e), - }, - Err(e) => return Err(e.into()), - } + let (sender, receiver) = mpsc::unbounded_channel(); + + let actor = DbActor::new(path, receiver)?; + spawn_blocking(move || actor.run()); + + Ok(Self { sender }) } #[cfg(not(target_arch = "wasm32"))] pub async fn create_connect_and_migrate_memory() -> Result { - let (sender, receiver) = mpsc::channel(20); - let (result_send, result_recv) = oneshot::channel(); - spawn_blocking(move || { - let result = DbActor::new_memory(receiver).await; - match result { - Ok(a) => { - result_send.send(Ok(())); - // TODO: async run when not wasm - a.run() - } - Err(e) => { - result_send.send(Err(e)); - } - } - }); - match result_recv.await { - Ok(r) => match r { - Ok(o) => Ok(Self { sender }), - Err(e) => return Err(e), - }, - Err(e) => return Err(e.into()), - } + let (sender, receiver) = mpsc::unbounded_channel(); + + let actor = DbActor::new_memory(receiver)?; + spawn_blocking(move || actor.run()); + + Ok(Self { sender }) } /// `file_name` should be a file not in a directory @@ -758,17 +727,19 @@ impl Display for DbCommand { impl DbActor { /// must be run in blocking spawn #[cfg(not(target_arch = "wasm32"))] - pub(crate) fn new(path: impl AsRef, receiver: mpsc::Receiver) -> Self { + pub(crate) fn new( + path: impl AsRef, + receiver: mpsc::UnboundedReceiver, + ) -> Result { if let Some(dir) = path.as_ref().parent() { if dir.is_dir() { } else { - tokio::fs::create_dir_all(dir).await?; + std::fs::create_dir_all(dir)?; } - let _file = tokio::fs::OpenOptions::new() + let _file = std::fs::OpenOptions::new() .append(true) .create(true) - .open(path.as_ref()) - .await?; + .open(path.as_ref())?; } let url = format!( "{}", @@ -786,7 +757,9 @@ impl DbActor { /// must be run in blocking spawn #[cfg(not(target_arch = "wasm32"))] - pub(crate) fn new_memory(receiver: mpsc::Receiver) -> Self { + pub(crate) fn new_memory( + receiver: mpsc::UnboundedReceiver, + ) -> Result { let db = Connection::open_in_memory()?; db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { db, receiver }) diff --git a/filamento/src/error.rs b/filamento/src/error.rs index af3320f..f9f9199 100644 --- a/filamento/src/error.rs +++ b/filamento/src/error.rs @@ -297,7 +297,7 @@ pub enum DatabaseOpenError { // #[error("migration: {0}")] // Migration(Arc), #[error("io: {0}")] - Io(Arc), + Io(Arc), #[error("invalid path")] InvalidPath, #[error("tokio oneshot recv error: {0}")] @@ -310,8 +310,8 @@ pub enum DatabaseOpenError { // } // } -impl From for DatabaseOpenError { - fn from(e: tokio::io::Error) -> Self { +impl From for DatabaseOpenError { + fn from(e: std::io::Error) -> Self { Self::Io(Arc::new(e)) } } -- cgit