Skip to content

Commit 39b1b49

Browse files
committed
cli: Add --config arg to report generate
1 parent 6c7160a commit 39b1b49

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

objdiff-cli/src/cmd/diff.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
io::stdout,
33
mem,
4-
str::FromStr,
54
sync::{
65
Arc,
76
atomic::{AtomicBool, Ordering},
@@ -29,10 +28,7 @@ use objdiff_core::{
2928
ProjectConfig, ProjectObject, ProjectObjectMetadata, build_globset,
3029
path::{check_path_buf, platform_path, platform_path_serde_option},
3130
},
32-
diff::{
33-
self, ConfigEnum, ConfigPropertyId, ConfigPropertyKind, DiffObjConfig, MappingConfig,
34-
ObjectDiff,
35-
},
31+
diff::{self, DiffObjConfig, MappingConfig, ObjectDiff},
3632
jobs::{
3733
Job, JobQueue, JobResult,
3834
objdiff::{ObjDiffConfig, start_build},
@@ -43,6 +39,7 @@ use ratatui::prelude::*;
4339
use typed_path::{Utf8PlatformPath, Utf8PlatformPathBuf};
4440

4541
use crate::{
42+
cmd::apply_config_args,
4643
util::{
4744
output::{OutputFormat, write_output},
4845
term::crossterm_panic_handler,
@@ -183,28 +180,7 @@ pub fn run(args: Args) -> Result<()> {
183180

184181
fn build_config_from_args(args: &Args) -> Result<(DiffObjConfig, MappingConfig)> {
185182
let mut diff_config = DiffObjConfig::default();
186-
for config in &args.config {
187-
let (key, value) = config.split_once('=').context("--config expects \"key=value\"")?;
188-
let property_id = ConfigPropertyId::from_str(key)
189-
.map_err(|()| anyhow!("Invalid configuration property: {}", key))?;
190-
diff_config.set_property_value_str(property_id, value).map_err(|()| {
191-
let mut options = String::new();
192-
match property_id.kind() {
193-
ConfigPropertyKind::Boolean => {
194-
options = "true, false".to_string();
195-
}
196-
ConfigPropertyKind::Choice(variants) => {
197-
for (i, variant) in variants.iter().enumerate() {
198-
if i > 0 {
199-
options.push_str(", ");
200-
}
201-
options.push_str(variant.value);
202-
}
203-
}
204-
}
205-
anyhow!("Invalid value for {}. Expected one of: {}", property_id.name(), options)
206-
})?;
207-
}
183+
apply_config_args(&mut diff_config, &args.config)?;
208184
let mut mapping_config = MappingConfig {
209185
mappings: Default::default(),
210186
selecting_left: args.selecting_left.clone(),

objdiff-cli/src/cmd/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
pub mod diff;
22
pub mod report;
3+
4+
use std::str::FromStr;
5+
6+
use anyhow::{Context, Result, anyhow};
7+
use objdiff_core::diff::{ConfigEnum, ConfigPropertyId, ConfigPropertyKind, DiffObjConfig};
8+
9+
pub fn apply_config_args(diff_config: &mut DiffObjConfig, args: &[String]) -> Result<()> {
10+
for config in args {
11+
let (key, value) = config.split_once('=').context("--config expects \"key=value\"")?;
12+
let property_id = ConfigPropertyId::from_str(key)
13+
.map_err(|()| anyhow!("Invalid configuration property: {}", key))?;
14+
diff_config.set_property_value_str(property_id, value).map_err(|()| {
15+
let mut options = String::new();
16+
match property_id.kind() {
17+
ConfigPropertyKind::Boolean => {
18+
options = "true, false".to_string();
19+
}
20+
ConfigPropertyKind::Choice(variants) => {
21+
for (i, variant) in variants.iter().enumerate() {
22+
if i > 0 {
23+
options.push_str(", ");
24+
}
25+
options.push_str(variant.value);
26+
}
27+
}
28+
}
29+
anyhow!("Invalid value for {}. Expected one of: {}", property_id.name(), options)
30+
})?;
31+
}
32+
Ok(())
33+
}

objdiff-cli/src/cmd/report.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tracing::{info, warn};
1717
use typed_path::{Utf8PlatformPath, Utf8PlatformPathBuf};
1818

1919
use crate::{
20-
cmd::diff::ObjectConfig,
20+
cmd::{apply_config_args, diff::ObjectConfig},
2121
util::output::{OutputFormat, write_output},
2222
};
2323

@@ -52,6 +52,9 @@ pub struct GenerateArgs {
5252
#[argp(option, short = 'f')]
5353
/// Output format (json, json-pretty, proto) (default: json)
5454
format: Option<String>,
55+
#[argp(option, short = 'c')]
56+
/// Configuration property (key=value)
57+
config: Vec<String>,
5558
}
5659

5760
#[derive(FromArgs, PartialEq, Debug)]
@@ -80,6 +83,12 @@ pub fn run(args: Args) -> Result<()> {
8083
}
8184

8285
fn generate(args: GenerateArgs) -> Result<()> {
86+
let mut diff_config = diff::DiffObjConfig {
87+
function_reloc_diffs: diff::FunctionRelocDiffs::None,
88+
..Default::default()
89+
};
90+
apply_config_args(&mut diff_config, &args.config)?;
91+
8392
let output_format = OutputFormat::from_option(args.format.as_deref())?;
8493
let project_dir = args.project.as_deref().unwrap_or_else(|| Utf8PlatformPath::new("."));
8594
info!("Loading project {}", project_dir);
@@ -114,14 +123,15 @@ fn generate(args: GenerateArgs) -> Result<()> {
114123
if args.deduplicate {
115124
// If deduplicating, we need to run single-threaded
116125
for object in &objects {
117-
if let Some(unit) = report_object(object, Some(&mut existing_functions))? {
126+
if let Some(unit) = report_object(object, &diff_config, Some(&mut existing_functions))?
127+
{
118128
units.push(unit);
119129
}
120130
}
121131
} else {
122132
let vec = objects
123133
.par_iter()
124-
.map(|object| report_object(object, None))
134+
.map(|object| report_object(object, &diff_config, None))
125135
.collect::<Result<Vec<Option<ReportUnit>>>>()?;
126136
units = vec.into_iter().flatten().collect();
127137
}
@@ -145,6 +155,7 @@ fn generate(args: GenerateArgs) -> Result<()> {
145155

146156
fn report_object(
147157
object: &ObjectConfig,
158+
diff_config: &diff::DiffObjConfig,
148159
mut existing_functions: Option<&mut HashSet<String>>,
149160
) -> Result<Option<ReportUnit>> {
150161
match (&object.target_path, &object.base_path) {
@@ -158,29 +169,25 @@ fn report_object(
158169
}
159170
_ => {}
160171
}
161-
let diff_config = diff::DiffObjConfig {
162-
function_reloc_diffs: diff::FunctionRelocDiffs::None,
163-
..Default::default()
164-
};
165172
let mapping_config = diff::MappingConfig::default();
166173
let target = object
167174
.target_path
168175
.as_ref()
169176
.map(|p| {
170-
obj::read::read(p.as_ref(), &diff_config)
177+
obj::read::read(p.as_ref(), diff_config)
171178
.with_context(|| format!("Failed to open {}", p))
172179
})
173180
.transpose()?;
174181
let base = object
175182
.base_path
176183
.as_ref()
177184
.map(|p| {
178-
obj::read::read(p.as_ref(), &diff_config)
185+
obj::read::read(p.as_ref(), diff_config)
179186
.with_context(|| format!("Failed to open {}", p))
180187
})
181188
.transpose()?;
182189
let result =
183-
diff::diff_objs(target.as_ref(), base.as_ref(), None, &diff_config, &mapping_config)?;
190+
diff::diff_objs(target.as_ref(), base.as_ref(), None, diff_config, &mapping_config)?;
184191

185192
let metadata = ReportUnitMetadata {
186193
complete: object.metadata.complete,

0 commit comments

Comments
 (0)