Skip to content

Commit a3f7916

Browse files
committed
dlt-rs: Improve constructor for DltId and add from_str_clamped
Simplifies building the class and adds util function. Signed-off-by: Alexander Mohr <[email protected]>
1 parent 193438c commit a3f7916

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

.gitignore

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
build
22
/target
33

4-
*.pcap*
54
.idea
65
.vscode/settings.json
7-
8-
# Docker environment settings
9-
.env
10-
11-
# Allow generated documentation
12-
!docs/
13-
!docs/**

dlt-rs/src/lib.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,10 @@ impl DltId {
257257
/// # Ok(())
258258
/// # }
259259
/// ```
260-
pub fn new<const N: usize>(bytes: &[u8; N]) -> Result<Self, DltError> {
260+
pub fn new(bytes: &[u8]) -> Result<Self, DltError> {
261261
// Validate that N is between 1 and 4
262-
if N == 0 || N > DLT_ID_SIZE_USIZE {
262+
let len = bytes.len();
263+
if bytes.is_empty() || len > DLT_ID_SIZE_USIZE {
263264
return Err(DltError::InvalidInput);
264265
}
265266

@@ -271,12 +272,31 @@ impl DltId {
271272
let mut padded = [0u8; DLT_ID_SIZE_USIZE];
272273
// Indexing is safe here: function ensures N <= DLT_ID_SIZE by validation
273274
#[allow(clippy::indexing_slicing)]
274-
padded[..N].copy_from_slice(&bytes[..N]);
275+
padded[..len].copy_from_slice(&bytes[..len]);
275276

276-
Ok(Self {
277-
bytes: padded,
278-
len: N,
279-
})
277+
Ok(Self { bytes: padded, len })
278+
}
279+
280+
/// Construct a `DltId` from a string slice, clamping to 4 bytes
281+
/// # Errors
282+
/// Returns an error if the string is empty
283+
/// # Example
284+
/// ```no_run
285+
/// # use dlt_rs::{DltId, DltError};
286+
/// # fn main() -> Result<(), DltError> {
287+
/// let id = DltId::from_str_clamped("APPTOOLONG")?;
288+
/// assert_eq!(id.as_str()?, "APPT");
289+
/// # Ok(())
290+
/// # }
291+
/// ```
292+
pub fn from_str_clamped(id: &str) -> Result<Self, DltError> {
293+
if id.is_empty() {
294+
return Err(DltError::InvalidInput);
295+
}
296+
let bytes = id.as_bytes();
297+
let len = bytes.len().clamp(1, DLT_ID_SIZE_USIZE);
298+
299+
DltId::new(bytes.get(0..len).ok_or(DltError::InvalidInput)?)
280300
}
281301

282302
/// Get the ID as a string slice
@@ -294,6 +314,7 @@ impl DltId {
294314
}
295315
}
296316

317+
/// Convert a string slice to a DLT ID, will yield an error if the string is too long or empty
297318
impl TryFrom<&str> for DltId {
298319
type Error = DltError;
299320

tracing-dlt/src/lib.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl DltLayer {
205205
}
206206

207207
// Create new context with custom ID
208-
let ctx_id = Self::span_name_to_dlt_id(&custom_id)?;
208+
let ctx_id = DltId::from_str_clamped(&custom_id)?;
209209
let context = Arc::new(self.app.create_context(&ctx_id, span_name)?);
210210

211211
let mut cache = self.context_cache.write().map_err(|_| DltError::BadLock)?;
@@ -215,27 +215,6 @@ impl DltLayer {
215215
Ok(context)
216216
}
217217

218-
/// Convert a span name to a valid DLT context ID
219-
///
220-
/// Takes the first 1-4 bytes of the span name, uppercase.
221-
/// If the name is longer, it's truncated. If shorter, it's used as-is.
222-
fn span_name_to_dlt_id(name: &str) -> Result<DltId, DltError> {
223-
if name.is_empty() {
224-
return Err(DltError::InvalidInput);
225-
}
226-
let bytes = name.as_bytes();
227-
let len = bytes.len().clamp(1, dlt_rs::DLT_ID_SIZE_USIZE);
228-
229-
let get = |i| bytes.get(i).copied().ok_or(DltError::InvalidInput);
230-
231-
match len {
232-
1 => DltId::new(&[get(0)?]),
233-
2 => DltId::new(&[get(0)?, get(1)?]),
234-
3 => DltId::new(&[get(0)?, get(1)?, get(2)?]),
235-
_ => DltId::new(&[get(0)?, get(1)?, get(2)?, get(3)?]),
236-
}
237-
}
238-
239218
#[cfg(feature = "dlt_layer_internal_logging")]
240219
fn log_dlt_error(metadata: &tracing_core::Metadata, level: tracing::Level, e: DltSysError) {
241220
eprintln!("DLT error occurred: {e:?}");

0 commit comments

Comments
 (0)