Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Sep 29, 2024
1 parent 7c84840 commit 54f70d8
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 118 deletions.
3 changes: 2 additions & 1 deletion crates/reg_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ path = "src/main.rs"

[dependencies]
reg_core = { path = "../reg_core" }
clap = { version = "4", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
serde_json = { version = "1.0" }
39 changes: 25 additions & 14 deletions crates/reg_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use reg_core::{run, Options};
use reg_core::{run, JsonReport, Options, Url};
use std::path::{Path, PathBuf};

#[derive(Parser, Debug)]
Expand All @@ -17,6 +17,9 @@ struct Args {
#[arg(long)]
report: Option<PathBuf>,

#[arg(long)]
json: Option<PathBuf>,

#[arg(long)]
matching_threshold: Option<f32>,

Expand All @@ -26,57 +29,65 @@ struct Args {
#[arg(long)]
threshold_pixel: Option<u64>,

#[arg(long)]
url_prefix: Option<Url>,

#[arg(long)]
concurrency: Option<usize>,

#[arg(long)]
enable_antialias: Option<bool>,
}

#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
pub fn main() {
let _ = inner();
}

fn inner() -> Result<(), reg_core::CompareError> {
#[cfg(all(target_os = "wasi", target_env = "p1"))]
pub fn main() {
// NOP
}

fn inner() -> Result<JsonReport, reg_core::CompareError> {
let args = Args::parse();

let options = Options {
report: args.report.as_deref().map(Path::new),
json: args.json.as_deref().map(Path::new),
matching_threshold: args.matching_threshold,
threshold_rate: args.threshold_rate,
threshold_pixel: args.threshold_pixel,
concurrency: args.concurrency,
enable_antialias: args.enable_antialias,
url_prefix: args.url_prefix,
};

run(args.actual_dir, args.expected_dir, args.diff_dir, options)
}

#[cfg(target = "wasm32-wasip1-threads")]
#[cfg(all(target_os = "wasi", target_env = "p1"))]
#[repr(C)]
pub struct WasmOutput {
pub len: usize,
pub buf: *mut u8,
}

#[cfg(target = "wasm32-wasip1-threads")]
#[cfg(all(target_os = "wasi", target_env = "p1"))]
#[no_mangle]
pub extern "C" fn wasm_main() -> *mut WasmOutput {
let json = "{}";
let res = inner().unwrap();
let mut s = serde_json::to_string_pretty(&res).unwrap();

let mut string = json.to_string().into_bytes();
let string_len = string.len();
let string_ptr = string.as_mut_ptr();
std::mem::forget(string);
let len = s.len();
let ptr = s.as_mut_ptr();
std::mem::forget(s);

let output = Box::new(WasmOutput {
len: string_len,
buf: string_ptr,
});
let output = Box::new(WasmOutput { len, buf: ptr });
Box::into_raw(output)
}

#[cfg(target = "wasm32-wasip1-threads")]
#[cfg(all(target_os = "wasi", target_env = "p1"))]
#[no_mangle]
pub extern "C" fn free_wasm_output(ptr: *mut WasmOutput) {
if ptr.is_null() {
Expand Down
1 change: 1 addition & 0 deletions crates/reg_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ urlencoding = "2.1.3"
pathdiff = "0.2.1"
path-clean = "1.0.1"
thiserror = "1.0"
url = "2.5.2"

[dev-dependencies]
31 changes: 20 additions & 11 deletions crates/reg_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ mod report;
use image_diff_rs::{DiffOption, DiffOutput, ImageDiffError};
use path_clean::PathClean;
use rayon::{prelude::*, ThreadPoolBuilder};
use report::Report;
use report::create_reports;
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};

use thiserror::Error;

pub use report::JsonReport;
pub use url::*;

#[derive(Error, Debug)]
pub enum CompareError {
#[error("file io error, {0}")]
Expand All @@ -24,6 +27,8 @@ pub enum CompareError {

static SUPPORTED_EXTENTIONS: [&str; 7] = ["tiff", "jpeg", "jpg", "gif", "png", "bmp", "webp"];

static DEFAULT_JSON_PATH: &'static str = "./reg.json";

fn is_supported_extension(path: &Path) -> bool {
if let Some(extension) = path.extension() {
if let Some(ext_str) = extension.to_str() {
Expand Down Expand Up @@ -53,10 +58,10 @@ pub(crate) struct DetectedImages {
pub struct Options<'a> {
pub report: Option<&'a Path>,
// junitReport?: string,
// json?: string,
pub json: Option<&'a Path>,
// update?: boolean,
// extendedErrors?: boolean,
// urlPrefix?: string,
pub url_prefix: Option<url::Url>,
pub matching_threshold: Option<f32>,
pub threshold_rate: Option<f32>,
pub threshold_pixel: Option<u64>,
Expand All @@ -69,6 +74,8 @@ impl<'a> Default for Options<'a> {
fn default() -> Self {
Self {
report: None,
json: Some(Path::new(DEFAULT_JSON_PATH)),
url_prefix: None,
matching_threshold: Some(0.0),
threshold_rate: None,
threshold_pixel: None,
Expand All @@ -91,10 +98,11 @@ pub fn run(
expected_dir: impl AsRef<Path>,
diff_dir: impl AsRef<Path>,
options: Options,
) -> Result<(), CompareError> {
) -> Result<JsonReport, CompareError> {
let actual_dir = actual_dir.as_ref();
let expected_dir = expected_dir.as_ref();
let diff_dir = diff_dir.as_ref();
let json_path = options.json.unwrap_or_else(|| Path::new(DEFAULT_JSON_PATH));

let detected = find_images(&expected_dir, &actual_dir);

Expand Down Expand Up @@ -151,32 +159,33 @@ pub fn run(
let mut diff_image = image_name.clone();
failed.insert(image_name.clone());
differences.insert(diff_image.clone());
// TODO: make webp, png selectable
diff_image.set_extension("webp");
std::fs::write(diff_dir.join(&diff_image), item.diff_image.clone())?;
}
}

let report = Report::create(report::ReportInput {
let report = create_reports(report::ReportInput {
passed,
failed,
new: detected.new,
deleted: detected.deleted,
// actual: detected.actual,
// expected: detected.expected,
actual: detected.actual,
expected: detected.expected,
report: options.report,
differences,
json: json_path,
actual_dir,
expected_dir,
diff_dir,
from_json: false,
url_prefix: options.url_prefix,
});

if let Some(html) = report.html {
std::fs::write("./report.html", html)?;
if let (Some(html), Some(report)) = (report.html, options.report) {
std::fs::write(report, html)?;
};

Ok(())
Ok(report.json)
}

pub(crate) fn find_images(
Expand Down
Loading

0 comments on commit 54f70d8

Please sign in to comment.