refactor: better error handling
This commit is contained in:
		
							parent
							
								
									80935c5826
								
							
						
					
					
						commit
						2368a90584
					
				
					 15 changed files with 2062 additions and 1909 deletions
				
			
		
							
								
								
									
										32
									
								
								src/utils.rs
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								src/utils.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,4 +1,3 @@
 | 
			
		|||
use crate::Result;
 | 
			
		||||
use argon2::{Config, Variant};
 | 
			
		||||
use rand::prelude::*;
 | 
			
		||||
use std::{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,39 +8,38 @@ use std::{
 | 
			
		|||
pub fn millis_since_unix_epoch() -> u64 {
 | 
			
		||||
    SystemTime::now()
 | 
			
		||||
        .duration_since(UNIX_EPOCH)
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .expect("time is valid")
 | 
			
		||||
        .as_millis() as u64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
 | 
			
		||||
    let number = match old {
 | 
			
		||||
        Some(bytes) => {
 | 
			
		||||
            let array: [u8; 8] = bytes.try_into().unwrap();
 | 
			
		||||
            let number = u64::from_be_bytes(array);
 | 
			
		||||
    let number = match old.map(|bytes| bytes.try_into()) {
 | 
			
		||||
        Some(Ok(bytes)) => {
 | 
			
		||||
            let number = u64::from_be_bytes(bytes);
 | 
			
		||||
            number + 1
 | 
			
		||||
        }
 | 
			
		||||
        None => 1, // Start at one. since 0 should return the first event in the db
 | 
			
		||||
        _ => 1, // Start at one. since 0 should return the first event in the db
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    Some(number.to_be_bytes().to_vec())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn generate_keypair(old: Option<&[u8]>) -> Option<Vec<u8>> {
 | 
			
		||||
    Some(
 | 
			
		||||
        old.map(|s| s.to_vec())
 | 
			
		||||
            .unwrap_or_else(|| ruma::signatures::Ed25519KeyPair::generate().unwrap()),
 | 
			
		||||
    )
 | 
			
		||||
    Some(old.map(|s| s.to_vec()).unwrap_or_else(|| {
 | 
			
		||||
        ruma::signatures::Ed25519KeyPair::generate()
 | 
			
		||||
            .expect("Ed25519KeyPair generation always works (?)")
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Parses the bytes into an u64.
 | 
			
		||||
pub fn u64_from_bytes(bytes: &[u8]) -> u64 {
 | 
			
		||||
    let array: [u8; 8] = bytes.try_into().expect("bytes are valid u64");
 | 
			
		||||
    u64::from_be_bytes(array)
 | 
			
		||||
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
 | 
			
		||||
    let array: [u8; 8] = bytes.try_into()?;
 | 
			
		||||
    Ok(u64::from_be_bytes(array))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Parses the bytes into a string.
 | 
			
		||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
 | 
			
		||||
    Ok(String::from_utf8(bytes.to_vec())?)
 | 
			
		||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
 | 
			
		||||
    String::from_utf8(bytes.to_vec())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn random_string(length: usize) -> String {
 | 
			
		||||
| 
						 | 
				
			
			@ -52,7 +50,7 @@ pub fn random_string(length: usize) -> String {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
/// Calculate a new hash for the given password
 | 
			
		||||
pub fn calculate_hash(password: &str) -> std::result::Result<String, argon2::Error> {
 | 
			
		||||
pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
 | 
			
		||||
    let hashing_config = Config {
 | 
			
		||||
        variant: Variant::Argon2id,
 | 
			
		||||
        ..Default::default()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue