From 13d88ae3a31e80a0e8d5dfbd066cd1ffb93f603e Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 11 Dec 2022 12:44:46 +0100
Subject: [PATCH] RUST: Hide the C types behind a feature to forbid any usage
 by safe creates

---
 src/rust/liblektor-rs/lektor_c_compat/Cargo.toml |  3 +++
 src/rust/liblektor-rs/lektor_c_compat/src/lib.rs | 15 ++++++++++++---
 src/rust/liblektor-rs/lektor_repo/src/lib.rs     |  2 +-
 src/rust/liblektor-rs/lektor_unsafe/Cargo.toml   |  2 +-
 src/rust/liblektor-rs/lektor_unsafe/src/lib.rs   |  2 +-
 src/rust/liblektor-rs/lektor_unsafe/src/repo.rs  |  2 +-
 6 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/rust/liblektor-rs/lektor_c_compat/Cargo.toml b/src/rust/liblektor-rs/lektor_c_compat/Cargo.toml
index d39190b9..4a505687 100644
--- a/src/rust/liblektor-rs/lektor_c_compat/Cargo.toml
+++ b/src/rust/liblektor-rs/lektor_c_compat/Cargo.toml
@@ -9,3 +9,6 @@ license.workspace = true
 log.workspace = true
 libc.workspace = true
 lazy_static.workspace = true
+
+[features]
+c_types = []
diff --git a/src/rust/liblektor-rs/lektor_c_compat/src/lib.rs b/src/rust/liblektor-rs/lektor_c_compat/src/lib.rs
index 241109ad..dfc7a034 100644
--- a/src/rust/liblektor-rs/lektor_c_compat/src/lib.rs
+++ b/src/rust/liblektor-rs/lektor_c_compat/src/lib.rs
@@ -4,10 +4,19 @@
 //! From safe rust code you *must not* use the types defined in the [c_types]
 //! module.
 
-pub mod c_types;
-
+pub(crate) mod c_types;
 mod rs_types;
-pub use rs_types::*;
+
+/// Re-exports the Rust types.
+pub mod rs {
+    pub use crate::rs_types::*;
+}
+
+/// Re-exports the C types if the feature is enabled.
+#[cfg(feature = "c_types")]
+pub mod c {
+    pub use crate::c_types::*;
+}
 
 pub use libc::{c_char, c_int, c_long, c_uint, c_void, size_t};
 
diff --git a/src/rust/liblektor-rs/lektor_repo/src/lib.rs b/src/rust/liblektor-rs/lektor_repo/src/lib.rs
index 78bd5497..ffd59cd6 100644
--- a/src/rust/liblektor-rs/lektor_repo/src/lib.rs
+++ b/src/rust/liblektor-rs/lektor_repo/src/lib.rs
@@ -1,6 +1,6 @@
 //! The crate responsible of downloading karas from kurisu.
 
-use lektor_c_compat::*;
+use lektor_c_compat::rs::*;
 
 /// The structure responsible to download karas from kurisu.
 pub struct LktModuleRepoRs {
diff --git a/src/rust/liblektor-rs/lektor_unsafe/Cargo.toml b/src/rust/liblektor-rs/lektor_unsafe/Cargo.toml
index 588c5d84..fd41d724 100644
--- a/src/rust/liblektor-rs/lektor_unsafe/Cargo.toml
+++ b/src/rust/liblektor-rs/lektor_unsafe/Cargo.toml
@@ -9,6 +9,6 @@ crate-type = ["staticlib"]
 [dependencies]
 log.workspace = true
 
-lektor_c_compat = { path = "../lektor_c_compat" }
+lektor_c_compat = { path = "../lektor_c_compat", features = ["c_types"] }
 lektor_repo = { path = "../lektor_repo" }
 lektor_db = { path = "../lektor_db" }
diff --git a/src/rust/liblektor-rs/lektor_unsafe/src/lib.rs b/src/rust/liblektor-rs/lektor_unsafe/src/lib.rs
index c89df43f..b99c1a47 100644
--- a/src/rust/liblektor-rs/lektor_unsafe/src/lib.rs
+++ b/src/rust/liblektor-rs/lektor_unsafe/src/lib.rs
@@ -6,6 +6,6 @@
 pub mod db;
 pub mod repo;
 
-pub(crate) use lektor_c_compat::*;
+pub(crate) use lektor_c_compat::{c::*, *};
 pub(crate) use log::error;
 pub(crate) use std::{mem::ManuallyDrop, path::PathBuf};
diff --git a/src/rust/liblektor-rs/lektor_unsafe/src/repo.rs b/src/rust/liblektor-rs/lektor_unsafe/src/repo.rs
index f5bd90de..89346dd9 100644
--- a/src/rust/liblektor-rs/lektor_unsafe/src/repo.rs
+++ b/src/rust/liblektor-rs/lektor_unsafe/src/repo.rs
@@ -1,4 +1,4 @@
-use crate::{c_types::*, *};
+use crate::*;
 use lektor_repo::*;
 
 /// A pointer to the repo structure. This is the only thing the C code will see.
-- 
GitLab