use std::{
    collections::HashSet,
    fs::File,
    io::{self, prelude::*, BufReader},
};

/// Get individual bits from the byte
pub fn parse_byte_to_bits(byte: &u8, allowed_bits: HashSet<u8>) -> Option<Vec<u8>> {
    let mut result: Vec<u8> = Vec::new();
    for i in 0..8 {
        let mask = 1 << i;
        if byte & mask != 0 && allowed_bits.contains(&mask) {
            result.push(mask);
        }
    }
    if result.is_empty() {
        None
    } else {
        Some(result)
    }
}

/// Read a file and return the content as a string
pub fn read_file_to_string(path: &str) -> io::Result<String> {
    let file = File::open(path)?;
    let mut buf_reader = BufReader::new(file);
    let mut contents = String::new();
    buf_reader.read_to_string(&mut contents)?;
    Ok(contents)
}

/// Write a string to a file
pub fn write_string_to_file(path: &str, content: String) -> io::Result<()> {
    let mut file = File::create(path)?;
    file.write_all(content.as_bytes())?;
    Ok(())
}
