Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge similar functions
Browse files Browse the repository at this point in the history
ldm0 committed Jan 1, 2025
1 parent 3b8e9ac commit c17c6de
Showing 1 changed file with 36 additions and 49 deletions.
85 changes: 36 additions & 49 deletions src/collapse/xctrace.rs
Original file line number Diff line number Diff line change
@@ -174,33 +174,23 @@ fn unescape_xctrace_text(text: Cow<'_, [u8]>) -> Vec<u8> {
.into_bytes()
}

fn get_ref_id_from_attributes(attributes: &Attributes) -> io::Result<u64> {
let ref_id = attributes
.clone()
.filter_map(|x| x.ok())
.find_map(|x| (x.key.into_inner() == REF).then_some(x.value));
let Some(ref_id) = ref_id else {
return invalid_data_error!("No ref id found in attributes");
};
let ref_id = String::from_utf8_lossy(&ref_id);
match ref_id.parse() {
Ok(x) => Ok(x),
Err(e) => invalid_data_error!("Unrecognized ref id: {}: {:?}", ref_id, e),
}
}

fn get_id_from_attributes(attributes: &Attributes) -> io::Result<u64> {
fn get_u64_from_attributes(key: &'static [u8], attributes: &Attributes) -> io::Result<u64> {
let id = attributes
.clone()
.filter_map(|x| x.ok())
.find_map(|x| (x.key.into_inner() == ID).then_some(x.value));
.find_map(|x| (x.key.into_inner() == key).then_some(x.value));
let Some(id) = id else {
return invalid_data_error!("No id found in attributes");
return invalid_data_error!("No {} found in attributes", String::from_utf8_lossy(key));
};
let id = String::from_utf8_lossy(&id);
match id.parse() {
Ok(x) => Ok(x),
Err(e) => invalid_data_error!("Unrecognized id: {}: {:?}", id, e),
Err(e) => invalid_data_error!(
"Unrecognized {}: {}: {:?}",
String::from_utf8_lossy(key),
id,
e
),

Check warning on line 193 in src/collapse/xctrace.rs

Codecov / codecov/patch

src/collapse/xctrace.rs#L188-L193

Added lines #L188 - L193 were not covered by tests
}
}

@@ -217,12 +207,12 @@ fn get_name_from_attributes(attributes: &Attributes) -> io::Result<Vec<u8>> {

/// Extract necessary info from attributes for constructing backtrace.
fn attributes_to_backtrace(attributes: &Attributes) -> io::Result<u64> {
get_id_from_attributes(attributes)
get_u64_from_attributes(ID, attributes)
}

/// Extract necessary info from attributes for constructing frame.
fn attributes_to_frame(attributes: &Attributes) -> io::Result<(u64, Vec<u8>)> {
let id = get_id_from_attributes(attributes)?;
let id = get_u64_from_attributes(ID, attributes)?;
let name = get_name_from_attributes(attributes)?;
Ok((id, name))
}
@@ -309,14 +299,11 @@ impl Folder {
}
Event::End(end) => {
let name = end.name().into_inner();
let state = match context.state_backtrace.pop_with_name(name) {
Ok(state) => state,
Err(()) => {
return invalid_data_error!(
"Unpaired tag: {}",
String::from_utf8_lossy(name)
)
}
let Some(state) = context.state_backtrace.pop_with_name(name) else {
return invalid_data_error!(
"Unpaired tag: {}",
String::from_utf8_lossy(name)
);

Check warning on line 306 in src/collapse/xctrace.rs

Codecov / codecov/patch

src/collapse/xctrace.rs#L303-L306

Added lines #L303 - L306 were not covered by tests
};
match (context.state_backtrace.top_mut(), state) {
(None, CurrentTag::TraceQueryResult(trace_query_result_state)) => {
@@ -366,7 +353,7 @@ impl Folder {
match (context.state_backtrace.top_mut(), name) {
(Some(CurrentTag::Row(row_state)), BACKTRACE) => {
let backtrace =
if let Ok(ref_id) = get_ref_id_from_attributes(&attributes) {
if let Ok(ref_id) = get_u64_from_attributes(REF, &attributes) {
match context.backtraces.get(&ref_id) {
Some(x) => x.clone(),
None => {
@@ -391,26 +378,26 @@ impl Folder {
row_state.backtrace = Some(backtrace);
}
(Some(CurrentTag::Backtrace(backtrace_state)), FRAME) => {
let frame = if let Ok(ref_id) = get_ref_id_from_attributes(&attributes)
{
match context.frames.get(&ref_id) {
Some(x) => x.clone(),
None => {
return invalid_data_error!(
"Invalid frame ref id: {}",
ref_id
)
let frame =
if let Ok(ref_id) = get_u64_from_attributes(REF, &attributes) {
match context.frames.get(&ref_id) {
Some(x) => x.clone(),
None => {
return invalid_data_error!(
"Invalid frame ref id: {}",
ref_id
)

Check warning on line 389 in src/collapse/xctrace.rs

Codecov / codecov/patch

src/collapse/xctrace.rs#L386-L389

Added lines #L386 - L389 were not covered by tests
}
}
}
} else if let Ok((id, name)) = attributes_to_frame(&attributes) {
let frame = Arc::new(Frame { id, name });
context.frames.insert(id, frame.clone());
frame
} else {
return invalid_data_error!(
"Get ref_id or attributes of frame failed."
);
};
} else if let Ok((id, name)) = attributes_to_frame(&attributes) {
let frame = Arc::new(Frame { id, name });
context.frames.insert(id, frame.clone());
frame
} else {
return invalid_data_error!(
"Get ref_id or attributes of frame failed."
);

Check warning on line 399 in src/collapse/xctrace.rs

Codecov / codecov/patch

src/collapse/xctrace.rs#L397-L399

Added lines #L397 - L399 were not covered by tests
};
backtrace_state.frames.push(frame);
}
_ => {}

0 comments on commit c17c6de

Please sign in to comment.