Skip to content
Extraits de code Groupes Projets
Vérifiée Valider c188f497 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

RUST: Continue C <-> Rust bindings + we will handle the config on the rust side of the force

parent 613c9d85
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
{
"settings": {
"ltex.language": "en",
"languageTool.language": "en",
"editor.formatOnSave": true,
......@@ -10,4 +9,3 @@
"rust-analyzer.procMacro.enable": true,
"rust-analyzer.procMacro.attributes.enable": true,
}
\ No newline at end of file
}
......@@ -187,7 +187,6 @@ set(lektor_db_SOURCES
set(lektor_module_SOURCES
src/base/reg.c
src/module/module_repo.c
src/module/module_repo_rs.c
src/module/mpv.c
src/module/thread.c
......
#if !defined(LIBLEKTOR_RS_DATABASE___)
#define LIBLEKTOR_RS_DATABASE___
#if defined(__cplusplus)
extern "C" {
#endif
#include <stdint.h>
bool lkt_config_write_default_to_file(const char *);
bool lkt_config_write_default_to_default_file(void);
#if defined(__cplusplus)
}
#endif
#endif // LIBLEKTOR_RS_DATABASE___
......@@ -22,7 +22,7 @@ typedef struct lkt_database_queue lkt_database_queue;
*/
typedef void (*lkt_search_cb)(int64_t, void *restrict);
lkt_database_queue *lkt_database_queue_new();
lkt_database_queue *lkt_database_queue_new(void);
void lkt_database_queue_delete(lkt_database_queue *);
bool lkt_database_queue_current(lkt_database_queue *, int64_t *restrict);
......
......@@ -9,8 +9,10 @@
#include <lektor/launch.h>
#include <lektor/logfile.h>
#include <liblektor-rs/database.h>
#include <liblektor-rs/loging.h>
#include <liblektor-rs/config.h>
#include <liblektor-rs/database.h>
#include <liblektor-rs/queue.h>
#include <wait.h>
#include <spawn.h>
......@@ -20,7 +22,6 @@
#if defined(LKT_STATIC_MODULE)
REG_DECLARE(qt_window_reg)
REG_DECLARE(repo_reg)
REG_DECLARE(repo_rs_reg)
#endif
......@@ -42,7 +43,6 @@ main(int argc, char *argv[])
REG_ADD(database_upgrade_scheme)
#if defined(LKT_STATIC_MODULE)
REG_REGISTER("repo_rs", repo_rs_reg)
REG_REGISTER("repo", repo_reg)
REG_REGISTER("qt", qt_window_reg)
#endif
REG_END()
......@@ -92,6 +92,7 @@ main(int argc, char *argv[])
FAIL_IF(lkt_queue_new(&srv.queue), "Failed to create server queue");
FAIL_UNLESS(database_new(&srv.db), "Failed to init memory database");
FAIL_IF(config_open(srv.db, conf_file, PATH_MAX), "Failed to open or create the config file");
lkt_config_write_default_to_default_file();
/* Dump and abort here, if we are dumping informations about the server */
if (dump_and_abort) {
......@@ -108,6 +109,9 @@ main(int argc, char *argv[])
lkt_set_log_logfile(logfile);
lektor_init_rust_logging();
/* Create objects from the rust part of the code */
lkt_database_queue *const UNUSED queue = lkt_database_queue_new();
/* Read the configuration. We already know that the config is valid */
database_config_get_int(srv.db, "player", "autoclear", &autoclear);
database_config_get_text(srv.db, "database", "kara_dir", kara_dir, PATH_MAX);
......
......@@ -6,6 +6,7 @@ members = [
"lektor_db",
"lektor_repo",
"lektor_unsafe",
"lektor_config",
# Common things
"kurisu_api",
......
[package]
name = "lektor_config"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
[dependencies]
log.workspace = true
serde.workspace = true
thiserror.workspace = true
lektor_c_compat = { path = "../lektor_c_compat" }
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
pub enum RepoApiVersion {
V1,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum LogLevel {
#[serde(rename = "DEBUG")]
Debug,
#[serde(rename = "INFO")]
Info,
#[serde(rename = "WARN")]
Warn,
#[serde(rename = "ERROR")]
Error,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct LektorConfig {
pub log: LektorLogConfig,
pub database: LektorDatabaseConfig,
pub player: LektorPlayerConfig,
pub repo: LektorRepoConfig,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LektorLogConfig {
pub level: LogLevel,
pub folder: PathBuf,
pub file_count: u64,
pub line_count: u64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LektorDatabaseConfig {
pub kara_dir: PathBuf,
pub dir_permission: u64,
pub db_path: PathBuf,
pub autoclear_queue: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LektorPlayerConfig {
pub module: String,
pub fond_size: u64,
pub font_name: String,
pub msg_duration: u64,
pub force_x11: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LektorRepoConfig {
pub module: String,
pub workers: usize,
pub server: Vec<RepoConfig>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RepoConfig {
pub name: String,
pub api: RepoApiVersion,
pub urls: Vec<String>,
}
impl Default for LogLevel {
fn default() -> Self {
LogLevel::Debug
}
}
impl Default for LektorLogConfig {
fn default() -> Self {
Self {
level: Default::default(),
folder: PathBuf::from("/home/kara/logs"),
file_count: 10,
line_count: 1000,
}
}
}
impl Default for LektorDatabaseConfig {
fn default() -> Self {
Self {
kara_dir: PathBuf::from("/home/kara"),
dir_permission: 0o700,
db_path: PathBuf::from("/home/kara/lektor.db"),
autoclear_queue: true,
}
}
}
impl Default for LektorPlayerConfig {
fn default() -> Self {
Self {
module: String::from("qt"),
fond_size: 12,
font_name: String::from("monospace"),
msg_duration: 4,
force_x11: true,
}
}
}
impl Default for LektorRepoConfig {
fn default() -> Self {
Self {
module: String::from("repo_rs"),
workers: 2,
server: vec![
Default::default(),
RepoConfig {
name: String::from("Dummy"),
api: RepoApiVersion::V1,
urls: Default::default(),
},
],
}
}
}
impl Default for RepoConfig {
fn default() -> Self {
Self {
name: String::from("Kurisu"),
api: RepoApiVersion::V1,
urls: vec![
String::from("https://kurisu.iiens.net/api"),
String::from("http://kurisu.iiens.net/api"),
],
}
}
}
mod config;
pub use config::*;
......@@ -8,8 +8,11 @@ crate-type = ["staticlib"]
[dependencies]
log.workspace = true
toml.workspace = true
lazy_static.workspace = true
lektor_c_compat = { path = "../lektor_c_compat", features = ["c_types"] }
lektor_config = { path = "../lektor_config" }
lektor_repo = { path = "../lektor_repo" }
lektor_db = { path = "../lektor_db" }
commons = { path = "../commons" }
//! An unsafe interface around the rust implementation of the config read/write
//! things to be able to call it from C or C++ code.
//!
//! Be carefull when naming things because those names might collide with things
//! defined in lektor's C code...
use crate::*;
use lektor_config::*;
#[no_mangle]
pub unsafe extern "C" fn lkt_config_write_default_to_file(file: *const u8) -> bool {
let mut len = 0;
while *file.offset(len) != 0 {
len += 1
}
let file = ManuallyDrop::new(String::from_raw_parts(
file as *mut _,
len as usize,
len as usize,
));
let conf = match toml::to_string_pretty(&LektorConfig::default()) {
Ok(conf) => conf,
Err(err) => {
log::error!(target: "CONF", "failed to serialize default config: {err}");
return false;
}
};
match std::fs::write(PathBuf::from(&file[..]), conf) {
Ok(()) => true,
Err(err) => {
log::error!(target: "CONF", "failed to write default config to {}: {err}", &file[..]);
false
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lkt_config_write_default_to_default_file() -> bool {
let path = commons::user_config_directory("lektor").join("lektor.toml");
let conf = match toml::to_string_pretty(&LektorConfig::default()) {
Ok(conf) => conf,
Err(err) => {
log::error!(target: "CONF", "failed to serialize default config: {err}");
return false;
}
};
match std::fs::write(&path, conf) {
Ok(()) => true,
Err(err) => {
log::error!(target: "CONF", "failed to write default config to {}: {err}", path.to_string_lossy());
false
}
}
}
//! Create C functions to calls things defined in the rust language from C code.
//! See the headers inside the liblektor-rs folder from the C include directory.
pub mod config;
pub mod db;
pub mod loging;
pub mod queue;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter