Skip to content

Commit

Permalink
Merge pull request #5 from vstroebel/replace_json
Browse files Browse the repository at this point in the history
Replace json crate and make json an optional feature
  • Loading branch information
vstroebel authored Oct 9, 2023
2 parents ddcdf41 + 875c918 commit 10e8a27
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion jfifdump-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ doc = false

[dependencies]
clap = { version = "4.3", features = ["cargo"] }
jfifdump = { path = "../jfifdump", version = "0.4.0" }
jfifdump = { path = "../jfifdump", version = "0.4.0", features = ["json"] }
6 changes: 5 additions & 1 deletion jfifdump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ keywords = ["jpg", "jpeg", "image"]
readme = "../README.md"
repository = "https://github.com/vstroebel/jfifdump.git"

[features]
default = []
json = ["jzon"]

[dependencies]
json = "0.12"
jzon = { version = "0.12", optional = true }
22 changes: 18 additions & 4 deletions jfifdump/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
use crate::{App0Jfif, Dac, Dht, Dqt, Frame, Handler, Rst, Scan};

use crate::reader::get_marker_string;
use json::object::Object;
use json::{object, JsonValue};
use std::fmt::Write;
use jzon::object::Object;
use jzon::{object, JsonValue};

pub fn get_marker_string(data: &[u8], max: usize) -> String {
let mut result = "".to_owned();
for &v in data.iter().take(max) {
if v.is_ascii_graphic() || v == 0x20 {
result.push(v as char);
} else {
write!(result, "\\x{:#04X}", v).unwrap();
}
}

result
}


pub struct JsonFormat {
markers: Vec<JsonValue>,
Expand All @@ -22,7 +36,7 @@ impl JsonFormat {
}

pub fn stringify(&self) -> String {
json::stringify_pretty(JsonValue::Array(self.markers.clone()), 4)
jzon::stringify_pretty(JsonValue::Array(self.markers.clone()), 4)
}
}

Expand Down
2 changes: 2 additions & 0 deletions jfifdump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ pub use reader::{
};
pub use text::TextFormat;

#[cfg(feature = "json")]
pub use crate::json::JsonFormat;

mod error;
mod handler;
#[cfg(feature = "json")]
mod json;
mod reader;
mod text;
Expand Down
14 changes: 0 additions & 14 deletions jfifdump/src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt::Write;
use std::io::{Error as IoError, Read};

pub use crate::JfifError;
Expand Down Expand Up @@ -464,16 +463,3 @@ impl Frame {
}
}
}

pub fn get_marker_string(data: &[u8], max: usize) -> String {
let mut result = "".to_owned();
for &v in data.iter().take(max) {
if v.is_ascii_graphic() || v == 0x20 {
result.push(v as char);
} else {
write!(result, "\\x{:#04X}", v).unwrap();
}
}

result
}

0 comments on commit 10e8a27

Please sign in to comment.