pt_err 0.0.0-alpha.4

A library for Rust-Based Pt Errors
// File: rust_pt\pt_err\src\lib.rs
// Directory: rust_pt\pt_err\src
// Filename: lib.rs
//======================================================================
#![doc = include_str!("../../../../README.md")]
// @@ begin lint list
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
#![allow(clippy::cognitive_complexity)] // See arti#2556
#![allow(clippy::collapsible_if)] // See arti#2342
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
#![allow(clippy::needless_lifetimes)] // See arti#1765
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
#![allow(clippy::uninlined_format_args)]
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
#![warn(noop_method_call)]
#![warn(unreachable_pub)]
#![warn(clippy::manual_ok_or)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_pass_by_value)]
#![warn(clippy::option_option)]
#![warn(clippy::rc_buffer)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::all)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
#![deny(clippy::mod_module_files)]
#![deny(clippy::print_stderr)]
#![deny(clippy::print_stdout)]
#![deny(clippy::ref_option_ref)]
#![deny(clippy::string_slice)] // See arti#2571
#![deny(clippy::unchecked_time_subtraction)]
#![deny(clippy::unnecessary_wraps)]
#![deny(clippy::unused_async)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::pedantic)] // This is not in Arti
//! <!-- @@ end lint list
#[doc(hidden)]
pub use derive_deftly;
use derive_deftly::Deftly;
use thiserror::Error;
/// Diffrent macros for Error
mod macros;
/// Errors which is related with `ExtOrPort`
#[derive(Debug, Error)]
#[non_exhaustive]
#[allow(unused)] // We will need these in future
pub enum ExtOrPortError {
    /// The stream is `None`. Therefore, we cannot receive the server's reply
    #[error("The stream is `None`. Therefore, we cannot receive the server's reply")]
    StreamMissing,
    /// The collection of `AuthType` is greater than limit
    #[error("AuthTypes is greater than limit:{0}")]
    AuthTypesTooMuch(String),
    /// The `StaticHeader` is invalid
    #[error("Invalid StaticHeader it should be \"! Extended ORPort Auth Cookie !\x0a\"")]
    InvalidStaticHeader,
    /// Unsupported auth types
    /// We have not found the supported auth types
    #[error("Unsupported auth types: {0}")]
    UnsupportedAuthTypes(String),
    /// The server closed the connection during the `ExtOrPort` handshake.
    ///
    /// This is usually detected when the underlying tokio read returns 0 (EOF),
    /// indicating the peer has closed its side.
    ///
    /// The `String` field should contain more informations
    ///
    /// Caller must not ignore this Error
    #[error("server closed the connection during the ExtOrPort handshake: {0}")]
    ServerClosedConnection(String),
    /// Invalid USERADDR
    /// Used for [0x0001] USERADDR: an address:port string that represents the
    /// client's address.
    #[error("Invalid USERADDR: {0}")]
    InvalidUserAddr(String),
    /// `EndAuthType` Unfound
    #[error("EndAuthTypeUnfound")]
    EndAuthTypeUnfoundWithCandidates(Vec<u8>),
    /// `EndAuthType` Unfound
    #[error("EndAuthTypeUnfound")]
    EndAuthTypeUnfound,
    /// Server returns an invalid message
    #[error("Invalid Server Message: {0}")]
    InvalidServerMsg(String),
    /// Invalid server hash, client must terminate the connection
    #[error("Invalid server hash")]
    InvalidServerHash,
    /// Insufficient cookie file length
    #[error("Insufficient cookie file length")]
    InsufficientCookieFile,
    /// Server sent a message which indicates that Client hash validation failed
    #[error("Server sent a message which indicates that Client hash validation failed")]
    InvalidClientHash,
    /// PT name is longer than 65535 bytes
    #[error("PT name is longer than 65535 bytes: {0}")]
    PtNameTooLong(String),
    /// Io Error
    #[error(transparent)]
    Io(#[from] std::io::Error),
    /// Other errors
    /// Can be converted from `anyhow::Error`
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

/// Errors for `pt_core::Pt`
#[derive(Debug, Error, Deftly)]
#[derive_deftly(OtherFromError)]
#[non_exhaustive]
pub enum PtError {
    /// The config is poisoned,
    /// this error is crusial
    /// User must not ignore it
    #[error("Config poisoned: {0}")]
    PtConfigPoisoned(String),
    /// Client side's Tor servers, do not support `OrPort` or `ExtOrPort`,
    /// you can only connect to them when you are in the server side
    #[error("ORPort and ExtORPort are only provided by a server-side Tor")]
    ClientOrPortUnavailable,
    /// Pt is never inited
    #[error("PtNotInitialized {0}")]
    PtNotInitialized(String),
    /// `ExtOrPort` is not set
    #[error("ExtOrPort unfound {0}")]
    ExtOrPortNotAvailable(String),
    /// Error for `ExtOrPortError`
    #[error("ExtOrPortError: {0}")]
    ExtOrPort(#[from] ExtOrPortError),
    /// User called some feature that needs `ExtOrPort` connection be ready
    #[error("Connection is not ExtOrPort or not inited: {0}")]
    NotExtOrPort(String),
    /// User called some feature that needs `OrPort` connection be ready
    #[error("Connection is not OrPort or not inited: {0}")]
    NotOrPort(String),
    /// Expect in the server, but actually in the client side
    #[error("Not in server: {0}")]
    NotInServer(String),
    /// Expect in the client, but actually in the server side
    #[error("Not in client: {0}")]
    NotInClient(String),
    /// Other errors
    #[error("{0}")]
    Other(String),
}
/// Errors which could cause during the Config parsing.
#[derive(Debug, Error, PartialEq, Deftly)]
#[derive_deftly(OtherFromError)]
#[non_exhaustive]
pub enum ConfigError {
    /// Configuration error with additional context.
    /// # Example
    /// ```rust
    /// use pt_err::ConfigError;
    /// fn main() {
    ///     assert_eq!(ConfigError::InvalidConfigErr {
    ///         message:"I want to eat radishes".to_string()
    ///     }.to_string(),
    ///     "Invalid Config: I want to eat radishes")
    /// }
    /// ```
    #[error("Invalid Config: {message}")]
    InvalidConfigErr {
        /// Additional context of the Error
        message: String,
    },
    /// Configuration error caused by unsupported version
    #[error("Unsupported Version {message}")]
    UnsupportedVer {
        /// Additional context of the Error
        message: String,
    },
    /// Other errors
    /// Can be converted from `anyhow::Error`
    #[error("Error: {0}")]
    Other(String),
}
/// Errors which could cause by variant
#[derive(Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum VariantError {
    /// Error indicates that the Variant hasn't found
    /// # Example
    /// ```rust
    /// use pt_err::VariantError;
    /// fn main() {
    ///     let _ = VariantError::UnfoundError {message:"I want to eat radishes".to_string()};
    /// }
    /// ```
    #[error("Variant Unfound {message}")]
    UnfoundError {
        /// Additional context of the Error
        message: String,
    },
}

/// Error for socks
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SocksError {
    /// Socks request has an unsupported command (CMD)
    #[error("unsupported CMD: {0:?}")]
    CommandNotSupported(Option<u8>),
    /// Socks request has an unsupported address type (ATYP)
    #[error("unsupported ATYP: {0:?}")]
    AddrTypeNotSupported(Option<u8>),
    /// Socks request has a non-zero RSV; reply with `0x01`.
    #[error("RSV must be 0x00, got {0:?}")]
    BadReserved(Option<u8>),
    /// Connection Setting poisoned
    #[error("Socks connection setting poisoned: {0}")]
    PoisonedSetting(String),
    /// Socks version unfound from the setting
    #[error("Socks version unfound from the setting")]
    SocksVerUnfound,
    /// The socks version is not matching
    #[error("Version does not match with before")]
    SocksVerNoMatching,
    /// There is not supported `auth_methods`
    #[error("There is not supported auth_methods")]
    NoSupportedAuthMethod,
    /// Socks message has invalid format
    #[error("Socks message has invalid format: {0}")]
    InvalidFormMsg(String),
    /// Unsupported socks version
    #[error("unsupported socks version")]
    UnsupportedVersion,
    /// Variant Error
    #[error("variant error")]
    VariantError(#[from] VariantError),
    /// Std io error
    #[error("std io error: {0}")]
    Io(#[from] std::io::Error),
    /// Other Errors
    #[error("Error: {0}")]
    Other(#[from] anyhow::Error),
}