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 {
    Test(test::Test),
}
pub mod test {
    /// This struct will be used later on (and this is also a struct doc comment test)
    #[allow(non_camel_case_types)]
    #[derive(Debug)]
    pub struct Callback {
        /// Very important field
        pub func: extern "C" fn(trixy::types::String) -> trixy::types::String,
        /// Very important field for keeping time constant
        pub timeout: u32,
    }
    impl trixy::types::traits::convert_trait::Convertible for Callback {
        type Ptr = crate::test_c::Callback_c;
        fn into_ptr(self) -> Self::Ptr {
            Self::Ptr {
                func: self.func.into(),
                timeout: self.timeout.into(),
            }
        }
        fn from_ptr(_ptr: Self::Ptr) -> Result<Self, trixy::types::error::TypeConversionError> {
            todo!()
        }
    }
    impl TryFrom<crate::test_c::Callback_c> for Callback {
        type Error = trixy::types::error::TypeConversionError;
        fn try_from(value: crate::test_c::Callback_c) -> Result<Self, Self::Error> {
            Ok(Self {
                func: value.func.try_into()?,
                timeout: value.timeout.try_into()?,
            })
        }
    }
    /// Same thing as above (and a enum doc comment test)
    #[allow(non_camel_case_types)]
    #[derive(Debug)]
    pub enum CallbackPriority {
        /// Callback **now**
        High,
        /// Maybe do a callback
        Medium,
        /// Calling back is ..
        /// really not important ..
        /// like not even think about doing it
        Low,
    }
    impl From<crate::test_c::CallbackPriority_c> for CallbackPriority {
        fn from(value: crate::test_c::CallbackPriority_c) -> Self {
            match value {
                crate::test_c::CallbackPriority_c::High => Self::High,
                crate::test_c::CallbackPriority_c::Medium => Self::Medium,
                crate::test_c::CallbackPriority_c::Low => Self::Low,
            }
        }
    }
    #[derive(Debug)]
    pub enum Test {
        #[allow(non_camel_case_types)]
        execute_callback {
            callback: crate::test::Callback,
            priority: crate::test::CallbackPriority,
        },
    }
}
/* 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 test_c {
    /// Same thing as above (and a enum doc comment test)
    #[allow(non_camel_case_types)]
    #[repr(C)]
    #[derive(Debug)]
    pub enum CallbackPriority_c {
        /// Callback **now**
        High,
        /// Maybe do a callback
        Medium,
        /// Calling back is ..
        /// really not important ..
        /// like not even think about doing it
        Low,
    }
    impl From<crate::test::CallbackPriority> for CallbackPriority_c {
        fn from(value: crate::test::CallbackPriority) -> Self {
            match value {
                crate::test::CallbackPriority::High => Self::High,
                crate::test::CallbackPriority::Medium => Self::Medium,
                crate::test::CallbackPriority::Low => Self::Low,
            }
        }
    }
    /// This struct will be used later on (and this is also a struct doc comment test)
    #[allow(non_camel_case_types)]
    #[repr(C)]
    #[derive(Debug)]
    pub struct Callback_c {
        /// Very important field
        pub func: extern "C" fn(trixy::types::String) -> trixy::types::String,
        /// Very important field for keeping time constant
        pub timeout: trixy::types::u32,
    }
    impl TryFrom<crate::test::Callback> for Callback_c {
        type Error = trixy::types::error::TypeConversionError;
        fn try_from(value: crate::test::Callback) -> Result<Self, Self::Error> {
            Ok(Self {
                func: value.func.try_into()?,
                timeout: value.timeout.try_into()?,
            })
        }
    }
}
#[no_mangle]
pub extern "C" fn test_execute_callback(
    callback: crate::test_c::Callback_c,
    priority: crate::test_c::CallbackPriority_c,
) -> core::ffi::c_int {
    let error = std::panic::catch_unwind(|| {
        crate::callback_function(crate::Commands::Test(crate::test::Test::execute_callback {
            callback: match callback.try_into() {
                Ok(ok) => ok,
                Err(err) => {
                    trixy::types::traits::errno::set(err.into());
                    return 0;
                }
            },
            priority: match priority.try_into() {
                Ok(ok) => ok,
                Err(err) => {
                    trixy::types::traits::errno::set(err.into());
                    return 0;
                }
            },
        }));
        0
    });
    if let Err(_) = error {
        eprintln!("Catched a panic just before the c ffi.");
        std::process::exit(1);
    }
    return 1;
}
// vim: filetype=rust