Skip to content

Commit

Permalink
re-org modules
Browse files Browse the repository at this point in the history
  • Loading branch information
haxjump committed Oct 7, 2024
1 parent 8a7cdc5 commit 08fca37
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 32 deletions.
46 changes: 39 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruc"
version = "6.1.0"
version = "7.0.0"
authors = ["[email protected]"]
edition = "2021"
description = "Rust Util Collections"
Expand All @@ -22,21 +22,53 @@ rand = { version = "0.8", optional = true }
base64 = {version = "0.22.1", optional = true }
hex = {version = "0.4.3", optional = true }

flate2 = {version = "1.0.34", optional = true }

nix = { version = "0.29", features = ["socket"], optional = true }
time = { version = "0.3", features = ["formatting"] }
ssh2 = { version = "0.9.4", optional = true }

serde = { version = "1", features = ["derive"], optional = true }
serde-transcode = { version = "1.1.1", optional = true }
serde_json = { version = "1.0.128", optional = true }
rmp = { package = "rmp-serde", version = "1.3.0", optional = true }

[features]
default = ["ansi"]
default = [ "ansi" ]

full = [ "uau", "cmd", "ssh", "algo", "ende" ]

ansi = []
compact = []
uau = ["nix","rand"]

uau = [ "nix", "rand" ]
cmd = []
ssh = ["ssh2"]
crypto = ["sha3", "ed25519-zebra", "rand", "base64", "hex"]
ser_de = ["serde"]
ssh = [ "ssh2" ]

algo = [
"algo_hash",
"algo_crypto",
"algo_rand",
]

algo_hash = [ "sha3" ]
algo_rand = [ "rand" ]
algo_crypto = [ "ed25519-zebra", "ende_base64", "ende_hex" ]

ende = [
"ende_hex",
"ende_base64",
"ende_compress",
"ende_json",
"ende_msgpack",
"ende_transcode",
]

ende_hex = [ "hex" ]
ende_base64 = [ "base64" ]
ende_compress = [ "flate2" ]

ende_json = [ "serde", "serde_json" ]
ende_msgpack = [ "serde", "rmp" ]

full = ["uau", "cmd", "ssh", "crypto", "ser_de"]
ende_transcode = [ "serde-transcode", "ende_json", "ende_msgpack" ]
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod origin;

#[cfg(feature = "ende_base64")]
pub mod readable;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::origin;
use crate::{crypto::codec::base64, *};
use crate::{ende::base64, *};
use std::fmt;

pub use ed25519_zebra::SigningKey as RawSignKey;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/algo/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(missing_docs)]

#[cfg(feature = "algo_hash")]
pub mod hash;

#[cfg(feature = "algo_crypto")]
pub mod crypto;

#[cfg(feature = "algo_rand")]
pub mod rand;
38 changes: 38 additions & 0 deletions src/algo/rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::ende::hex;
use rand::{thread_rng, RngCore};

#[inline(always)]
pub fn rand_jwt() -> String {
let mut data = [0u8; 32];
thread_rng().fill_bytes(&mut data);
hex::encode(data)
}

#[inline(always)]
pub fn rand_jwt_n(n: usize) -> String {
hex::encode(rand_data(n))
}

#[inline(always)]
pub fn rand_data(len: usize) -> Vec<u8> {
let mut data = vec![0u8; len];
thread_rng().fill_bytes(&mut data);
data
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn t_rand() {
let jwt = rand_jwt();
assert_eq!(jwt.len(), 64);

let jwt = rand_jwt_n(193);
assert_eq!(jwt.len(), 2 * 193);

let data = rand_data(121);
assert_eq!(data.len(), 121);
}
}
2 changes: 0 additions & 2 deletions src/crypto/codec/mod.rs

This file was deleted.

19 changes: 0 additions & 19 deletions src/crypto/mod.rs

This file was deleted.

File renamed without changes.
30 changes: 30 additions & 0 deletions src/ende/compress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::*;
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
use std::io::{Read, Write};

pub fn zlib_compress(inputs: &[u8]) -> Result<Vec<u8>> {
let mut en = ZlibEncoder::new(Vec::new(), Compression::default());
en.write_all(inputs)
.c(d!())
.and_then(|_| en.finish().c(d!()))
}

pub fn zlib_uncompress(inputs: &[u8]) -> Result<Vec<u8>> {
let mut de = ZlibDecoder::new(inputs);
let mut res = vec![];
de.read_to_end(&mut res).c(d!()).map(|_| res)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "algo_rand")]
fn t_compress_uncompress() {
let data = crate::algo::rand::rand_data(128);
let compressed = zlib_compress(&data).unwrap();
let uncompressed = zlib_uncompress(&compressed).unwrap();
assert_eq!(uncompressed, data);
}
}
File renamed without changes.
18 changes: 18 additions & 0 deletions src/ende/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::*;
use serde::{Deserialize, Serialize};

#[inline(always)]
pub fn json_encode<T>(t: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
serde_json::to_vec(&t).c(d!())
}

#[inline(always)]
pub fn json_decode<T>(bytes: &[u8]) -> Result<T>
where
T: for<'a> Deserialize<'a>,
{
serde_json::from_slice(bytes).c(d!())
}
19 changes: 19 additions & 0 deletions src/ende/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![allow(missing_docs)]

#[cfg(feature = "ende_hex")]
pub mod hex;

#[cfg(feature = "ende_base64")]
pub mod base64;

#[cfg(feature = "ende_compress")]
pub mod compress;

#[cfg(feature = "ende_json")]
pub mod json;

#[cfg(feature = "ende_msgpack")]
pub mod msgpack;

#[cfg(feature = "ende_transcode")]
pub mod transcode;
18 changes: 18 additions & 0 deletions src/ende/msgpack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::*;
use serde::{Deserialize, Serialize};

#[inline(always)]
pub fn encode<T>(t: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
rmp::to_vec(&t).c(d!())
}

#[inline(always)]
pub fn decode<T>(bytes: &[u8]) -> Result<T>
where
T: for<'a> Deserialize<'a>,
{
rmp::from_slice(bytes).c(d!())
}
43 changes: 43 additions & 0 deletions src/ende/transcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::*;
use std::io::Write;

pub fn convert_json_to_msgpack(json: &[u8]) -> Result<Vec<u8>> {
let mut ret = vec![];

let mut deserializer = serde_json::Deserializer::from_slice(json);
let mut serializer = rmp::Serializer::new(&mut ret);

serde_transcode::transcode(&mut deserializer, &mut serializer)
.c(d!())
.and_then(|_| serializer.into_inner().flush().c(d!()))
.map(|_| ret)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ende::msgpack;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
struct Foo {
a: u32,
b: String,
c: (),
d: Option<Vec<u8>>,
e: bool,
}

#[test]
fn t_convert_json_to_msgpack() {
let foo = pnk!(do_convert_json_to_msgpack());
assert_eq!(foo, Foo::default());
}

fn do_convert_json_to_msgpack() -> Result<Foo> {
let json_str = serde_json::to_string(&Foo::default()).c(d!())?;
let msgpack_bytes =
super::convert_json_to_msgpack(json_str.as_bytes()).c(d!())?;
msgpack::decode(&msgpack_bytes).c(d!())
}
}
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub mod ssh;
#[cfg(target_os = "linux")]
pub mod uau;

pub use err::*;
pub mod algo;

pub mod ende;

#[cfg(feature = "crypto")]
pub mod crypto;
/////////////////////////////////////////////

pub use err::*;

0 comments on commit 08fca37

Please sign in to comment.