example of using enum to be more explicit about data

This commit is contained in:
Chris Cochrun 2023-11-20 17:38:48 -06:00
parent 586db06b3a
commit adc8bc8c04
2 changed files with 34 additions and 1 deletions

View file

@ -3,7 +3,28 @@
:CATEGORY: dev :CATEGORY: dev
:END: :END:
* Tasks [60%] [45/75] * Tasks [0%] [0/0]
** TODO Possibly add better handling of data through enums
[[file:~/dev/lumina/src/rust/image_model.rs::impl FromStr for Role {]]
I've done a sample of this in the image_model.rs file. But, I essentially want to make sure I can ask that each piece of data is associated with an enum so that I am ensuring to protect myself. Now that I think about it more though, I'm not 100% sure why or what to do it with, so I need a clear reason before just "It's more rust-like!!!"
Here is an example
#+begin_src rust
impl FromStr for Role {
type Err = ();
fn from_str(input: &str) -> Result<Role, Self::Err> {
match input {
"id" => Ok(Role::IdRole),
"title" => Ok(Role::TitleRole),
"path" => Ok(Role::PathRole),
_ => Err(()),
}
}
}
#+end_src
** TODO Mouse needs to have resize shape when hovering controls ** TODO Mouse needs to have resize shape when hovering controls
[[file:~/dev/lumina/src/qml/presenter/SongEditor.qml::Controls.SplitView {]] [[file:~/dev/lumina/src/qml/presenter/SongEditor.qml::Controls.SplitView {]]
** TODO Add a way to interact with OBS ** TODO Add a way to interact with OBS

View file

@ -62,6 +62,18 @@ mod image_model {
TitleRole, TitleRole,
} }
impl FromStr for Role {
type Err = ();
fn from_str(input: &str) -> Result<Role, Self::Err> {
match input {
"id" => Ok(Role::IdRole),
"title" => Ok(Role::TitleRole),
"path" => Ok(Role::PathRole),
_ => Err(()),
}
}
}
// use crate::entities::{images, prelude::Images}; // use crate::entities::{images, prelude::Images};
// use sea_orm::{ConnectionTrait, Database, DbBackend, DbErr, Statement, ActiveValue}; // use sea_orm::{ConnectionTrait, Database, DbBackend, DbErr, Statement, ActiveValue};
impl qobject::ImageModel { impl qobject::ImageModel {