Browse Source

feat: metadata mod for rust_ffi

legacy
Dnomd343 2 years ago
parent
commit
f5fa612468
  1. 2
      src/cli/src/main.rs
  2. 26
      src/rust_ffi/src/core.rs
  3. 48
      src/rust_ffi/src/lib.rs
  4. 27
      src/rust_ffi/src/metadata.rs

2
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());
}

26
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())
}
}
}

48
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;

27
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(),
}
}
}
Loading…
Cancel
Save