Skip to content

Commit 1b7e4b9

Browse files
committed
Auto-pad text in FileMetaTableBuilder
- make builder field setters generic - apply padding to ensure even length
1 parent 6249581 commit 1b7e4b9

File tree

2 files changed

+85
-25
lines changed

2 files changed

+85
-25
lines changed

object/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ mod tests {
203203
#[test]
204204
fn smoke_test() {
205205
let meta = FileMetaTableBuilder::new()
206-
.transfer_syntax(dicom_transfer_syntax_registry::entries::EXPLICIT_VR_LITTLE_ENDIAN.uid().to_owned() + "\0")
207-
.media_storage_sop_class_uid("1.2.840.10008.5.1.4.1.1.1\0".to_owned())
208-
.media_storage_sop_instance_uid("1.2.3.456\0".to_owned())
209-
.implementation_class_uid("1.2.345.6.7890.1.234".to_owned())
206+
.transfer_syntax(dicom_transfer_syntax_registry::entries::EXPLICIT_VR_LITTLE_ENDIAN.uid())
207+
.media_storage_sop_class_uid("1.2.840.10008.5.1.4.1.1.1")
208+
.media_storage_sop_instance_uid("1.2.3.456")
209+
.implementation_class_uid("1.2.345.6.7890.1.234")
210210
.build()
211211
.unwrap();
212212
let obj = RootDicomObject::new_empty_with_meta(

object/src/meta.rs

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,36 @@ impl Default for FileMetaTableBuilder {
360360
}
361361
}
362362

363+
/// Ensure that the string is even lengthed, by adding a trailing character
364+
/// if not.
365+
#[inline]
366+
fn padded<T>(s: T, pad: char) -> String
367+
where
368+
T: Into<String>,
369+
{
370+
let mut s = s.into();
371+
if s.len() % 2 == 1 {
372+
s.push(pad);
373+
}
374+
s
375+
}
376+
377+
/// Ensure that the string is even lengthed with trailing '\0's.
378+
fn ui_padded<T>(s: T) -> String
379+
where
380+
T: Into<String>,
381+
{
382+
padded(s, '\0')
383+
}
384+
385+
/// Ensure that the string is even lengthed with trailing spaces.
386+
fn txt_padded<T>(s: T) -> String
387+
where
388+
T: Into<String>,
389+
{
390+
padded(s, ' ')
391+
}
392+
363393
impl FileMetaTableBuilder {
364394
/// Create a new, empty builder.
365395
pub fn new() -> FileMetaTableBuilder {
@@ -379,62 +409,92 @@ impl FileMetaTableBuilder {
379409
}
380410

381411
/// Define the media storage SOP class UID.
382-
pub fn media_storage_sop_class_uid(mut self, value: String) -> FileMetaTableBuilder {
383-
self.media_storage_sop_class_uid = Some(value);
412+
pub fn media_storage_sop_class_uid<T>(mut self, value: T) -> FileMetaTableBuilder
413+
where
414+
T: Into<String>,
415+
{
416+
self.media_storage_sop_class_uid = Some(ui_padded(value));
384417
self
385418
}
386419

387420
/// Define the media storage SOP instance UID.
388-
pub fn media_storage_sop_instance_uid(mut self, value: String) -> FileMetaTableBuilder {
389-
self.media_storage_sop_instance_uid = Some(value);
421+
pub fn media_storage_sop_instance_uid<T>(mut self, value: T) -> FileMetaTableBuilder
422+
where
423+
T: Into<String>,
424+
{
425+
self.media_storage_sop_instance_uid = Some(ui_padded(value));
390426
self
391427
}
392428

393-
/// Define the transfer syntax.
394-
pub fn transfer_syntax(mut self, value: String) -> FileMetaTableBuilder {
395-
self.transfer_syntax = Some(value);
429+
/// Define the transfer syntax UID.
430+
pub fn transfer_syntax<T>(mut self, value: T) -> FileMetaTableBuilder
431+
where
432+
T: Into<String>,
433+
{
434+
self.transfer_syntax = Some(ui_padded(value));
396435
self
397436
}
398437

399438
/// Define the implementation class UID.
400-
pub fn implementation_class_uid(mut self, value: String) -> FileMetaTableBuilder {
401-
self.implementation_class_uid = Some(value);
439+
pub fn implementation_class_uid<T>(mut self, value: T) -> FileMetaTableBuilder
440+
where
441+
T: Into<String>,
442+
{
443+
self.implementation_class_uid = Some(ui_padded(value));
402444
self
403445
}
404446

405447
/// Define the implementation version name.
406-
pub fn implementation_version_name(mut self, value: String) -> FileMetaTableBuilder {
407-
self.implementation_version_name = Some(value);
448+
pub fn implementation_version_name<T>(mut self, value: T) -> FileMetaTableBuilder
449+
where
450+
T: Into<String>,
451+
{
452+
self.implementation_version_name = Some(txt_padded(value));
408453
self
409454
}
410455

411456
/// Define the source application entity title.
412-
pub fn source_application_entity_title(mut self, value: String) -> FileMetaTableBuilder {
413-
self.source_application_entity_title = Some(value);
457+
pub fn source_application_entity_title<T>(mut self, value: T) -> FileMetaTableBuilder
458+
where
459+
T: Into<String>,
460+
{
461+
self.source_application_entity_title = Some(txt_padded(value));
414462
self
415463
}
416464

417465
/// Define the sending application entity title.
418-
pub fn sending_application_entity_title(mut self, value: String) -> FileMetaTableBuilder {
419-
self.sending_application_entity_title = Some(value);
466+
pub fn sending_application_entity_title<T>(mut self, value: T) -> FileMetaTableBuilder
467+
where
468+
T: Into<String>,
469+
{
470+
self.sending_application_entity_title = Some(txt_padded(value));
420471
self
421472
}
422473

423474
/// Define the receiving application entity title.
424-
pub fn receiving_application_entity_title(mut self, value: String) -> FileMetaTableBuilder {
425-
self.receiving_application_entity_title = Some(value);
475+
pub fn receiving_application_entity_title<T>(mut self, value: T) -> FileMetaTableBuilder
476+
where
477+
T: Into<String>,
478+
{
479+
self.receiving_application_entity_title = Some(txt_padded(value));
426480
self
427481
}
428482

429483
/// Define the private information creator UID.
430-
pub fn private_information_creator_uid(mut self, value: String) -> FileMetaTableBuilder {
431-
self.private_information_creator_uid = Some(value);
484+
pub fn private_information_creator_uid<T>(mut self, value: T) -> FileMetaTableBuilder
485+
where
486+
T: Into<String>,
487+
{
488+
self.private_information_creator_uid = Some(ui_padded(value));
432489
self
433490
}
434491

435492
/// Define the private information as a vector of bytes.
436-
pub fn private_information(mut self, value: Vec<u8>) -> FileMetaTableBuilder {
437-
self.private_information = Some(value);
493+
pub fn private_information<T>(mut self, value: T) -> FileMetaTableBuilder
494+
where
495+
T: Into<Vec<u8>>,
496+
{
497+
self.private_information = Some(value.into());
438498
self
439499
}
440500

0 commit comments

Comments
 (0)