Skip to content

Commit 34b6016

Browse files
fix: make LangID optional on get_string
1 parent 0ca8f09 commit 34b6016

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub trait UsbClass<B: UsbBus> {
4444
/// * `index` - A string index allocated earlier with
4545
/// [`UsbAllocator`](crate::bus::UsbBusAllocator).
4646
/// * `lang_id` - The language ID for the string to retrieve.
47-
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
47+
fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
4848
let _ = (index, lang_id);
4949
None
5050
}

src/device.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -563,22 +563,25 @@ impl<B: UsbBus> UsbDevice<'_, B> {
563563

564564
// rest STRING Requests
565565
_ => {
566-
let lang_id = match LangID::try_from(req.index) {
567-
Err(_err) => {
568-
#[cfg(feature = "defmt")]
569-
defmt::warn!(
570-
"Receive unknown LANGID {:#06X}, reject the request",
571-
_err.number
572-
);
573-
xfer.reject().ok();
574-
return;
575-
}
566+
let lang_id = LangID::try_from(req.index);
576567

577-
Ok(req_lang_id) => req_lang_id,
578-
};
579568
let string = match index {
580569
// Manufacturer, product, and serial are handled directly here.
581570
1..=3 => {
571+
let lang_id = match lang_id {
572+
Err(_err) => {
573+
#[cfg(feature = "defmt")]
574+
defmt::warn!(
575+
"Receive unknown LANGID {:#06X}, reject the request",
576+
_err.number
577+
);
578+
xfer.reject().ok();
579+
return;
580+
}
581+
582+
Ok(req_lang_id) => req_lang_id,
583+
};
584+
582585
let Some(lang) = config
583586
.string_descriptors
584587
.iter()
@@ -599,7 +602,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
599602
let index = StringIndex::new(index);
600603
classes
601604
.iter()
602-
.find_map(|cls| cls.get_string(index, lang_id))
605+
.find_map(|cls| cls.get_string(index, lang_id.ok()))
603606
}
604607
};
605608

src/test_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
232232
Ok(())
233233
}
234234

235-
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
236-
if lang_id == LangID::EN_US {
235+
fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
236+
if lang_id == Some(LangID::EN_US) {
237237
if index == self.custom_string {
238238
return Some(CUSTOM_STRING);
239239
} else if index == self.interface_string {

0 commit comments

Comments
 (0)