1
0
Files
wfassoc/wfassoc-cdylib/src/lib.rs

212 lines
4.7 KiB
Rust
Raw Normal View History

2026-05-10 23:16:56 +08:00
mod cstr_ffi;
mod last_error;
mod object_pool;
2026-05-10 16:43:25 +08:00
2026-05-10 23:16:56 +08:00
use object_pool::ObjectPool;
use std::sync::{LazyLock, RwLock};
use thiserror::Error as TeError;
2026-05-10 23:16:56 +08:00
use wfassoc::highlevel::{Program, Schema};
pub use cstr_ffi::CStyleString;
2026-05-10 23:16:56 +08:00
pub use object_pool::Token;
2025-11-26 22:40:17 +08:00
// region: Error
/// Error occurs in this crate.
#[derive(Debug, TeError)]
enum Error {
2026-05-10 16:55:09 +08:00
/// Error when operating Schema.
#[error("{0}")]
Schema(#[from] wfassoc::highlevel::SchemaError),
2026-05-09 16:27:04 +08:00
/// Error when parsing Schema into Program.
#[error("{0}")]
2026-05-09 16:27:04 +08:00
ParseProgram(#[from] wfassoc::highlevel::ParseProgramError),
/// Error when operating Program.
#[error("{0}")]
Program(#[from] wfassoc::highlevel::ProgramError),
2026-05-10 16:55:09 +08:00
/// Error when manipulating with C-style string.
#[error("{0}")]
CStrFfi(#[from] cstr_ffi::Error),
2026-05-10 23:16:56 +08:00
/// Error when manipulating with object pool.
#[error("{0}")]
ObjectPool(#[from] object_pool::Error),
/// Error when manipulating with poison RwLock
#[error("RwLock is poisoning")]
PoisonRwLock,
}
/// Result type used in this crate.
type Result<T> = std::result::Result<T, Error>;
// endregion
// region: Macros
macro_rules! in_param_ty {
($t:ty) => {
$t
};
}
macro_rules! out_param_ty {
($t:ty) => {
*mut $t
};
}
macro_rules! set_out_param {
($lhs:expr, $rhs:expr) => {
unsafe { *$lhs = $rhs };
};
}
macro_rules! pull_reader {
($pool:expr) => {
$pool.read().map_err(|_| Error::PoisonRwLock)
};
}
macro_rules! pull_writer {
($pool:expr) => {
$pool.write().map_err(|_| Error::PoisonRwLock)
};
}
// endregion
2026-05-10 23:16:56 +08:00
// region: Exposed Functions
// region: Facilities
2025-10-18 09:55:08 +08:00
#[unsafe(no_mangle)]
pub extern "C" fn WFStartup() -> bool {
// TODO: Initialize all pool by fetching writer from them
2026-05-10 16:55:09 +08:00
true
}
#[unsafe(no_mangle)]
pub extern "C" fn WFShutdown() -> bool {
// TODO: Free all pool stored objects
2026-05-10 16:55:09 +08:00
true
}
#[unsafe(no_mangle)]
pub extern "C" fn WFGetLastError() -> CStyleString {
2025-11-26 22:40:17 +08:00
last_error::get_last_error()
}
#[unsafe(no_mangle)]
pub extern "C" fn WFHasPrivilege() -> bool {
2026-05-09 16:27:04 +08:00
wfassoc::win32::utilities::has_privilege()
}
2026-05-10 23:16:56 +08:00
// endregion
// region: Schema
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
2026-05-10 23:16:56 +08:00
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
2026-05-10 23:16:56 +08:00
fn inner() -> Result<Token> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
2026-05-10 23:16:56 +08:00
Ok(pool.allocate(Schema::new())?)
}
match inner() {
Ok(rv) => {
set_out_param!(out_schema, rv);
last_error::clear_last_error();
2026-05-10 23:16:56 +08:00
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaSetIdentifier(
in_schema: in_param_ty!(Token),
in_value: in_param_ty!(CStyleString),
) -> bool {
fn inner(in_schema: Token, in_value: CStyleString) -> Result<()> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
let schema = pool.get_mut(in_schema)?;
schema.set_identifier(cstr_ffi::parse_ffi_string(in_value)?);
Ok(())
}
match inner(in_schema, in_value) {
Ok(_) => {
last_error::clear_last_error();
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
2026-05-10 23:16:56 +08:00
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaIntoProgram(
in_schema: in_param_ty!(Token),
out_program: out_param_ty!(Token),
) -> bool {
2026-05-10 23:16:56 +08:00
fn inner(in_token: Token) -> Result<Token> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
2026-05-10 23:16:56 +08:00
let schema = pool.pop(in_token)?;
let mut pool = pull_writer!(PROGRAM_POOL)?;
2026-05-10 23:16:56 +08:00
let program = schema.into_program()?;
Ok(pool.allocate(program)?)
}
match inner(in_schema) {
2026-05-10 23:16:56 +08:00
Ok(rv) => {
set_out_param!(out_program, rv);
last_error::clear_last_error();
2026-05-10 23:16:56 +08:00
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
2026-05-10 23:16:56 +08:00
fn inner(in_token: Token) -> Result<()> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
2026-05-10 23:16:56 +08:00
Ok(pool.free(in_token)?)
}
match inner(in_schema) {
Ok(_) => {
last_error::clear_last_error();
true
}
2026-05-10 23:16:56 +08:00
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
// endregion
// region: Program
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
2026-05-10 23:16:56 +08:00
LazyLock::new(|| RwLock::new(ObjectPool::new()));
// endregion
// endregion