fix: use traits for enums to/from number and string

This commit is contained in:
Gabriele Musco 2024-08-27 23:08:57 +02:00
parent c8f219402b
commit de4aebd40f
2 changed files with 52 additions and 40 deletions

View file

@ -21,33 +21,10 @@ pub enum XRServiceType {
}
impl XRServiceType {
pub fn from_string(s: String) -> Self {
match s.trim().to_lowercase().as_str() {
"monado" => Self::Monado,
"wivrn" => Self::Wivrn,
_ => Self::Monado,
}
}
pub fn iter() -> Iter<'static, Self> {
[Self::Monado, Self::Wivrn].iter()
}
pub fn as_number(&self) -> u32 {
match self {
Self::Monado => 0,
Self::Wivrn => 1,
}
}
pub fn from_number(i: u32) -> Self {
match i {
0 => Self::Monado,
1 => Self::Wivrn,
_ => panic!("XRServiceType index out of bounds"),
}
}
/// relative path from the prefix lib dir of the libopenxr shared object
pub fn libopenxr_path(&self) -> &'static str {
match self {
@ -65,6 +42,35 @@ impl XRServiceType {
}
}
impl From<&str> for XRServiceType {
fn from(s: &str) -> Self {
match s.trim().to_lowercase().as_str() {
"monado" => Self::Monado,
"wivrn" => Self::Wivrn,
_ => Self::Monado,
}
}
}
impl From<u32> for XRServiceType {
fn from(i: u32) -> Self {
match i {
0 => Self::Monado,
1 => Self::Wivrn,
_ => panic!("XRServiceType index out of bounds"),
}
}
}
impl Into<u32> for XRServiceType {
fn into(self) -> u32 {
match self {
Self::Monado => 0,
Self::Wivrn => 1,
}
}
}
impl Display for XRServiceType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
@ -182,8 +188,8 @@ impl Default for LighthouseDriver {
}
}
impl LighthouseDriver {
pub fn from_string(s: String) -> Self {
impl From<&str> for LighthouseDriver {
fn from(s: &str) -> Self {
match s.trim().to_lowercase().as_str() {
"vive" => Self::Vive,
"survive" => Self::Survive,
@ -193,20 +199,10 @@ impl LighthouseDriver {
_ => Self::Vive,
}
}
}
pub fn iter() -> Iter<'static, Self> {
[Self::Vive, Self::Survive, Self::SteamVR].iter()
}
pub fn as_number(&self) -> u32 {
match self {
Self::Vive => 0,
Self::Survive => 1,
Self::SteamVR => 2,
}
}
pub fn from_number(i: u32) -> Self {
impl From<u32> for LighthouseDriver {
fn from(i: u32) -> Self {
match i {
0 => Self::Vive,
1 => Self::Survive,
@ -216,6 +212,22 @@ impl LighthouseDriver {
}
}
impl Into<u32> for LighthouseDriver {
fn into(self) -> u32 {
match self {
Self::Vive => 0,
Self::Survive => 1,
Self::SteamVR => 2,
}
}
}
impl LighthouseDriver {
pub fn iter() -> Iter<'static, Self> {
[Self::Vive, Self::Survive, Self::SteamVR].iter()
}
}
impl Display for LighthouseDriver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {

View file

@ -147,7 +147,7 @@ impl SimpleComponent for ProfileEditor {
.collect::<Vec<String>>(),
clone!(#[strong] prof, move |row| {
prof.borrow_mut().xrservice_type =
XRServiceType::from_number(row.selected());
XRServiceType::from(row.selected());
}),
),
add: &entry_row(
@ -171,7 +171,7 @@ impl SimpleComponent for ProfileEditor {
.collect::<Vec<String>>(),
clone!(#[strong] prof, move |row| {
prof.borrow_mut().lighthouse_driver =
LighthouseDriver::from_number(row.selected());
LighthouseDriver::from(row.selected());
})
),
add: &path_row(