rustlol 0.1.1

A wad files lib
Documentation
    pub mod macros;
    pub mod errors;
    pub mod wad;
    pub mod io;
    pub mod utility;
    pub mod hash {
        use std::{collections::HashMap, fs::File, hash::Hasher, io::{self, BufRead, BufReader, BufWriter, Write}, path::Path};

  


        /// Container for unhashed dictionary
        pub struct Dict {
            unhashed: HashMap<u64, String>,
        }
        impl Dict {
            /// Create an empty hash dictionary
            pub fn new() -> Self {
                Dict {
                    unhashed: HashMap::new(),
                }
            }

            /// Create a hash dictionary with a reserved capacity
            pub fn with_capacity(capacity: usize) -> Self {
                Dict {
                    unhashed: HashMap::with_capacity(capacity),
                }
            }

            /// Load hash dictionary entries from a file
            pub fn load<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
                let file = File::open(path)?;
                let mut reader = BufReader::new(file);
                let mut buffer = String::new();

                self.unhashed.clear();
                while let Ok(size) = reader.read_line(&mut buffer) {
                    if size == 0 {
                        break;
                    }
                    let mut parts = buffer.trim().splitn(2, ' ');
                    if let (Some(key), Some(value)) = (parts.next(), parts.next()) {
                        if let Ok(key) = u64::from_str_radix(key, 16) {
                            self.unhashed.insert(key, value.to_string());
                        }
                    }
                    buffer.clear();
                }
                Ok(())
            }

            /// Save hash dictionary entries to a file
            pub fn save<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
                let file = File::create(path)?;
                let mut writer = BufWriter::new(file);

                for (key, value) in &self.unhashed {
                    writeln!(writer, "{} {}", key, value)?;
                }
                writer.flush()?;
                Ok(())
            }

            /// Get the value associated with a key
            pub fn get(&self, key: u64) -> Option<&String> {
                self.unhashed.get(&key)
            }

            /// Add a key-value pair to the dictionary
            pub fn add(&mut self, key: u64, value: String) {
                self.unhashed.insert(key, value);
            }
        }

        #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct Xxh64(pub u64);

        impl Xxh64 {

        pub fn new(s: &str) -> Self {
            let mut hasher = twox_hash::XxHash64::with_seed(0);

            for c in s.bytes() {
                let lc = if c >= b'A' && c <= b'Z' {
                    c - b'A' + b'a'
                } else {
                    c
                };
                hasher.write_u8(lc);
            }
            let hash = hasher.finish();
            Self(hash)
        }
            

            pub fn from_path(path: &Path)  -> Self {
                let binding = path.to_string_lossy().replace('\\', "/");
                let mut s = binding.as_str();
                while s.starts_with('.') || s.starts_with('/') {
                    s = &s[1..]
                }
                if s.contains('.') && s.rsplit_once('.').map(|(b, _)| b).unwrap_or(s).len() == 16 {
                    if let Ok(v) = u64::from_str_radix(s.rsplit_once('.').map(|(b, _)| b).unwrap_or(s), 16) {
                        println!("v {:16x}", v);
                        return Self(v);
                    }
                }


                Self::new(s)
            }            
        }

    }