trixy 0.4.0

A rust crate used to generate multi-language apis for your application
Documentation
/*
* Copyright (C) 2023 - 2024:
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This file is part of the Trixy crate for Trinitrix.
*
* Trixy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

// Host code
/* Rust API */
#[derive(Debug)]
pub enum Commands {
    Trinitrix(trinitrix::Trinitrix),
}
pub mod trinitrix {
    #[derive(trixy::__private::thiserror::Error)]
    #[allow(non_camel_case_types)]
    #[derive(Debug)]
    pub enum GetBError {
        #[error("Could not do A!")]
        A,
        #[error("Failed on calling b")]
        B,
    }
    impl From<crate::trinitrix_c::GetBError_c> for GetBError {
        fn from(value: crate::trinitrix_c::GetBError_c) -> Self {
            match value {
                crate::trinitrix_c::GetBError_c::A => Self::A,
                crate::trinitrix_c::GetBError_c::B => Self::B,
            }
        }
    }
    #[derive(Debug)]
    pub enum Trinitrix {
        #[allow(non_camel_case_types)]
        get_a {
            trixy_output: trixy::oneshot::Sender<crate::trinitrix_c::Result_u32_String>,
        },
        #[allow(non_camel_case_types)]
        get_b {
            trixy_output: trixy::oneshot::Sender<crate::trinitrix_c::Result_String_GetBError>,
        },
    }
}
/* C API */
// Workaround rust rules when implementing a foreign trait on a foreign type
#[derive(Debug)]
pub struct OurResult<T, E> {
    value: Result<T, E>,
}
impl<T, E> std::ops::Deref for OurResult<T, E> {
    type Target = Result<T, E>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}
impl<T, E> From<Result<T, E>> for OurResult<T, E> {
    fn from(value: Result<T, E>) -> Self {
        Self { value }
    }
}

#[derive(Debug)]
pub struct OurOption<T> {
    value: Option<T>,
}
impl<T> std::ops::Deref for OurOption<T> {
    type Target = Option<T>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T> From<Option<T>> for OurOption<T> {
    fn from(value: Option<T>) -> Self {
        Self { value }
    }
}
#[derive(Debug)]
pub struct OurVec<T> {
    value: Vec<T>,
}
impl<T> std::ops::Deref for OurVec<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T> From<Vec<T>> for OurVec<T> {
    fn from(value: Vec<T>) -> Self {
        Self { value }
    }
}
/* The real C API */
pub mod trinitrix_c {
    #[allow(non_camel_case_types)]
    #[repr(C)]
    pub union Result_u32_String_value {
        ok: std::mem::ManuallyDrop<trixy::types::u32>,
        err: std::mem::ManuallyDrop<trixy::types::String>,
    }
    #[allow(non_camel_case_types)]
    #[repr(C)]
    pub struct Result_u32_String {
        tag: trixy::types::ResultTag,
        value: Result_u32_String_value,
    }
    impl std::fmt::Debug for Result_u32_String {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self.tag {
                trixy::types::ResultTag::Ok => {
                    write!(f, "{:?}", unsafe { &self.value.ok })
                }
                trixy::types::ResultTag::Err => {
                    write!(f, "{:?}", unsafe { &self.value.err })
                }
            }
        }
    }
    impl trixy::types::traits::convert_trait::Convertible
        for crate::OurResult<trixy::types::u32, trixy::types::String>
    {
        type Ptr = Result_u32_String;
        fn into_ptr(self) -> Self::Ptr {
            if self.value.is_ok() {
                Self::Ptr {
                    tag: trixy::types::ResultTag::Ok,
                    value: Result_u32_String_value {
                        ok: std::mem::ManuallyDrop::new(self.value.expect("Is ok")),
                    },
                }
            } else {
                Self::Ptr {
                    tag: trixy::types::ResultTag::Err,
                    value: Result_u32_String_value {
                        err: std::mem::ManuallyDrop::new(self.value.expect_err("Is err")),
                    },
                }
            }
        }
        fn from_ptr(_ptr: Self::Ptr) -> Result<Self, trixy::types::error::TypeConversionError> {
            unreachable!("This should probably not be called?");
        }
    }
    #[allow(non_camel_case_types)]
    #[repr(C)]
    pub union Result_String_GetBError_value {
        ok: std::mem::ManuallyDrop<trixy::types::String>,
        err: std::mem::ManuallyDrop<crate::trinitrix_c::GetBError_c>,
    }
    #[allow(non_camel_case_types)]
    #[repr(C)]
    pub struct Result_String_GetBError {
        tag: trixy::types::ResultTag,
        value: Result_String_GetBError_value,
    }
    impl std::fmt::Debug for Result_String_GetBError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self.tag {
                trixy::types::ResultTag::Ok => {
                    write!(f, "{:?}", unsafe { &self.value.ok })
                }
                trixy::types::ResultTag::Err => {
                    write!(f, "{:?}", unsafe { &self.value.err })
                }
            }
        }
    }
    impl trixy::types::traits::convert_trait::Convertible
        for crate::OurResult<trixy::types::String, crate::trinitrix_c::GetBError_c>
    {
        type Ptr = Result_String_GetBError;
        fn into_ptr(self) -> Self::Ptr {
            if self.value.is_ok() {
                Self::Ptr {
                    tag: trixy::types::ResultTag::Ok,
                    value: Result_String_GetBError_value {
                        ok: std::mem::ManuallyDrop::new(self.value.expect("Is ok")),
                    },
                }
            } else {
                Self::Ptr {
                    tag: trixy::types::ResultTag::Err,
                    value: Result_String_GetBError_value {
                        err: std::mem::ManuallyDrop::new(self.value.expect_err("Is err")),
                    },
                }
            }
        }
        fn from_ptr(_ptr: Self::Ptr) -> Result<Self, trixy::types::error::TypeConversionError> {
            unreachable!("This should probably not be called?");
        }
    }
    #[derive(trixy::__private::thiserror::Error)]
    #[allow(non_camel_case_types)]
    #[repr(C)]
    #[derive(Debug)]
    pub enum GetBError_c {
        #[error("Could not do A!")]
        A,
        #[error("Failed on calling b")]
        B,
    }
    impl From<crate::trinitrix::GetBError> for GetBError_c {
        fn from(value: crate::trinitrix::GetBError) -> Self {
            match value {
                crate::trinitrix::GetBError::A => Self::A,
                crate::trinitrix::GetBError::B => Self::B,
            }
        }
    }
}
#[no_mangle]
pub extern "C" fn trinitrix_get_a(
    output: *mut crate::trinitrix_c::Result_u32_String,
) -> core::ffi::c_int {
    let output_val: crate::trinitrix_c::Result_u32_String = {
        let (tx, rx) = trixy::oneshot::channel();
        let error = std::panic::catch_unwind(|| {
            crate::callback_function(crate::Commands::Trinitrix(
                crate::trinitrix::Trinitrix::get_a { trixy_output: tx },
            ));
            0
        });
        if let Err(_) = error {
            eprintln!("Catched a panic just before the c ffi.");
            std::process::exit(1);
        }
        let recv = rx
            .recv()
            .expect("The channel should not be closed until this value is received");
        recv.into()
    };
    unsafe {
        std::ptr::write(output, output_val);
    }
    return 1;
}
#[no_mangle]
pub extern "C" fn trinitrix_get_b(
    output: *mut crate::trinitrix_c::Result_String_GetBError,
) -> core::ffi::c_int {
    let output_val: crate::trinitrix_c::Result_String_GetBError = {
        let (tx, rx) = trixy::oneshot::channel();
        let error = std::panic::catch_unwind(|| {
            crate::callback_function(crate::Commands::Trinitrix(
                crate::trinitrix::Trinitrix::get_b { trixy_output: tx },
            ));
            0
        });
        if let Err(_) = error {
            eprintln!("Catched a panic just before the c ffi.");
            std::process::exit(1);
        }
        let recv = rx
            .recv()
            .expect("The channel should not be closed until this value is received");
        recv.into()
    };
    unsafe {
        std::ptr::write(output, output_val);
    }
    return 1;
}
// vim: filetype=rust