Add shared lib
This commit is contained in:
parent
7d04f3c85c
commit
006d9d9160
3 changed files with 58 additions and 0 deletions
9
backend/sync_lib/Cargo.toml
Normal file
9
backend/sync_lib/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "sync_lib"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
|
||||
thiserror = {workspace = true}
|
||||
24
backend/sync_lib/src/errors.rs
Normal file
24
backend/sync_lib/src/errors.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use base64::DecodeError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SyncLibError {
|
||||
#[error("Base64 decoding error: {}", .reason)]
|
||||
DecodingError { reason: String },
|
||||
}
|
||||
|
||||
impl From<DecodeError> for SyncLibError {
|
||||
fn from(e: DecodeError) -> Self {
|
||||
SyncLibError::DecodingError {
|
||||
reason: e.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for SyncLibError {
|
||||
fn from(e: std::string::FromUtf8Error) -> Self {
|
||||
SyncLibError::DecodingError {
|
||||
reason: e.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
25
backend/sync_lib/src/lib.rs
Normal file
25
backend/sync_lib/src/lib.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use base64::{Engine as _, engine::general_purpose::STANDARD_NO_PAD};
|
||||
use errors::SyncLibError;
|
||||
|
||||
pub mod errors;
|
||||
|
||||
pub fn bytes_to_base64(input: &[u8]) -> String {
|
||||
STANDARD_NO_PAD.encode(input)
|
||||
}
|
||||
|
||||
pub fn string_to_base64(input: &str) -> String {
|
||||
bytes_to_base64(input.as_bytes())
|
||||
}
|
||||
|
||||
pub fn base64_to_bytes(input: &str) -> Result<Vec<u8>, SyncLibError> {
|
||||
STANDARD_NO_PAD.decode(input).map_err(SyncLibError::from)
|
||||
}
|
||||
|
||||
pub fn base64_to_string(input: &str) -> Result<String, SyncLibError> {
|
||||
let bytes = base64_to_bytes(input)?;
|
||||
String::from_utf8(bytes).map_err(SyncLibError::from)
|
||||
}
|
||||
|
||||
pub fn is_binary(data: &[u8]) -> bool {
|
||||
data.iter().any(|&b| b == 0)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue