From adc8bc8c04b289d135a88236e5f9820304ec46e5 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Mon, 20 Nov 2023 17:38:48 -0600 Subject: [PATCH] example of using enum to be more explicit about data --- TODO.org | 23 ++++++++++++++++++++++- src/rust/image_model.rs | 12 ++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index 7c459df..79d63e4 100644 --- a/TODO.org +++ b/TODO.org @@ -3,7 +3,28 @@ :CATEGORY: dev :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 { + 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 [[file:~/dev/lumina/src/qml/presenter/SongEditor.qml::Controls.SplitView {]] ** TODO Add a way to interact with OBS diff --git a/src/rust/image_model.rs b/src/rust/image_model.rs index 62da8f0..89f3c89 100644 --- a/src/rust/image_model.rs +++ b/src/rust/image_model.rs @@ -62,6 +62,18 @@ mod image_model { TitleRole, } + impl FromStr for Role { + type Err = (); + fn from_str(input: &str) -> Result { + match input { + "id" => Ok(Role::IdRole), + "title" => Ok(Role::TitleRole), + "path" => Ok(Role::PathRole), + _ => Err(()), + } + } + } + // use crate::entities::{images, prelude::Images}; // use sea_orm::{ConnectionTrait, Database, DbBackend, DbErr, Statement, ActiveValue}; impl qobject::ImageModel {