feat: cmd info works

This commit is contained in:
2023-02-12 15:17:33 +08:00
parent cfa66dabaf
commit ff89bf6711
4 changed files with 55 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ use rust_util::{opt_result, simple_error, XResult};
use crate::spec::TinyEncryptMeta;
fn write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta) -> XResult<usize> {
pub fn write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta) -> XResult<usize> {
let meta_json = opt_result!( serde_json::to_string(meta), "Meta to JSON failed: {}");
let meta_json_bytes = meta_json.as_bytes();
let meta_json_bytes_len = meta_json_bytes.len();
@@ -17,7 +17,7 @@ fn write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta) -> XResu
Ok(meta_json_bytes_len + 2 + 4)
}
fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
pub fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
let mut tag_buff = [0_u8; 2];
opt_result!(r.read_exact(&mut tag_buff), "Read tag failed: {}");
let tag = u16::from_be_bytes(tag_buff);
@@ -32,8 +32,8 @@ fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
return simple_error!("Meta too large: {}", length);
}
let mut meta_buff = Vec::with_capacity(length as usize);
opt_result!( r.read_exact(meta_buff.as_mut_slice()), "Read meta failed: {}");
let mut meta_buff = vec![0; length as usize];
opt_result!(r.read_exact(meta_buff.as_mut_slice()), "Read meta failed: {}");
Ok(opt_result!(serde_json::from_slice(&meta_buff), "Parse meta failed: {}"))
}