univert 0.4.0

Universal file converter (library and CLI)
Documentation
use std::fs;

use crate::{Error, Job, Result};

pub fn toml_to_json(job: &Job) -> Result<()> {
  let raw = fs::read_to_string(job.input)?;
  let value: toml::Value = toml::from_str(&raw)
    .map_err(|e| Error::other(format!("invalid TOML: {e}")))?;
  let json = serde_json::to_string_pretty(&value)
    .map_err(|e| Error::other(format!("json serialize: {e}")))?;
  fs::write(job.output, json + "\n")?;
  Ok(())
}

pub fn json_to_toml(job: &Job) -> Result<()> {
  let raw = fs::read_to_string(job.input)?;
  let value: serde_json::Value = serde_json::from_str(&raw)
    .map_err(|e| Error::other(format!("invalid JSON: {e}")))?;
  let out = toml::to_string_pretty(&value)
    .map_err(|e| Error::other(format!("toml serialize: {e}")))?;
  fs::write(job.output, out)?;
  Ok(())
}