diff --git a/Cargo.toml b/Cargo.toml
index 54d4450..3a19fee 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rusty-tesseract"
-version = "1.1.3"
+version = "1.1.4"
edition = "2021"
authors = ["thomasgruebl"]
description = "A Rust wrapper for Google Tesseract"
diff --git a/README.md b/README.md
index 60a6405..94a69d1 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ A Rust wrapper for Google Tesseract
Add the following line to your Cargo.toml file:
```rust
-rusty-tesseract = "1.1.3"
+rusty-tesseract = "1.1.4"
```
## Description
@@ -94,7 +94,10 @@ Choose either string, bounding box or data output:
// define parameters
let mut my_args = Args {
lang: "eng",
- config_variables: "'tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'",
+ config_variables: HashMap::from([(
+ "tessedit_char_whitelist".into(),
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".into(),
+ )]),
dpi: 150,
psm: 6,
oem: 3
diff --git a/src/tesseract.rs b/src/tesseract.rs
index 9b8ed56..586de8c 100644
--- a/src/tesseract.rs
+++ b/src/tesseract.rs
@@ -11,3 +11,6 @@ pub use input::*;
pub use output_boxes::*;
pub use output_config_parameters::*;
pub use output_data::*;
+
+mod parse_line_util;
+use parse_line_util::*;
diff --git a/src/tesseract/error.rs b/src/tesseract/error.rs
index 383fb6c..d29c838 100644
--- a/src/tesseract/error.rs
+++ b/src/tesseract/error.rs
@@ -1,22 +1,28 @@
use thiserror::Error;
-#[derive(Error, Debug)]
+#[derive(Error, Debug, PartialEq)]
pub enum TessError {
#[error("Tesseract not found. Please check installation path!")]
TesseractNotFoundError,
- #[error("Invalid tesseract version!\n{0}")]
+
+ #[error("Invalid Tesseract version!\n{0}")]
VersionError(String),
+
#[error(
"Image format not within the list of allowed image formats:\n\
- ['JPEG','JPG','PNG','PBM','PGM','PPM','TIFF','BMP','GIF','WEBP']"
+ ['JPEG','JPG','PNG','PBM','PGM','PPM','TIFF','BMP','GIF','WEBP']"
)]
ImageFormatError,
+
#[error("Please assign a valid image path.")]
ImageNotFoundError,
- #[error("Data could not be parsed.")]
- ParseError,
+
+ #[error("Could not parse {0}.")]
+ ParseError(String),
+
#[error("Could not create tempfile.\n{0}")]
TempfileError(String),
+
#[error("Could not save dynamic image to tempfile.\n{0}")]
DynamicImageError(String),
}
diff --git a/src/tesseract/input.rs b/src/tesseract/input.rs
index f4bd798..52c19e2 100644
--- a/src/tesseract/input.rs
+++ b/src/tesseract/input.rs
@@ -33,12 +33,13 @@ impl Args {
if self.config_variables.is_empty() {
return None;
}
- let parameter = self
- .config_variables
- .iter()
- .map(|(key, value)| format!("{}={}", key, value))
- .fold(String::new(), |acc, x| format!("{} {}", acc, x));
- Some(parameter)
+ Some(
+ self.config_variables
+ .iter()
+ .map(|(key, value)| format!("{}={}", key, value))
+ .collect::>()
+ .join(" "),
+ )
}
}
@@ -56,10 +57,6 @@ impl Image {
})
}
- const FORMATS: [&'static str; 10] = [
- "JPEG", "JPG", "PNG", "PBM", "PGM", "PPM", "TIFF", "BMP", "GIF", "WEBP",
- ];
-
fn check_image_format(path: &Path) -> TessResult<()> {
let binding = path
.extension()
@@ -67,7 +64,10 @@ impl Image {
.to_str()
.ok_or(TessError::ImageFormatError)?
.to_uppercase();
- if Self::FORMATS.contains(&binding.as_str()) {
+ if matches!(
+ binding.as_str(),
+ "JPEG" | "JPG" | "PNG" | "PBM" | "PGM" | "PPM" | "TIFF" | "BMP" | "GIF" | "WEBP"
+ ) {
Ok(())
} else {
Err(TessError::ImageFormatError)
diff --git a/src/tesseract/output_boxes.rs b/src/tesseract/output_boxes.rs
index 695ccd9..04ac2ec 100644
--- a/src/tesseract/output_boxes.rs
+++ b/src/tesseract/output_boxes.rs
@@ -1,6 +1,5 @@
-use core::fmt;
-
use super::*;
+use core::fmt;
#[derive(Debug, PartialEq)]
pub struct BoxOutput {
@@ -34,23 +33,17 @@ impl fmt::Display for Box {
}
}
-impl Box {
- fn parse(line: &str) -> Option {
+impl FromLine for Box {
+ fn from_line(line: &str) -> Option {
let mut x = line.split_whitespace();
- let symbol = x.next()?.to_string();
- let left = str::parse::(x.next()?).ok()?;
- let bottom = str::parse::(x.next()?).ok()?;
- let right = str::parse::(x.next()?).ok()?;
- let top = str::parse::(x.next()?).ok()?;
- let page = str::parse::(x.next()?).ok()?;
Some(Box {
- symbol,
- left,
- bottom,
- right,
- top,
- page,
+ symbol: x.next()?.to_string(),
+ left: parse_next(&mut x)?,
+ bottom: parse_next(&mut x)?,
+ right: parse_next(&mut x)?,
+ top: parse_next(&mut x)?,
+ page: parse_next(&mut x)?,
})
}
}
@@ -69,8 +62,7 @@ fn string_to_boxes(output: &str) -> TessResult> {
.lines()
.into_iter()
.map(|line| Box::parse(line.into()))
- .collect::