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

Code before:
    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
            }
        }
    }

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

    serde_json::to_writer(dest.open()?, &Deps { inputs, outputs: outputs2 })?;

    Ok(())
}

/// Writes dependencies in the Zero / Text0 format.
fn write_deps_zero(world: &mut SystemWorld, dest: &Output) -> io::Result<()> {
    let mut dest = dest.open()?;
    for dep in relative_dependencies(world)? {

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

Code after:
    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>,
        outputs: Option<Vec<String>>,
    }

    serde_json::to_writer(dest.open()?, &Deps { inputs, outputs })?;

    Ok(())
}

/// Writes dependencies in the Zero / Text0 format.
fn write_deps_zero(world: &mut SystemWorld, dest: &Output) -> io::Result<()> {
    let mut dest = dest.open()?;
    for dep in relative_dependencies(world)? {
        dest.write_all(dep.as_os_str().as_encoded_bytes())?;
        dest.write_all(b"\0")?;
    }
    Ok(())
}