From e0684f5f050cec4d8308a5d726e1f68825995e9e Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 22 Oct 2023 19:48:06 +0200
Subject: [PATCH] BUILD: Begin to make things work on linux and windob' with
 wine

---
 Cargo.lock                              | 39 +++++++++++++++++++++++++
 Cargo.toml                              |  1 +
 lektor_nkdb/src/lib.rs                  | 11 +++++--
 lektor_nkdb/src/storage/disk_storage.rs |  4 ++-
 lektor_utils/Cargo.toml                 |  1 +
 lektor_utils/src/config/base.rs         |  2 +-
 lektor_utils/src/lib.rs                 | 24 +++++----------
 lektord/c/CMakeLists.txt                |  2 +-
 lektord/src/app.rs                      | 18 ++++++++----
 9 files changed, 74 insertions(+), 28 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index d4415621..8bf9af01 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -840,6 +840,27 @@ dependencies = [
  "crypto-common",
 ]
 
+[[package]]
+name = "dirs"
+version = "5.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
+dependencies = [
+ "dirs-sys",
+]
+
+[[package]]
+name = "dirs-sys"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
+dependencies = [
+ "libc",
+ "option-ext",
+ "redox_users",
+ "windows-sys 0.48.0",
+]
+
 [[package]]
 name = "dispatch"
 version = "0.2.0"
@@ -1885,6 +1906,7 @@ dependencies = [
  "anyhow",
  "base64",
  "chrono",
+ "dirs",
  "is-wsl",
  "libc",
  "log",
@@ -2440,6 +2462,12 @@ dependencies = [
  "vcpkg",
 ]
 
+[[package]]
+name = "option-ext"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
+
 [[package]]
 name = "orbclient"
 version = "0.3.46"
@@ -2845,6 +2873,17 @@ dependencies = [
  "bitflags 1.3.2",
 ]
 
+[[package]]
+name = "redox_users"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
+dependencies = [
+ "getrandom",
+ "redox_syscall 0.2.16",
+ "thiserror",
+]
+
 [[package]]
 name = "regex"
 version = "1.10.2"
diff --git a/Cargo.toml b/Cargo.toml
index 0fb8bdc5..43d99fff 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -61,6 +61,7 @@ chrono = { version = "0.4", default-features = false, features = ["clock"] }
 sha256 = { version = "1", default-features = false, features = ["async"] }
 base64 = "*"
 anyhow = "1"
+dirs = "5"
 
 # Data Structures
 hashbrown = { version = "0.14", features = ["serde"] }
diff --git a/lektor_nkdb/src/lib.rs b/lektor_nkdb/src/lib.rs
index f911de72..526808a1 100644
--- a/lektor_nkdb/src/lib.rs
+++ b/lektor_nkdb/src/lib.rs
@@ -11,7 +11,7 @@ pub use crate::{
 };
 
 use crate::{database::*, queue::*, search::*};
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
 use hashbrown::HashMap;
 use lektor_utils::{log, pushvec::*};
 use playlist::Playlists;
@@ -59,7 +59,9 @@ impl AsRef<str> for PlayState {
 impl<Storage: DatabaseStorage> Database<Storage> {
     /// Create a new database with the correspondig prefix.
     pub async fn new(prefix: impl Into<Storage::Prefix>) -> Result<Self> {
-        let storage = Storage::load_from_prefix(prefix.into()).await?;
+        let storage = Storage::load_from_prefix(prefix.into())
+            .await
+            .with_context(|| "failed to load database storage")?;
 
         log::info!("load the last epoch and populate the pool without factorization");
         let epochs: PushVec<Epoch> = Default::default();
@@ -75,7 +77,10 @@ impl<Storage: DatabaseStorage> Database<Storage> {
         .await;
 
         log::info!("fatcorize content in the last_epoch and in the playlists");
-        let mut playlists = storage.read_playlists().await?;
+        let mut playlists = storage
+            .read_playlists()
+            .await
+            .with_context(|| "failed to read playlists")?;
         pool.factorize_epoch_data(last_epoch.content().data_mut());
         playlists
             .iter_mut()
diff --git a/lektor_nkdb/src/storage/disk_storage.rs b/lektor_nkdb/src/storage/disk_storage.rs
index 8d3d7012..6dc408fd 100644
--- a/lektor_nkdb/src/storage/disk_storage.rs
+++ b/lektor_nkdb/src/storage/disk_storage.rs
@@ -271,7 +271,9 @@ impl DatabaseStorage for DatabaseDiskStorage {
             let folder = prefix.join(folder);
             match create_dir_all(&folder).await {
                 Err(err) if err.kind() != std::io::ErrorKind::AlreadyExists => {
-                    return Err(err).with_context(|| "failed to create folder")
+                    return Err(err).with_context(|| {
+                        format!("failed to create folder {}", folder.to_string_lossy())
+                    })
                 }
                 _ => {}
             }
diff --git a/lektor_utils/Cargo.toml b/lektor_utils/Cargo.toml
index c46b0c9d..a0c54bbe 100644
--- a/lektor_utils/Cargo.toml
+++ b/lektor_utils/Cargo.toml
@@ -18,6 +18,7 @@ doctest = false
 [dependencies]
 log.workspace = true
 toml.workspace = true
+dirs.workspace = true
 serde.workspace = true
 tokio.workspace = true
 anyhow.workspace = true
diff --git a/lektor_utils/src/config/base.rs b/lektor_utils/src/config/base.rs
index a7c7d6c0..8e95d67e 100644
--- a/lektor_utils/src/config/base.rs
+++ b/lektor_utils/src/config/base.rs
@@ -102,7 +102,7 @@ impl Default for LektorRepoConfig {
 impl Default for LektorDatabaseConfig {
     fn default() -> Self {
         Self {
-            folder: PathBuf::from("/home/kara"),
+            folder: crate::user_home_directory().join("kara"),
             autoclear: true,
             save_history: false,
         }
diff --git a/lektor_utils/src/lib.rs b/lektor_utils/src/lib.rs
index 07bd53e1..7ba1476e 100644
--- a/lektor_utils/src/lib.rs
+++ b/lektor_utils/src/lib.rs
@@ -15,22 +15,9 @@ pub mod pushvec;
 include!(concat!(env!("OUT_DIR"), "/commons_build_infos.rs"));
 
 /// Returns the home folder of the user. If no home folder is found log the
-/// error and panic. This function check in order the following env variables to
-/// determine the home folder:
-/// 1. HOME
-/// 2. HOMEPATH
-/// 3. USERPROFILE
+/// error and panic.
 pub fn user_home_directory() -> std::path::PathBuf {
-    use std::env::var;
-    std::path::PathBuf::from(if let Ok(home) = var("HOME") {
-        home
-    } else if let Ok(home) = var("HOMEPATH") {
-        home
-    } else if let Ok(home) = var("USERPROFILE") {
-        home
-    } else {
-        panic!("failed to find a home folder for the user...")
-    })
+    dirs::home_dir().expect("failed to find a home folder for the user...")
 }
 
 /// Returns the config folder for the user. If neither HOME, HOMEPATH,
@@ -39,7 +26,12 @@ pub fn user_home_directory() -> std::path::PathBuf {
 /// behaviour on unix systems.
 pub fn user_config_directory(app: impl AsRef<str>) -> std::path::PathBuf {
     let folder = user_home_directory().join(".config").join(app.as_ref());
-    std::fs::create_dir_all(&folder).expect("failed to create config folder for application");
+    std::fs::create_dir_all(&folder).unwrap_or_else(|err| {
+        panic!(
+            "failed to create config folder for application {}: {err}",
+            folder.to_string_lossy()
+        )
+    });
     folder
 }
 
diff --git a/lektord/c/CMakeLists.txt b/lektord/c/CMakeLists.txt
index ec9b6d5e..81169171 100644
--- a/lektord/c/CMakeLists.txt
+++ b/lektord/c/CMakeLists.txt
@@ -85,7 +85,7 @@ set(GNU_C_FLAGS
 
 if(WIN32)
     # list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .dll.a .lib)
-    set(Qt6_USE_STATIC_LIBS ON)    
+    set(Qt6_USE_STATIC_LIBS ON)
     set(Qt6_USE_STATIC_RUNTIME ON)
 endif()
 
diff --git a/lektord/src/app.rs b/lektord/src/app.rs
index a8d70b5e..a1f76f17 100644
--- a/lektord/src/app.rs
+++ b/lektord/src/app.rs
@@ -3,7 +3,7 @@
 #![forbid(unsafe_code)]
 
 use crate::{routes, LektorConfig};
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
 use axum::{
     http::{Request, StatusCode},
     middleware::Next,
@@ -58,7 +58,9 @@ pub async fn app(config: LektorConfig) -> Result<(Router, tokio::sync::oneshot::
     }
 
     let (sender, receiver) = tokio::sync::oneshot::channel();
-    let state = LektorState::new(config, sender).await?;
+    let state = LektorState::new(config, sender)
+        .await
+        .with_context(|| "failed to build lektord state")?;
     let route = router! { "/" -> get: routes::root
 
                // Control the playback
@@ -173,19 +175,23 @@ impl LektorState {
             false => LektorUser::User(user.user.into(), user.token.into()),
         });
         let ptr = LektorStatePtr::from(Arc::new(Self {
-            database: Database::<DatabaseDiskStorage>::new(folder).await?,
+            database: Database::<DatabaseDiskStorage>::new(folder)
+                .await
+                .with_context(|| "failed to build database")?,
             repo: Repo::new(repo),
             users: users.collect(),
             mpris: Default::default(),
             shutdown: RwLock::new(Some(shutdown)),
         }));
         if config.mpris {
-            *ptr.mpris.write().await = Some(
+            *ptr.mpris.write().await =
                 lektor_mpris::MPRISAdapter::builder("lektord", LektorStateWeakPtr::from(&ptr))
                     .desktop_entry("Lektord")
                     .try_build()
-                    .await?,
-            );
+                    .await
+                    .with_context(|| "failed to build mpris server, run with no mpris")
+                    .map_err(|err| log::error!("{err}"))
+                    .ok();
         }
         crate::c_wrapper::init_player_module(ptr.clone(), player)?;
         Ok(ptr)
-- 
GitLab