Skip to content

Commit

Permalink
refactor(ocr): error for already initialized TESSERACT
Browse files Browse the repository at this point in the history
If the thread local variable TESSERACT is already initialized,
propagate the new error `AlreadyInitialized`.
  • Loading branch information
gwen-lg committed Feb 8, 2025
1 parent 02c073e commit 6ad3ce3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ocr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub enum Error {
#[error("Could not initialize tesseract")]
Initialize(#[from] TessInitError),

/// Indicate than `TESSERACT` was already initialized on this thread
#[error("Thread local var `TESSERACT` is already initialized")]
AlreadyInitialized,

/// Indicate an error during `Tesseract` variable set.
#[error("Could not set tesseract variable")]
SetVariable(#[from] TessSetVariableError),
Expand Down Expand Up @@ -82,8 +86,9 @@ where
ctx.index()
);
let tesseract = TesseractWrapper::new(opt.tessdata_dir.as_deref(), opt.lang, opt.config)?;
let old = TESSERACT.replace(Some(tesseract));
assert!(old.is_none());
if TESSERACT.replace(Some(tesseract)).is_some() {
return Err(Error::AlreadyInitialized);
};
Ok::<_, Error>(())
})
.into_iter()
Expand Down

0 comments on commit 6ad3ce3

Please sign in to comment.