orchestra-toolkit 0.6.1

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
*
* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* The Orchestra library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

use std::path::PathBuf;
use thiserror::Error;

use crate::{
    constants::IANA_PORT, hgtp::*, AvesterraError, ConvertValueError, String255TooLongError,
};

/// Configuration for the session
/// You can use the default configuration by calling [`SessionConfig::default`],
///
/// ```
/// use orchestra_toolkit::*;
///
/// /// see [`Session`]
/// fn connect() -> anyhow::Result<Session> {
///     let mut config = SessionConfig::default();
///     config.address = "prod.ledr.io".to_string();
///     Session::initialize(config)
/// }
///
/// /// see [`SessionAsync`]
/// async fn connect_async() -> anyhow::Result<SessionAsync> {
///     let mut config = SessionConfig::default();
///     config.address = "prod.ledr.io".to_string();
///     SessionAsync::initialize(config).await
/// }
///
/// ```
///
pub struct SessionConfig {
    pub address: String,
    pub port: u16,
    pub pem_filepath: PathBuf,
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            address: "127.0.0.1".to_string(),
            port: IANA_PORT,
            pem_filepath: PathBuf::from("/AvesTerra/Certificates/avesterra.pem"),
        }
    }
}

pub trait SessionTrait: Sized + Clone {
    fn get_async_session(&self) -> &SessionAsync;
    fn get_socket_pool(&self) -> &HGTPPool;
}

mod asynchronous;
pub use asynchronous::SessionAsync;
mod synchronous;
pub use synchronous::Session;

#[derive(Error, Debug)]
pub enum CallError {
    /// Error communicating with the server
    #[error(transparent)]
    IO(#[from] std::io::Error),

    /// Invalid response from the server
    #[error(transparent)]
    InvalidResponse(#[from] InvalidResponse),

    /// Error returned by the server
    #[error(transparent)]
    Avesterra(#[from] AvesterraError),
}

impl From<UnpackError> for CallError {
    fn from(e: UnpackError) -> Self {
        Self::InvalidResponse(e.into())
    }
}

impl From<ConvertValueError> for CallError {
    fn from(e: ConvertValueError) -> Self {
        Self::InvalidResponse(e.into())
    }
}

#[derive(Error, Debug)]
pub enum InvalidResponse {
    #[error("received invalid HGTP frame: {0}")]
    HGTPFrame(#[from] UnpackError),

    #[error("received invalid value: {0}")]
    ValueTag(#[from] ConvertValueError),

    #[error("failed to deserialize value: {0}")]
    ValueDeser(#[from] serde_json::Error),

    #[error("received invalid name: {0}")]
    Name(#[source] String255TooLongError),

    #[error("received invalid key: {0}")]
    Key(#[source] String255TooLongError),

    #[error("received attribute value not in taxonomy: {0}")]
    Attribute(i64),
}

mod api;
pub use api::*;