fxkit 0.1.4

Useful utilities for writting Rust CLI tools
Documentation
use std::ffi::c_char;

use crate::{
    core::os::unix::linux::os_info::{find_os_release, get_os, get_os_version, read_value_os_release},
    utils::convert::cstr::{cstr_to_str, str_to_cstr},
};

/// Looks for the ``os-release`` file and returns it
/// # Safety
/// If the function fails to find the ``os-release`` file, it returns null
#[unsafe(no_mangle)]
pub extern "C" fn fxkit_find_os_release() -> *const c_char {
    let os_release = match find_os_release() {
        Ok(os) => unsafe { str_to_cstr(&os) },
        Err(e) => unsafe { str_to_cstr(&e.to_string()) },
    };

    os_release
}

#[unsafe(no_mangle)]
pub extern "C" fn fxkit_read_value_os_release(value: *const c_char) -> *const c_char {
    let value = unsafe { cstr_to_str(value) };

    match read_value_os_release(&value) {
        Ok(result) => unsafe { str_to_cstr(&result) },
        Err(e) => unsafe { str_to_cstr(&e.to_string()) }
    }
}

/// Checks the os version from the ``os-release`` file, and returns it
/// # Safety
/// If the function fails to get the os version, it returns null
#[unsafe(no_mangle)]
pub extern "C" fn fxkit_get_os_version() -> *const c_char {
    match get_os_version() {
        Ok(version) => unsafe { str_to_cstr(&version) },
        Err(e) => unsafe { str_to_cstr(&e.to_string()) },
    }
}

/// Checks the os id from the ``os-release`` file, and returns it
/// # Safety
/// If the function fails to get the os id, it returns null
#[unsafe(no_mangle)]
pub extern "C" fn fxkit_get_os() -> *const c_char {
    match get_os() {
        Ok(name) => unsafe { str_to_cstr(&name) },
        Err(e) => unsafe { str_to_cstr(&e.to_string()) },
    }
}