introducing LJResult<T>

This commit is contained in:
Marc Planard 2023-06-04 15:09:15 +02:00
parent 133f301d1c
commit 61ede78868
5 changed files with 13 additions and 10 deletions

View File

@ -17,5 +17,5 @@ fn do_something() -> redis::RedisResult<()> {
} }
fn main() { fn main() {
do_something(); _ = do_something();
} }

View File

@ -1,5 +1,6 @@
use config::Config; use config::Config;
use serde::Deserialize; use serde::Deserialize;
use crate::errors::LJResult;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub enum DacFamily { pub enum DacFamily {
@ -18,7 +19,7 @@ pub struct Conf {
} }
impl Conf { impl Conf {
pub fn new(path: &str) -> Result<Conf, Box<dyn std::error::Error>> { pub fn new(path: &str) -> LJResult<Conf> {
let settings = Config::builder() let settings = Config::builder()
.add_source(config::File::with_name(path)) .add_source(config::File::with_name(path))
.build()?; .build()?;

View File

@ -2,6 +2,8 @@ use std::error::Error;
use std::fmt; use std::fmt;
use redis::RedisError; use redis::RedisError;
pub type LJResult<T> = Result<T, Box<dyn std::error::Error>>;
#[derive(Debug)] #[derive(Debug)]
pub enum LJError { pub enum LJError {
ConfigFileMissing, ConfigFileMissing,

View File

@ -19,7 +19,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order}; use redis_ctrl::{RedisCtrl,Order};
use conf::Conf; use conf::Conf;
use errors::LJError; use errors::{LJError,LJResult};
const CENTER : (u16,u16) = (2000, 2000); const CENTER : (u16,u16) = (2000, 2000);
@ -32,7 +32,7 @@ pub fn main() {
} }
} }
fn run_all() -> Result<(), Box<dyn std::error::Error>> { fn run_all() -> LJResult<()> {
let Some(filename) = std::env::args().nth(1) else { let Some(filename) = std::env::args().nth(1) else {
return Err(Box::new(LJError::ConfigFileMissing)); return Err(Box::new(LJError::ConfigFileMissing));
}; };
@ -66,7 +66,7 @@ fn run_all() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
fn get_helios_device() -> Result<NativeHeliosDac, Box<dyn std::error::Error>> { fn get_helios_device() -> LJResult<NativeHeliosDac> {
let controller = NativeHeliosDacController::new()?; let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?; let devices = controller.list_devices()?;
let Some(device) = devices.into_iter().next() else { let Some(device) = devices.into_iter().next() else {
@ -81,7 +81,7 @@ fn get_next_frame(
speed: u32, speed: u32,
rs: &mut RedisCtrl, rs: &mut RedisCtrl,
_black: bool _black: bool
) -> Result<Frame, Box<dyn std::error::Error>> { ) -> LJResult<Frame> {
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let line: Vec<Point> = line.iter().map(tuple_to_point).collect(); let line: Vec<Point> = line.iter().map(tuple_to_point).collect();

View File

@ -1,6 +1,6 @@
use redis::{Client, Commands, Connection}; use redis::{Client, Commands, Connection};
use ron::de::from_str; use ron::de::from_str;
use crate::errors::LJError; use crate::errors::{LJError,LJResult};
#[repr(u8)] #[repr(u8)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -49,7 +49,7 @@ pub struct RedisCtrl {
} }
impl RedisCtrl { impl RedisCtrl {
pub fn new(url: &str) -> Result<Self, Box<dyn std::error::Error>> { pub fn new(url: &str) -> LJResult<Self> {
let client = Client::open(url) let client = Client::open(url)
.map_err(LJError::RedisConnect)?; .map_err(LJError::RedisConnect)?;
let connection = client.get_connection() let connection = client.get_connection()
@ -57,13 +57,13 @@ impl RedisCtrl {
Ok(RedisCtrl { client, connection }) Ok(RedisCtrl { client, connection })
} }
pub fn get(&mut self, key: &str) -> Result<Line, Box<dyn std::error::Error>> { pub fn get(&mut self, key: &str) -> LJResult<Line> {
let val: String = self.connection.get(key)?; let val: String = self.connection.get(key)?;
let line: Line = from_str(&val)?; let line: Line = from_str(&val)?;
Ok(line) Ok(line)
} }
pub fn get_order(&mut self, id: u8) -> Result<Order, Box<dyn std::error::Error>> { pub fn get_order(&mut self, id: u8) -> LJResult<Order> {
let path = format!("/order/{id}"); let path = format!("/order/{id}");
let val: u8 = self.connection.get(path.clone())?; let val: u8 = self.connection.get(path.clone())?;