Skip to content

Commit ec8cb2b

Browse files
committed
og_image: Refactor OgImageGenerator constructors and add with_typst_path() method
1 parent f3a0fbf commit ec8cb2b

File tree

1 file changed

+38
-22
lines changed
  • crates/crates_io_og_image/src

1 file changed

+38
-22
lines changed

crates/crates_io_og_image/src/lib.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@ pub struct OgImageGenerator {
8080
}
8181

8282
impl OgImageGenerator {
83-
/// Creates a new `OgImageGenerator` with the specified path to the Typst binary.
83+
/// Creates a new `OgImageGenerator` with default binary paths.
84+
///
85+
/// Uses "typst" and "oxipng" as default binary paths, assuming they are
86+
/// available in PATH. Use [`with_typst_path()`](Self::with_typst_path) and
87+
/// [`with_oxipng_path()`](Self::with_oxipng_path) to customize the
88+
/// binary paths.
8489
///
8590
/// # Examples
8691
///
8792
/// ```
88-
/// use std::path::PathBuf;
8993
/// use crates_io_og_image::OgImageGenerator;
9094
///
91-
/// let generator = OgImageGenerator::new(PathBuf::from("/usr/local/bin/typst"));
95+
/// let generator = OgImageGenerator::new();
9296
/// ```
93-
pub fn new(typst_binary_path: PathBuf) -> Self {
94-
Self {
95-
typst_binary_path,
96-
typst_font_path: None,
97-
oxipng_binary_path: PathBuf::from("oxipng"),
98-
}
97+
pub fn new() -> Self {
98+
Self::default()
9999
}
100100

101101
/// Creates a new `OgImageGenerator` using the `TYPST_PATH` environment variable.
@@ -117,36 +117,51 @@ impl OgImageGenerator {
117117
let font_path = var("TYPST_FONT_PATH").map_err(OgImageError::EnvVarError)?;
118118
let oxipng_path = var("OXIPNG_PATH").map_err(OgImageError::EnvVarError)?;
119119

120-
let mut generator = if let Some(ref path) = typst_path {
120+
let mut generator = OgImageGenerator::default();
121+
122+
if let Some(ref path) = typst_path {
121123
debug!(typst_path = %path, "Using custom Typst binary path from environment");
122-
Self::new(PathBuf::from(path))
124+
generator.typst_binary_path = PathBuf::from(path);
123125
} else {
124126
debug!("Using default Typst binary path (assumes 'typst' in PATH)");
125-
Self::default()
126127
};
127128

128129
if let Some(ref font_path) = font_path {
129130
debug!(font_path = %font_path, "Setting custom font path from environment");
130-
let current_dir = std::env::current_dir()?;
131-
let font_path = current_dir.join(font_path).canonicalize()?;
132-
debug!(resolved_font_path = %font_path.display(), "Resolved font path");
133-
generator = generator.with_font_path(font_path);
131+
generator.typst_font_path = Some(PathBuf::from(font_path));
134132
} else {
135133
debug!("No custom font path specified, using Typst default font discovery");
136134
}
137135

138-
let oxipng_binary_path = if let Some(ref path) = oxipng_path {
136+
if let Some(ref path) = oxipng_path {
139137
debug!(oxipng_path = %path, "Using custom oxipng binary path from environment");
140-
PathBuf::from(path)
138+
generator.oxipng_binary_path = PathBuf::from(path);
141139
} else {
142140
debug!("OXIPNG_PATH not set, defaulting to 'oxipng' in PATH");
143-
PathBuf::from("oxipng")
144141
};
145-
generator.oxipng_binary_path = oxipng_binary_path;
146142

147143
Ok(generator)
148144
}
149145

146+
/// Sets the Typst binary path for the generator.
147+
///
148+
/// This allows specifying a custom path to the Typst binary.
149+
/// If not set, defaults to "typst" which assumes the binary is available in PATH.
150+
///
151+
/// # Examples
152+
///
153+
/// ```
154+
/// use std::path::PathBuf;
155+
/// use crates_io_og_image::OgImageGenerator;
156+
///
157+
/// let generator = OgImageGenerator::default()
158+
/// .with_typst_path(PathBuf::from("/usr/local/bin/typst"));
159+
/// ```
160+
pub fn with_typst_path(mut self, typst_path: PathBuf) -> Self {
161+
self.typst_binary_path = typst_path;
162+
self
163+
}
164+
150165
/// Sets the font path for the Typst compiler.
151166
///
152167
/// This allows specifying a custom directory where Typst will look for fonts
@@ -518,8 +533,9 @@ impl OgImageGenerator {
518533
}
519534

520535
impl Default for OgImageGenerator {
521-
/// Creates a default `OgImageGenerator` that assumes the Typst binary is available
522-
/// as "typst" in the system PATH.
536+
/// Creates a default `OgImageGenerator` with default binary paths.
537+
///
538+
/// Uses "typst" and "oxipng" as default binary paths, assuming they are available in PATH.
523539
fn default() -> Self {
524540
Self {
525541
typst_binary_path: PathBuf::from("typst"),

0 commit comments

Comments
 (0)