Repository: AmbientRun/Ambient
File: crates/build/src/pipelines/importer.rs (Rust)

Code before:
use std::io::prelude::*;
use std::path::PathBuf;

pub fn import_audio(path: PathBuf, convert: bool) -> anyhow::Result<()> {
    let current_dir = std::env::current_dir().context("Error getting current directory")?;
    let asset_folder_path = current_dir.join("assets");
    let tomlpath = current_dir.join("assets/pipeline.toml");
    println!("Importing audio...");
    println!("Read more about audio usage here: https://ambientrun.github.io/Ambient/reference/audio.html");
    if !std::path::Path::new(&asset_folder_path).exists() {
        std::fs::create_dir_all(&asset_folder_path)?;
    }
    if !std::path::Path::new(&tomlpath).exists() {
        std::fs::File::create(&tomlpath)?;
    }

    let mut file = std::fs::File::open(&tomlpath)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;

    let mut data: toml::Value = match toml::from_str(&contents) {
        Ok(v) => v,
        Err(_) => toml::Value::Table(toml::map::Map::new()), // if we cannot parse the file, start with a fresh table
    };

    if let toml::Value::Table(table) = &mut data {
        let pipelines = match table.get_mut("pipelines") {
            Some(toml::Value::Array(arr)) => arr,
            _ => {
                table.insert("pipelines".to_string(), toml::Value::Array(Vec::new()));
                match table.get_mut("pipelines") {
                    Some(toml::Value::Array(arr)) => arr,
                    _ => panic!("Unexpected state"),
                }
            }
        };

        let mut new_pipeline = toml::map::Map::new();
        new_pipeline.insert(
            String::from("type"),
            toml::Value::String(String::from("Audio")),
        );
        new_pipeline.insert(String::from("convert"), toml::Value::Boolean(convert));
        let filename_with_ext = path.file_name().unwrap().to_str().unwrap().to_string();
        new_pipeline.insert(
            String::from("sources"),
            toml::Value::Array(vec![toml::Value::String(filename_with_ext)]),
        );

        pipelines.push(toml::Value::Table(new_pipeline));
    } else {

Review comment (suggestion):
I think I'd prefer to add `use toml::Value` and `use std::path::Path` etc to reduce the noise in the function body 😊

Code after:
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use toml::map::Map;
use toml::Value;

pub fn import_audio(path: PathBuf, convert: bool) -> anyhow::Result<()> {
    let current_dir = std::env::current_dir().context("Error getting current directory")?;
    let asset_folder_path = current_dir.join("assets");
    let tomlpath = current_dir.join("assets/pipeline.toml");
    println!("Importing audio...");
    println!("Read more about audio usage here: https://ambientrun.github.io/Ambient/reference/audio.html");
    if !Path::new(&asset_folder_path).exists() {
        std::fs::create_dir_all(&asset_folder_path)?;
    }
    if !Path::new(&tomlpath).exists() {
        File::create(&tomlpath)?;
    }

    let mut file = File::open(&tomlpath)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;

    let mut data: Value = match toml::from_str(&contents) {
        Ok(v) => v,
        Err(_) => Value::Table(Map::new()), // if we cannot parse the file, start with a fresh table
    };

    if let Value::Table(table) = &mut data {
        let pipelines = match table.get_mut("pipelines") {
            Some(Value::Array(arr)) => arr,
            _ => {
                table.insert("pipelines".to_string(), Value::Array(Vec::new()));
                match table.get_mut("pipelines") {
                    Some(Value::Array(arr)) => arr,
                    _ => panic!("Unexpected state"),
                }
            }
        };

        let mut new_pipeline = Map::new();
        new_pipeline.insert(String::from("type"), Value::String(String::from("Audio")));
        new_pipeline.insert(String::from("convert"), Value::Boolean(convert));
        let filename_with_ext = path.file_name().unwrap().to_str().unwrap().to_string();
        new_pipeline.insert(
            String::from("sources"),
            Value::Array(vec![Value::String(filename_with_ext)]),
        );

        pipelines.push(Value::Table(new_pipeline));
    } else {