Skip to content

Commit 20a8f0a

Browse files
committed
refactor: migrate crate library to use anyhow::Result
This enables CLI and crate to share the same error handling pattern, which is a prerequisite for deduplicating stream and parser code. - parser/stream.rs: use anyhow::Result instead of BBLError - parser/decoder.rs: use anyhow::anyhow! for error messages - parser/event.rs: use anyhow::Result - parser/frame.rs: use anyhow::Result - parser/gps.rs: use anyhow::Result - parser/header.rs: use anyhow::Result and anyhow::anyhow! - parser/main.rs: use anyhow::Result - export.rs: use anyhow::Result Addresses Issue #9: Refactor CLI to use crate library functions
1 parent 0f9d196 commit 20a8f0a

8 files changed

Lines changed: 13 additions & 19 deletions

File tree

src/export.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
66
use crate::conversion::*;
77
use crate::types::*;
8-
use crate::Result;
9-
use anyhow::Context;
8+
use anyhow::{Context, Result};
109
use std::collections::HashMap;
1110
use std::fs::File;
1211
use std::io::{BufWriter, Write};

src/parser/decoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::error::{BBLError, Result};
21
use crate::parser::stream::BBLDataStream;
2+
use anyhow::Result;
33

44
// BBL Encoding constants - directly from JavaScript reference
55
pub const ENCODING_SIGNED_VB: u8 = 0;
@@ -46,7 +46,7 @@ pub fn decode_field_value(
4646
values[index] = 0;
4747
}
4848
_ => {
49-
return Err(BBLError::InvalidEncoding(encoding));
49+
return Err(anyhow::anyhow!("Invalid encoding type: {}", encoding));
5050
}
5151
}
5252
Ok(())
@@ -127,6 +127,6 @@ pub fn apply_predictor(
127127
let motor_output_min = sysconfig.get("motorOutput[0]").copied().unwrap_or(48);
128128
Ok(value + motor_output_min) // Force signed 32-bit like Betaflight
129129
}
130-
_ => Err(BBLError::InvalidPredictor(predictor)),
130+
_ => Err(anyhow::anyhow!("Invalid predictor type: {}", predictor)),
131131
}
132132
}

src/parser/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use crate::parser::stream::BBLDataStream;
77
use crate::types::EventFrame;
8-
use crate::Result;
8+
use anyhow::Result;
99

1010
/// Helper function to parse inflight adjustment events (types 4 and 13)
1111
/// Returns the event description string

src/parser/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::error::Result;
21
use crate::parser::{decoder::*, event::parse_e_frame, gps::*, stream::BBLDataStream};
32
use crate::types::{
43
DecodedFrame, EventFrame, FrameDefinition, FrameHistory, FrameStats, GpsCoordinate,
54
GpsHomeCoordinate,
65
};
6+
use anyhow::Result;
77
use std::collections::HashMap;
88

99
/// Parse frames from binary data

src/parser/gps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::parser::decoder::{
1212
use crate::parser::frame::parse_frame_data;
1313
use crate::parser::stream::BBLDataStream;
1414
use crate::types::{FrameDefinition, GpsCoordinate, GpsHomeCoordinate};
15-
use crate::Result;
15+
use anyhow::Result;
1616
use std::collections::HashMap;
1717

1818
/// Parse H-frame (GPS home position) data from the stream

src/parser/header.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::error::{BBLError, Result};
21
use crate::types::{BBLHeader, FrameDefinition};
2+
use anyhow::Result;
33
use std::collections::HashMap;
44

55
/// Parse BBL headers from text
@@ -157,9 +157,7 @@ fn parse_predictor_info(line: &str, frame_def: &mut FrameDefinition) -> Result<(
157157
frame_def.update_predictors(&predictors);
158158
Ok(())
159159
}
160-
Err(_) => Err(BBLError::InvalidHeader(
161-
"Invalid predictor values".to_string(),
162-
)),
160+
Err(_) => Err(anyhow::anyhow!("Invalid header: Invalid predictor values")),
163161
}
164162
}
165163

@@ -178,9 +176,7 @@ fn parse_encoding_info(line: &str, frame_def: &mut FrameDefinition) -> Result<()
178176
frame_def.update_encoding(&encodings);
179177
Ok(())
180178
}
181-
Err(_) => Err(BBLError::InvalidHeader(
182-
"Invalid encoding values".to_string(),
183-
)),
179+
Err(_) => Err(anyhow::anyhow!("Invalid header: Invalid encoding values")),
184180
}
185181
}
186182

src/parser/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::types::*;
2-
use crate::Result;
3-
use anyhow::{anyhow, Context};
2+
use anyhow::{anyhow, Context, Result};
43
use std::path::Path;
54

65
/// Parse BBL file and return all logs (for CLI and multi-log processing)

src/parser/stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::{BBLError, Result};
1+
use anyhow::Result;
22

33
/// BBL data stream for reading binary data
44
pub struct BBLDataStream<'a> {
@@ -30,7 +30,7 @@ impl<'a> BBLDataStream<'a> {
3030
Ok(byte)
3131
} else {
3232
self.eof = true;
33-
Err(BBLError::UnexpectedEof)
33+
Err(anyhow::anyhow!("EOF"))
3434
}
3535
}
3636

0 commit comments

Comments
 (0)