diff options
Diffstat (limited to 'filamento')
-rw-r--r-- | filamento/src/db.rs | 71 | ||||
-rw-r--r-- | filamento/src/error.rs | 6 |
2 files changed, 25 insertions, 52 deletions
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<Path> + Send, ) -> Result<Self, DatabaseOpenError> { - 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<Self, DatabaseOpenError> { - 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<Path>, receiver: mpsc::Receiver<DbCommand>) -> Self { + pub(crate) fn new( + path: impl AsRef<Path>, + receiver: mpsc::UnboundedReceiver<DbCommand>, + ) -> Result<Self, DatabaseOpenError> { 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<DbCommand>) -> Self { + pub(crate) fn new_memory( + receiver: mpsc::UnboundedReceiver<DbCommand>, + ) -> Result<Self, DatabaseOpenError> { 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<rusqlite::migrate::MigrateError>), #[error("io: {0}")] - Io(Arc<tokio::io::Error>), + Io(Arc<std::io::Error>), #[error("invalid path")] InvalidPath, #[error("tokio oneshot recv error: {0}")] @@ -310,8 +310,8 @@ pub enum DatabaseOpenError { // } // } -impl From<tokio::io::Error> for DatabaseOpenError { - fn from(e: tokio::io::Error) -> Self { +impl From<std::io::Error> for DatabaseOpenError { + fn from(e: std::io::Error) -> Self { Self::Io(Arc::new(e)) } } |