mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 14:40:44 +00:00
use ascii64 encoding
This commit is contained in:
parent
64c7fda201
commit
35dcacb956
4 changed files with 7 additions and 50 deletions
|
@ -47,6 +47,7 @@ tokio-stream = { version = "0.1.12", features = ["sync"], optional = true }
|
|||
futures-util = { version = "0.3.28", optional = true }
|
||||
postcard = { version = "1.0.4", features = ["use-std"] }
|
||||
yazi = "0.1.5"
|
||||
base64 = "0.21.0"
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
dioxus-hot-reload = { path = "../hot-reload" }
|
||||
|
|
|
@ -1,18 +1,11 @@
|
|||
use serde::de::DeserializeOwned;
|
||||
|
||||
use super::u16_from_char;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn serde_from_string<T: DeserializeOwned>(string: &str) -> Option<T> {
|
||||
let decompressed = string
|
||||
.chars()
|
||||
.flat_map(|c| {
|
||||
let u = u16_from_char(c);
|
||||
let u1 = (u >> 8) as u8;
|
||||
let u2 = (u & 0xFF) as u8;
|
||||
[u1, u2].into_iter()
|
||||
})
|
||||
.collect::<Vec<u8>>();
|
||||
let decompressed = STANDARD.decode(string.as_bytes()).ok()?;
|
||||
let (decompressed, _) = yazi::decompress(&decompressed, yazi::Format::Zlib).unwrap();
|
||||
|
||||
postcard::from_bytes(&decompressed).ok()
|
||||
|
|
|
@ -52,34 +52,3 @@ fn serialized_and_deserializes() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encodes_and_decodes_bytes() {
|
||||
for i in 0..(u16::MAX) {
|
||||
let c = u16_to_char(i);
|
||||
let i2 = u16_from_char(c);
|
||||
assert_eq!(i, i2);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn u16_to_char(u: u16) -> char {
|
||||
let u = u as u32;
|
||||
let mapped = if u <= 0xD7FF {
|
||||
u
|
||||
} else {
|
||||
0xE000 + (u - 0xD7FF)
|
||||
};
|
||||
char::from_u32(mapped).unwrap()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn u16_from_char(c: char) -> u16 {
|
||||
let c = c as u32;
|
||||
let mapped = if c <= 0xD7FF {
|
||||
c
|
||||
} else {
|
||||
0xD7FF + (c - 0xE000)
|
||||
};
|
||||
mapped as u16
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use serde::Serialize;
|
||||
|
||||
use super::u16_to_char;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn serde_to_writable<T: Serialize>(
|
||||
|
@ -14,14 +15,7 @@ pub(crate) fn serde_to_writable<T: Serialize>(
|
|||
yazi::CompressionLevel::BestSize,
|
||||
)
|
||||
.unwrap();
|
||||
for array in compressed.chunks(2) {
|
||||
let w = if array.len() == 2 {
|
||||
[array[0], array[1]]
|
||||
} else {
|
||||
[array[0], 0]
|
||||
};
|
||||
write_to.write_char(u16_to_char((w[0] as u16) << 8 | (w[1] as u16)))?;
|
||||
}
|
||||
write_to.write_str(&STANDARD.encode(compressed));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue