Skip to content

Commit da3e6e3

Browse files
committed
og_image: Add debug logging to from_environment() fn
1 parent d0a05d1 commit da3e6e3

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/crates_io_og_image/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ serde_json = "=1.0.140"
1818
tempfile = "=3.20.0"
1919
thiserror = "=2.0.12"
2020
tokio = { version = "=1.45.1", features = ["process", "fs"] }
21+
tracing = "=0.1.41"
2122

2223
[dev-dependencies]
2324
insta = "=1.43.1"
2425
tokio = { version = "=1.45.1", features = ["macros", "rt-multi-thread"] }
26+
tracing-subscriber = { version = "=0.3.19", features = ["env-filter"] }

crates/crates_io_og_image/src/lib.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ pub use error::OgImageError;
77

88
use crate::formatting::{serialize_bytes, serialize_number, serialize_optional_number};
99
use bytes::Bytes;
10-
use crates_io_env_vars::var;
10+
use crates_io_env_vars::var_parsed;
1111
use serde::Serialize;
1212
use std::collections::HashMap;
1313
use std::path::PathBuf;
1414
use tempfile::NamedTempFile;
1515
use tokio::fs;
1616
use tokio::process::Command;
17+
use tracing::debug;
1718

1819
/// Data structure containing information needed to generate an OpenGraph image
1920
/// for a crates.io crate.
@@ -109,19 +110,26 @@ impl OgImageGenerator {
109110
/// # Ok::<(), crates_io_og_image::OgImageError>(())
110111
/// ```
111112
pub fn from_environment() -> Result<Self, OgImageError> {
112-
let typst_path = var("TYPST_PATH").map_err(OgImageError::EnvVarError)?;
113-
let font_path = var("TYPST_FONT_PATH").map_err(OgImageError::EnvVarError)?;
114-
115-
let mut generator = if let Some(path) = typst_path {
116-
Self::new(PathBuf::from(path))
113+
let typst_path = var_parsed::<PathBuf>("TYPST_PATH");
114+
let typst_path = typst_path.map_err(OgImageError::EnvVarError)?;
115+
let font_path = var_parsed::<PathBuf>("TYPST_FONT_PATH");
116+
let font_path = font_path.map_err(OgImageError::EnvVarError)?;
117+
118+
let mut generator = if let Some(typst_path) = typst_path {
119+
debug!("Using TYPST_PATH: {}", typst_path.display());
120+
Self::new(typst_path)
117121
} else {
122+
debug!("TYPST_PATH not set, using default 'typst' binary in PATH");
118123
Self::default()
119124
};
120125

121126
if let Some(font_path) = font_path {
122127
let current_dir = std::env::current_dir()?;
123128
let font_path = current_dir.join(font_path).canonicalize()?;
129+
debug!("Using TYPST_FONT_PATH: {}", font_path.display());
124130
generator = generator.with_font_path(font_path);
131+
} else {
132+
debug!("TYPST_FONT_PATH not set, using default font discovery");
125133
}
126134

127135
Ok(generator)
@@ -325,6 +333,19 @@ impl Default for OgImageGenerator {
325333
#[cfg(test)]
326334
mod tests {
327335
use super::*;
336+
use tracing::level_filters::LevelFilter;
337+
use tracing_subscriber::EnvFilter;
338+
339+
fn init_tracing() {
340+
let env_filter = EnvFilter::builder()
341+
.with_default_directive(LevelFilter::DEBUG.into())
342+
.from_env_lossy();
343+
344+
tracing_subscriber::fmt()
345+
.compact()
346+
.with_env_filter(env_filter)
347+
.init();
348+
}
328349

329350
const fn author(name: &str) -> OgImageAuthorData<'_> {
330351
OgImageAuthorData::new(name, None)
@@ -450,6 +471,8 @@ mod tests {
450471

451472
#[tokio::test]
452473
async fn test_generate_og_image_snapshot() {
474+
init_tracing();
475+
453476
let data = create_simple_test_data();
454477

455478
if let Some(image_data) = generate_image(data).await {
@@ -459,6 +482,8 @@ mod tests {
459482

460483
#[tokio::test]
461484
async fn test_generate_og_image_overflow_snapshot() {
485+
init_tracing();
486+
462487
let data = create_overflow_test_data();
463488

464489
if let Some(image_data) = generate_image(data).await {
@@ -468,6 +493,8 @@ mod tests {
468493

469494
#[tokio::test]
470495
async fn test_generate_og_image_minimal_snapshot() {
496+
init_tracing();
497+
471498
let data = create_minimal_test_data();
472499

473500
if let Some(image_data) = generate_image(data).await {
@@ -477,6 +504,8 @@ mod tests {
477504

478505
#[tokio::test]
479506
async fn test_generate_og_image_escaping_snapshot() {
507+
init_tracing();
508+
480509
let data = create_escaping_test_data();
481510

482511
if let Some(image_data) = generate_image(data).await {

0 commit comments

Comments
 (0)