Skip to content

Commit

Permalink
Return PathBuf from Picture::to_pdf method (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
DJDuque authored Jan 8, 2023
1 parent b8e734b commit a121b43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgfplots"
version = "0.5.0" # Remember to also change this in the README.md
version = "0.5.1" # Remember to also change this in the README.md
edition = "2021"
license = "MIT"
description = "A Rust library to generate publication-quality figures"
Expand Down
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::axis::{plot::Plot2D, Axis};
use rand::distributions::{Alphanumeric, DistString};
use std::fmt;
use std::io::Write;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use tempfile::NamedTempFile;
use thiserror::Error;
Expand Down Expand Up @@ -235,17 +235,22 @@ impl Picture {
/// Compile the picture environment into a standalone PDF document. This
/// will create the file `jobname.pdf` in the specified `working_dir`
/// (additional files will be created in the same directory e.g. `.log` and
/// `.aux` files).
/// `.aux` files). Return a [`Result`] with the path to the generated PDF
/// file or a [`CompileError`].
///
/// # Examples
///
// Example is `no_run` because `std::env::temp_dir` causes the test to fail
// running GitHub Actions.
/// ```no_run
/// # use pgfplots::CompileError;
/// # fn main() -> Result<(), CompileError> {
/// use pgfplots::{Engine, Picture};
///
/// let picture = Picture::new();
/// picture.to_pdf("./", "jobname", Engine::PdfLatex)?;
/// let pdf_path = picture.to_pdf(std::env::temp_dir(), "jobname", Engine::PdfLatex)?;
///
/// assert_eq!(pdf_path, std::env::temp_dir().join("jobname.pdf"));
///
/// # Ok(())
/// # }
Expand All @@ -255,7 +260,7 @@ impl Picture {
working_dir: P,
jobname: S,
engine: Engine,
) -> Result<(), CompileError>
) -> Result<PathBuf, CompileError>
where
P: AsRef<Path>,
// str instead of OsStr because of Tectonic's `tex_input_file`
Expand All @@ -270,7 +275,7 @@ impl Picture {
match engine {
Engine::PdfLatex => {
let status = Command::new("pdflatex")
.current_dir(working_dir)
.current_dir(working_dir.as_ref())
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg("-interaction=batchmode")
Expand Down Expand Up @@ -311,13 +316,15 @@ impl Picture {
.keep_intermediates(true)
.print_stdout(false)
.output_format(tectonic::driver::OutputFormat::Pdf)
.output_dir(working_dir);
.output_dir(working_dir.as_ref());

let mut sess = tectonic::ctry!(sb.create(&mut status); "failed to initialize the LaTeX processing session");
tectonic::ctry!(sess.run(&mut status); "the LaTeX engine failed");
}
}
Ok(())
Ok(working_dir
.as_ref()
.join(String::from(jobname.as_ref()) + ".pdf"))
}
/// Show the picture environment in a standalone PDF document. This will
/// create a file in the location returned by [`std::env::temp_dir`] and
Expand Down Expand Up @@ -354,8 +361,7 @@ impl Picture {
}

let jobname = random_jobname();
self.to_pdf(std::env::temp_dir(), &jobname, engine)?;
let pdf_path = std::env::temp_dir().join(jobname + ".pdf");
let pdf_path = self.to_pdf(std::env::temp_dir(), &jobname, engine)?;
opener::open(pdf_path)?;
Ok(())
}
Expand Down

0 comments on commit a121b43

Please sign in to comment.