Skip to content

Commit

Permalink
fix(cache): doctests cannot access private mod util
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Jan 26, 2024
1 parent fcaac60 commit d6f78b9
Showing 1 changed file with 0 additions and 54 deletions.
54 changes: 0 additions & 54 deletions cache/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ use std::{
use crate::error::Error;

/// Decode a hex string into a bytearray
///
/// ```
/// use heimdall_cache::util::decode_hex;
///
/// let hex = "48656c6c6f20576f726c64"; // "Hello World" in hex
/// let result = decode_hex(hex);
/// assert_eq!(result, Ok(vec![72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]));
/// ```
pub fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16)).collect()
}

/// Encode a bytearray into a hex string
///
/// ```
/// use heimdall_cache::util::encode_hex;
///
/// let bytes = vec![72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100];
/// let result = encode_hex(bytes);
/// assert_eq!(result, "48656c6c6f20576f726c64");
/// ```
pub fn encode_hex(s: Vec<u8>) -> String {
s.iter().fold(String::new(), |mut acc: String, b| {
write!(acc, "{b:02x}", b = b).expect("unable to write");
Expand All @@ -39,19 +23,6 @@ pub fn encode_hex(s: Vec<u8>) -> String {
}

/// Prettify bytes into a human-readable format \
/// e.g. 1024 -> 1 KB
///
/// ```
/// use heimdall_cache::util::prettify_bytes;
///
/// let bytes = 500;
/// let result = prettify_bytes(bytes);
/// assert_eq!(result, "500 B");
///
/// let bytes = 500_000;
/// let result = prettify_bytes(bytes);
/// assert_eq!(result, "488 KB");
/// ```
pub fn prettify_bytes(bytes: u64) -> String {
if bytes < 1024 {
format!("{bytes} B")
Expand All @@ -68,16 +39,6 @@ pub fn prettify_bytes(bytes: u64) -> String {
}

/// Write contents to a file on the disc
///
/// ```no_run
/// use heimdall_cache::util::write_file;
///
/// let path = "/tmp/test.txt";
/// let contents = "Hello, World!";
/// let result = write_file(path, contents);
///
/// assert!(result.is_ok());
/// ```
pub fn write_file(path_str: &str, contents: &str) -> Result<(), Error> {
let path = Path::new(path_str);

Expand All @@ -97,14 +58,6 @@ pub fn write_file(path_str: &str, contents: &str) -> Result<(), Error> {
}

/// Read contents from a file on the disc
///
/// ```no_run
/// use heimdall_cache::util::read_file;
///
/// let path = "/tmp/test.txt";
/// let contents = read_file(path);
/// assert!(contents.is_ok());
/// ```
pub fn read_file(path: &str) -> Result<String, Error> {
let path = Path::new(path);
let mut file = File::open(path)
Expand All @@ -115,13 +68,6 @@ pub fn read_file(path: &str) -> Result<String, Error> {
}

/// Delete a file or directory on the disc
///
/// ```no_run
/// use heimdall_cache::util::delete_path;
///
/// let path = "/tmp/test.txt";
/// let result = delete_path(path);
/// ```
pub fn delete_path(_path: &str) -> bool {
let path = match std::path::Path::new(_path).to_str() {
Some(path) => path,
Expand Down

0 comments on commit d6f78b9

Please sign in to comment.