~vam-jam/wpath

fabede0e24da37d6ce6f83f3bb9dc07c91dba035 — Vamist 1 year, 8 months ago 44b3bcc
[dev] Safety + Comments where needed
1 files changed, 16 insertions(+), 15 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +16 -15
@@ 117,17 117,12 @@ impl WordPath {
            for step in to_search.iter_mut() {
                let lead = &step.current;
                for (index, _) in lead.chars().enumerate() {
                    for temp in 0..26 {
                        let mut raw = 'a' as u32;
                        raw += temp;

                        // Can panic if initial point is near the limit of utf8
                        let letter = char::from_u32(raw).unwrap();

                        if !letter.is_alphabetic() {
                            continue;
                        }
                    for increment in 0..26 {
                        // From U+0061, add 'increment' and convert back to utf8
                        let utf8 = 'a' as u32 + increment;
                        let letter = char::from_u32(utf8).unwrap();

                        // Take the original word and replace the index with the new char
                        let mut word = lead.clone();
                        word.replace_range(index..index + 1, letter.to_string().as_str());



@@ 140,8 135,6 @@ impl WordPath {
                                continue;
                            }

                            println!("New word {word}");

                            *searched = true;
                            next_search.push(step.branch(word));
                        }


@@ 156,7 149,7 @@ impl WordPath {
            to_search = next_search;
        }

        Err("No word path could be formed".to_string())
        Err("No word path could be formed, limit reached".to_string())
    }
}



@@ 187,19 180,27 @@ fn fetch_args() -> Result<WordPath, String> {
        );
    }

    // TODO: Scan for -h -help
    // Skip first, as thats us
    // Skip the first arg, as that is our exe
    args.next().unwrap();

    let file = args.next().unwrap();
    let start = args.next().unwrap().to_lowercase();
    let end = args.next().unwrap().to_lowercase();

    // Process all args
    let file = PathBuf::from(file);

    if start.len() != end.len() {
        return Err("Word lengths are not equal, this is not supported".to_string());
    }

    if !start.chars().all(char::is_alphabetic) {
        return Err("Start word must only contain alphabetic characters".to_string());
    }

    if !end.chars().all(char::is_alphabetic) {
        return Err("End word must only contain alphabetic characters".to_string());
    }

    Ok(WordPath { file, start, end })
}