move read_u32 and read_string lambdas back to where they must to be

This commit is contained in:
MeexReay 2025-06-18 09:14:54 +03:00
parent ae9fb60eab
commit d3bb035fda
2 changed files with 22 additions and 15 deletions

View File

@ -18,7 +18,7 @@ use rand::{Rng, distr::Alphanumeric};
use crate::{ use crate::{
Args, Args,
util::{format_message, sanitize_text}, util::{format_message, read_string, read_u32, sanitize_text},
}; };
fn load_accounts(accounts_file: Option<String>) -> Vec<Account> { fn load_accounts(accounts_file: Option<String>) -> Vec<Account> {
@ -214,24 +214,11 @@ impl Account {
pub fn from_bytes(text: Vec<u8>) -> Result<Self, Box<dyn Error>> { pub fn from_bytes(text: Vec<u8>) -> Result<Self, Box<dyn Error>> {
let mut cursor = Cursor::new(text); let mut cursor = Cursor::new(text);
let read_u32 = |cursor: &mut Cursor<Vec<u8>>| -> Result<u32, Box<dyn Error>> {
let mut buf = [0; 4];
cursor.read_exact(&mut buf)?;
Ok(u32::from_le_bytes(buf))
};
let name_len = read_u32(&mut cursor)? as usize; let name_len = read_u32(&mut cursor)? as usize;
let salt_len = read_u32(&mut cursor)? as usize; let salt_len = read_u32(&mut cursor)? as usize;
let addr_len = read_u32(&mut cursor)? as usize; let addr_len = read_u32(&mut cursor)? as usize;
let pass_len = read_u32(&mut cursor)? as usize; let pass_len = read_u32(&mut cursor)? as usize;
let read_string = |cursor: &mut Cursor<Vec<u8>>, len: usize| -> Result<String, Box<dyn Error>> {
let mut buf = vec![0; len];
cursor.read_exact(&mut buf)?;
String::from_utf8(buf).map_err(|e| e.into())
};
let name = read_string(&mut cursor, name_len)?; let name = read_string(&mut cursor, name_len)?;
let salt = read_string(&mut cursor, salt_len)?; let salt = read_string(&mut cursor, salt_len)?;
let addr = read_string(&mut cursor, addr_len)?; let addr = read_string(&mut cursor, addr_len)?;

View File

@ -1,3 +1,5 @@
use std::{error::Error, io::Read};
use colored::{Color, Colorize}; use colored::{Color, Colorize};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
@ -94,3 +96,21 @@ pub fn find_username_color(message: &str) -> Option<(String, String, Color)> {
} }
None None
} }
pub fn read_u32<R>(cursor: &mut R) -> Result<u32, Box<dyn Error>>
where
R: Read,
{
let mut buf = [0; 4];
cursor.read_exact(&mut buf)?;
Ok(u32::from_le_bytes(buf))
}
pub fn read_string<R>(cursor: &mut R, len: usize) -> Result<String, Box<dyn Error>>
where
R: Read,
{
let mut buf = vec![0; len];
cursor.read_exact(&mut buf)?;
String::from_utf8(buf).map_err(|e| e.into())
}