-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
224 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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" ] |
2 changes: 2 additions & 0 deletions
2
src/crypto/sig/ed25519/mod.rs → src/algo/crypto/ed25519/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
2 changes: 1 addition & 1 deletion
2
src/crypto/sig/ed25519/readable/mod.rs → src/algo/crypto/ed25519/readable/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters