Repository: typst/typst
File: crates/typst-cli/src/deps.rs (Rust)

Code before:
    outputs: Option<&[Output]>,
) -> io::Result<()> {
    match format {
        DepsFormat::Json => write_deps_json(world, dest, outputs)?,
        DepsFormat::Zero => write_deps_zero(world, dest)?,
        DepsFormat::Make => {
            if let Some(outputs) = outputs {
                write_deps_make(world, dest, outputs)?;
            }
        }
    }
    Ok(())
}

/// Writes dependencies in JSON format.
fn write_deps_json(
    world: &mut SystemWorld,
    dest: &Output,
    outputs: Option<&[Output]>,
) -> io::Result<()> {
    let inputs = relative_dependencies(world)?
        .map(|dep| {
            dep.into_string().map_err(|dep| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("input {dep:?} is not valid utf-8"),
                )
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    let mut outputs2: Vec<String> = Vec::new();
    if let Some(outputs) = outputs {
        for output in outputs {
            match output {
                Output::Path(path) => {
                    outputs2.push(
                        path.as_os_str()
                            .to_str()
                            .ok_or_else(|| {
                                io::Error::new(
                                    io::ErrorKind::InvalidData,
                                    format!("output {path:?} is not valid utf-8"),
                                )
                            })?
                            .to_string(),
                    );
                }
                Output::Stdout => {} // Skip stdout
            }
        }

Review comment (suggestion):
```suggestion
                    format!("input {dep:?} is not valid UTF-8"),
```

Code after:
    outputs: Option<&[Output]>,
) -> io::Result<()> {
    match format {
        DepsFormat::Json => write_deps_json(world, dest, outputs)?,
        DepsFormat::Zero => write_deps_zero(world, dest)?,
        DepsFormat::Make => {
            if let Some(outputs) = outputs {
                write_deps_make(world, dest, outputs)?;
            }
        }
    }
    Ok(())
}

/// Writes dependencies in JSON format.
fn write_deps_json(
    world: &mut SystemWorld,
    dest: &Output,
    outputs: Option<&[Output]>,
) -> io::Result<()> {
    let to_string = |dep: PathBuf, kind| {
        dep.into_os_string().into_string().map_err(|dep| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("{kind} {dep:?} is not valid utf-8"),
            )
        })
    };

    let inputs = relative_dependencies(world)?
        .map(|dep| to_string(dep, "input"))
        .collect::<Result<_, _>>()?;

    let outputs = outputs
        .map(|outputs| {
            outputs
                .iter()
                .filter_map(|output| {
                    match output {
                        Output::Path(path) => Some(to_string(path.clone(), "output")),
                        // Skip stdout
                        Output::Stdout => None,
                    }
                })
                .collect::<Result<_, _>>()
        })
        .transpose()?;

    #[derive(Serialize)]
    struct Deps {
        inputs: Vec<String>,