Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,11 @@ impl std::str::FromStr for DeviceId {
}
"jack" => Ok(DeviceId::Jack(data.to_string())),
"webaudio" => Ok(DeviceId::WebAudio(data.to_string())),
"webaudioworklet" => Ok(DeviceId::WebAudioWorklet(data.to_string())),
"emscripten" => Ok(DeviceId::Emscripten(data.to_string())),
"ios" => Ok(DeviceId::IOS(data.to_string())),
"null" => Ok(DeviceId::Null),
&_ => todo!("implement DeviceId::FromStr for {platform}"),
_ => Err(DeviceIdError::UnsupportedPlatform),
}
}
}
Expand Down Expand Up @@ -992,3 +993,64 @@ fn test_stream_instant() {
);
assert_eq!(max.add(Duration::from_secs(1)), None);
}

#[test]
fn test_device_id_from_str() {
use std::str::FromStr;
let cases = [
("alsa:foo", Ok(DeviceId::ALSA("foo".to_string()))),
("foo:bar", Err(DeviceIdError::UnsupportedPlatform)),
("foobar", Err(DeviceIdError::BackendSpecific{
err: BackendSpecificError {
description: "Failed to parse device id from: foobar\nCheck if format matches Audio_API:DeviceId".to_owned()
},
})),
];

for (input, expected) in cases {
let actual = DeviceId::from_str(input);
assert_eq!(actual, expected, "{input:?}");
}
}

#[test]
fn test_device_id_display_from_str_identity() {
use std::str::FromStr;

// Just a friendly reminder to add a test case
// for every DeviceId variant.
match DeviceId::Null {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this @xephyris ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this seems to work nicely.

DeviceId::CoreAudio(_) => {}
DeviceId::WASAPI(_) => {}
DeviceId::ASIO(_) => {}
DeviceId::ALSA(_) => {}
DeviceId::AAudio(_) => {}
DeviceId::Jack(_) => {}
DeviceId::WebAudio(_) => {}
DeviceId::WebAudioWorklet(_) => {}
DeviceId::Emscripten(_) => {}
DeviceId::IOS(_) => {}
DeviceId::Null => {}
};

let cases = [
DeviceId::AAudio(42),
DeviceId::ALSA("foo".to_string()),
DeviceId::ASIO("bar".to_string()),
DeviceId::CoreAudio("baz".to_string()),
DeviceId::Emscripten("quux".to_string()),
DeviceId::IOS("alfa".to_string()),
DeviceId::Jack("bravo".to_string()),
DeviceId::WASAPI("charlie".to_string()),
DeviceId::WebAudio("delta".to_string()),
DeviceId::WebAudioWorklet("foxtrot".to_string()),
DeviceId::Null,
];

for input in cases {
let string = input.to_string();
let expected = Ok(input);
let actual = DeviceId::from_str(&string);
assert_eq!(expected, actual);
}
}
Loading