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/>.
*/

#[cfg(feature = "macros")]
pub mod macros;
#[cfg(feature = "parser")]
pub mod parser;
#[cfg(feature = "types")]
pub mod types;

// Some re-exports to make the documentation better readable
pub use crate::macros::config::trixy::TrixyConfig;

pub mod __private {
    //! This module contains crates needed for the generated code, it should not be used by humans.
    pub mod thiserror {
        pub use thiserror::*;
    }
}

pub mod oneshot {
    //! This module provides a synced version of tokio's oneshot channel.
    //! Notably this is implemented as wrapper around std's mpsc channel, which closes after one received value
    use std::{
        mem,
        sync::mpsc::{self, RecvError, SendError, TryRecvError},
    };

    #[derive(Clone, Debug)]
    pub struct Sender<T> {
        channel: mpsc::Sender<T>,
    }
    #[derive(Debug)]
    pub struct Receiver<T> {
        channel: mpsc::Receiver<T>,
        last_value: Option<T>,
    }

    pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
        let (tx, rx) = mpsc::channel();
        (
            Sender { channel: tx },
            Receiver {
                channel: rx,
                last_value: None,
            },
        )
    }
    impl<T> Sender<T> {
        pub fn send(&self, input: T) -> Result<(), SendError<T>> {
            self.channel.send(input)
        }
    }
    impl<T> Receiver<T> {
        pub fn try_recv(&mut self) -> Result<bool, TryRecvError> {
            match self.channel.try_recv() {
                Ok(ok) => {
                    self.close();
                    self.last_value = Some(ok);
                    Ok(true)
                }
                Err(err) => Err(err),
            }
        }

        pub fn close(&mut self) {
            let (_, recv) = mpsc::channel();
            let channel = mem::replace(&mut self.channel, recv);
            drop(channel);
        }

        pub fn recv(mut self) -> Result<T, RecvError> {
            match self.channel.recv() {
                Ok(ok) => {
                    self.close();
                    Ok(ok)
                }
                Err(err) => {
                    if let Some(val) = self.last_value {
                        Ok(val)
                    } else {
                        Err(err)
                    }
                }
            }
        }
    }
}