The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Convert Files Into A New Format While Copying Them Into A Duplicate Directory Structure In Rust

TODO: Pull subtitle into page object
Code
use anyhow::Error;
use glob::glob;
use std::fs;
use std::path::PathBuf;

fn main() {
    let input_root = "test_input_dir";
    let input_extension = "txt";
    let output_root = "test_output_dir";
    let output_extension = "neo";
    let file_list =
        prep_structure(input_root, input_extension, output_root, output_extension).unwrap();
    dbg!(file_list);
}

fn prep_structure(
    in_root: &str,
    in_ext: &str,
    out_root: &str,
    out_ext: &str,
) -> Result<Vec<(PathBuf, PathBuf)>, Error> {
    let mut response: Vec<(PathBuf, PathBuf)> = vec![];
    let mut glob_expression = String::from(in_root);
    glob_expression.push_str("/**/*.");
    glob_expression.push_str(in_ext);
    for entry in glob(glob_expression.as_str())? {
        let in_path = entry?;
        let mut out_path = PathBuf::from(out_root);
        out_path.push(in_path.strip_prefix(in_root).unwrap());
        out_path.set_extension(out_ext);
        let check_dir = &out_path.parent().unwrap();
        if !check_dir.try_exists().unwrap() {
            fs::create_dir_all(check_dir).unwrap();
        }
        response.push((in_path, out_path));
    }
    Ok(response)
}

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Convert Files Into A New Format While Copying 
Them Into A Duplicate Directory Structure In Rust

This is the code I'm using when I need to make
new versions of files in a different format. It
does the following:

-- list

- Finds all `.txt` files via a glob search in an
input directory

- Creates a tuple with the input path plus an
output file path in a new root directory and
with a new file extension

- Creates the directory structure under the 
output directory root

- Returns a Vec of the tuples with the file
paths for processing



-- code
-- rust

use anyhow::Error;
use glob::glob;
use std::fs;
use std::path::PathBuf;

fn main() {
    let input_root = "test_input_dir";
    let input_extension = "txt";
    let output_root = "test_output_dir";
    let output_extension = "neo";
    let file_list =
        prep_structure(input_root, input_extension, output_root, output_extension).unwrap();
    dbg!(file_list);
}

fn prep_structure(
    in_root: &str,
    in_ext: &str,
    out_root: &str,
    out_ext: &str,
) -> Result<Vec<(PathBuf, PathBuf)>, Error> {
    let mut response: Vec<(PathBuf, PathBuf)> = vec![];
    let mut glob_expression = String::from(in_root);
    glob_expression.push_str("/**/*.");
    glob_expression.push_str(in_ext);
    for entry in glob(glob_expression.as_str())? {
        let in_path = entry?;
        let mut out_path = PathBuf::from(out_root);
        out_path.push(in_path.strip_prefix(in_root).unwrap());
        out_path.set_extension(out_ext);
        let check_dir = &out_path.parent().unwrap();
        if !check_dir.try_exists().unwrap() {
            fs::create_dir_all(check_dir).unwrap();
        }
        response.push((in_path, out_path));
    }
    Ok(response)
}


-- blurb

-- categories
-- Rust 

-- metadata
-- date: 2023-05-04 18:24:05
-- id: 2plavp5m
-- site: aws
-- type: post
-- status: published