From f5fa6124680bd9ffbb61febcdf6bfa89e204b3f1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 28 Jan 2023 21:21:13 +0800 Subject: [PATCH] feat: metadata mod for rust_ffi --- src/cli/src/main.rs | 2 +- src/rust_ffi/src/core.rs | 26 +++++++++++++++++++ src/rust_ffi/src/lib.rs | 48 +++--------------------------------- src/rust_ffi/src/metadata.rs | 27 ++++++++++++++++++++ 4 files changed, 57 insertions(+), 46 deletions(-) create mode 100644 src/rust_ffi/src/core.rs create mode 100644 src/rust_ffi/src/metadata.rs diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index 46bc784..fc18b3d 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,5 +1,5 @@ extern crate klotski_ffi; fn main() { - println!("{:#?}", klotski_ffi::load_version()); + println!("{:#?}", klotski_ffi::load_metadata()); } diff --git a/src/rust_ffi/src/core.rs b/src/rust_ffi/src/core.rs new file mode 100644 index 0000000..9d2c9eb --- /dev/null +++ b/src/rust_ffi/src/core.rs @@ -0,0 +1,26 @@ +/// Klotski c-style API convert by bindgen. +/// Core module expose these interfaces for abstraction. + +pub mod Core { + #![allow(dead_code)] + #![allow(non_snake_case)] + #![allow(non_camel_case_types)] + #![allow(non_upper_case_globals)] + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + + use std::ffi::{c_char, CStr}; + + pub trait CStringDump { + fn dump(&self) -> String; + } + + impl CStringDump for *const c_char { + fn dump(&self) -> String { + let cstr: &CStr; + unsafe { + cstr = CStr::from_ptr(*self); + } + String::from(cstr.to_str().unwrap()) + } + } +} diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index 95cc2ad..498f4f2 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -1,46 +1,4 @@ -#![allow(non_snake_case)] -#![allow(non_camel_case_types)] -#![allow(non_upper_case_globals)] -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +mod core; +mod metadata; -use std::env; -use std::ffi::{c_char, CStr}; - -#[derive(Debug)] -pub struct Version { - pub author: String, - pub system: String, - pub version: String, - pub compiler: String, - pub commit_id: String, - pub build_time: String, - pub project_url: String, -} - -pub trait CStringDump { - fn dump(&self) -> String; -} - -impl CStringDump for *const c_char { - fn dump(&self) -> String { - let cstr: &CStr; - unsafe { - cstr = CStr::from_ptr(*self); - } - String::from(cstr.to_str().unwrap()) - } -} - -pub fn load_version() -> Version { - unsafe { - Version { - author: get_author().dump(), - system: get_system_info().dump(), - version: get_version().dump(), - compiler: get_compiler_info().dump(), - commit_id: get_commit_id().dump(), - build_time: get_build_time().dump(), - project_url: get_project_url().dump(), - } - } -} +pub use metadata::load_metadata; diff --git a/src/rust_ffi/src/metadata.rs b/src/rust_ffi/src/metadata.rs new file mode 100644 index 0000000..f815509 --- /dev/null +++ b/src/rust_ffi/src/metadata.rs @@ -0,0 +1,27 @@ +use super::core::Core; +use Core::CStringDump; + +#[derive(Debug)] +pub struct Metadata { + pub author: String, + pub system: String, + pub version: String, + pub compiler: String, + pub commit_id: String, + pub build_time: String, + pub project_url: String, +} + +pub fn load_metadata() -> Metadata { + unsafe { + Metadata { + author: Core::get_author().dump(), + system: Core::get_system_info().dump(), + version: Core::get_version().dump(), + compiler: Core::get_compiler_info().dump(), + commit_id: Core::get_commit_id().dump(), + build_time: Core::get_build_time().dump(), + project_url: Core::get_project_url().dump(), + } + } +}