orchestra_toolkit/
session.rs

1/* Copyright 2024-2025 LEDR Technologies Inc.
2* 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.
3*
4* 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.
5*
6* 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.
7*
8* 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/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::path::PathBuf;
14use thiserror::Error;
15
16use crate::{
17    constants::IANA_PORT, hgtp::*, AvesterraError, ConvertValueError, String255TooLongError,
18};
19
20/// Configuration for the session
21/// You can use the default configuration by calling [`SessionConfig::default`],
22///
23/// ```
24/// use orchestra_toolkit::*;
25///
26/// /// see [`Session`]
27/// fn connect() -> anyhow::Result<Session> {
28///     let mut config = SessionConfig::default();
29///     config.address = "prod.ledr.io".to_string();
30///     Session::initialize(config)
31/// }
32///
33/// /// see [`SessionAsync`]
34/// async fn connect_async() -> anyhow::Result<SessionAsync> {
35///     let mut config = SessionConfig::default();
36///     config.address = "prod.ledr.io".to_string();
37///     SessionAsync::initialize(config).await
38/// }
39///
40/// ```
41///
42pub struct SessionConfig {
43    pub address: String,
44    pub port: u16,
45    pub pem_filepath: PathBuf,
46}
47
48impl Default for SessionConfig {
49    fn default() -> Self {
50        Self {
51            address: "127.0.0.1".to_string(),
52            port: IANA_PORT,
53            pem_filepath: PathBuf::from("/AvesTerra/Certificates/avesterra.pem"),
54        }
55    }
56}
57
58pub trait SessionTrait: Sized + Clone {
59    fn get_async_session(&self) -> &SessionAsync;
60    fn get_socket_pool(&self) -> &HGTPPool;
61}
62
63mod asynchronous;
64pub use asynchronous::SessionAsync;
65mod synchronous;
66pub use synchronous::Session;
67
68#[derive(Error, Debug)]
69pub enum CallError {
70    /// Error communicating with the server
71    #[error(transparent)]
72    IO(#[from] std::io::Error),
73
74    /// Invalid response from the server
75    #[error(transparent)]
76    InvalidResponse(#[from] InvalidResponse),
77
78    /// Error returned by the server
79    #[error(transparent)]
80    Avesterra(#[from] AvesterraError),
81}
82
83impl From<UnpackError> for CallError {
84    fn from(e: UnpackError) -> Self {
85        Self::InvalidResponse(e.into())
86    }
87}
88
89impl From<ConvertValueError> for CallError {
90    fn from(e: ConvertValueError) -> Self {
91        Self::InvalidResponse(e.into())
92    }
93}
94
95#[derive(Error, Debug)]
96pub enum InvalidResponse {
97    #[error("received invalid HGTP frame: {0}")]
98    HGTPFrame(#[from] UnpackError),
99
100    #[error("received invalid value: {0}")]
101    ValueTag(#[from] ConvertValueError),
102
103    #[error("failed to deserialize value: {0}")]
104    ValueDeser(#[from] serde_json::Error),
105
106    #[error("received invalid name: {0}")]
107    Name(#[source] String255TooLongError),
108
109    #[error("received invalid key: {0}")]
110    Key(#[source] String255TooLongError),
111
112    #[error("received attribute value not in taxonomy: {0}")]
113    Attribute(i64),
114}
115
116mod api;
117pub use api::*;