Skip to main content

stealth_lib/
utils.rs

1//! Legacy utilities module for backwards compatibility.
2//!
3//! # Deprecated
4//!
5//! This module is deprecated. Use [`crate::error`] and [`crate::encoding`] instead.
6//!
7//! ## Migration
8//!
9//! ```ignore
10//! // Old code:
11//! use stealth_lib::utils::SolanaError;
12//!
13//! // New code:
14//! use stealth_lib::Error;
15//! ```
16
17#![allow(missing_docs)]
18#![allow(deprecated)]
19
20use std::fmt::Display;
21
22/// Legacy error type.
23///
24/// # Deprecated
25///
26/// Use [`crate::Error`] instead.
27#[deprecated(since = "1.0.0", note = "Use crate::Error instead")]
28#[derive(Debug, PartialEq)]
29pub struct SolanaError {
30    error_msg: String,
31    error_name: String,
32    #[allow(unused)]
33    error_code_number: u32,
34    #[allow(unused)]
35    error_origin: Option<String>,
36    #[allow(unused)]
37    compared_values: Option<String>
38}
39
40impl Display for SolanaError {
41    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42        write!(f, "Error: {} - {}", self.error_name, self.error_msg)
43    }
44}
45
46impl std::error::Error for SolanaError {}
47
48/// Creates a legacy error.
49///
50/// # Deprecated
51///
52/// Use [`crate::Error`] variants instead.
53#[deprecated(since = "1.0.0", note = "Use crate::Error variants instead")]
54pub fn err(msg: &str) -> SolanaError {
55    SolanaError {
56        error_msg: msg.to_string(),
57        error_name: "Exception".to_string(),
58        error_code_number: 0,
59        error_origin: None,
60        compared_values: None
61    }
62}
63
64/// Converts a `Vec<u8>` to `u128`.
65///
66/// # Deprecated
67///
68/// Use [`crate::encoding::hex_utils::bytes_to_u128`] instead.
69///
70/// # Panics
71///
72/// Panics if the vector is not exactly 16 bytes.
73#[deprecated(since = "1.0.0", note = "Use crate::encoding::hex_utils::bytes_to_u128 instead")]
74pub fn vec_to_u128(vec: &[u8]) -> u128 {
75    let mut array = [0u8; 16];
76    array.copy_from_slice(vec);
77    u128::from_be_bytes(array)
78}
79
80/// Converts bytes to binary representation.
81///
82/// # Deprecated
83///
84/// This function is deprecated and will be removed in a future version.
85#[deprecated(since = "1.0.0", note = "Will be removed in 2.0")]
86pub fn bytes_to_binary(i: &[u8; 32], r: &mut Vec<u8>) {
87    for m in i.iter() {
88        format!("{:8b}", m).chars().for_each(|b| if b == '1' { r.push(1); } else { r.push(0) } );
89    }
90}
91
92// Conversion from legacy error to new error
93impl From<SolanaError> for crate::Error {
94    fn from(err: SolanaError) -> Self {
95        crate::Error::ParseError(err.error_msg)
96    }
97}