lj_rust/src/redis_ctrl.rs

87 lines
1.5 KiB
Rust
Raw Normal View History

2023-06-03 13:21:36 +00:00
use redis::{
Client,
Connection,
Commands
};
use ron::de::from_str;
2023-06-03 14:20:35 +00:00
#[repr(u8)]
#[derive(Debug,PartialEq)]
pub enum Order {
Draw = 0,
Edh, //homography
Black,
Grid,
Resampler,
ClientKey,
Intensity,
Kpps,
ColorBalance
}
impl TryFrom<u8> for Order {
type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> {
use Order::*;
if value > 8 {
return Err("order out of range".to_string());
}
Ok(match value {
0 => Draw,
1 => Edh,
2 => Black,
3 => Grid,
4 => Resampler,
5 => ClientKey,
6 => Intensity,
7 => Kpps,
8 => ColorBalance,
2023-06-04 09:32:11 +00:00
_ => unreachable!()
2023-06-03 14:20:35 +00:00
})
}
}
2023-06-03 13:21:36 +00:00
pub type Line = Vec<(f32,f32,u32)>;
pub struct RedisCtrl {
pub client: Client,
pub connection: Connection
}
impl RedisCtrl {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let client = Client::open("redis://127.0.0.1/")?;
let connection = client.get_connection()?;
Ok(RedisCtrl { client, connection })
}
pub fn get(
&mut self,
key: &str
) -> Result<Line, Box<dyn std::error::Error>> {
let val : String = self.connection.get(key)?;
let line : Line = from_str(&val)?;
Ok(line)
}
2023-06-03 14:20:35 +00:00
pub fn get_order(
&mut self,
id: u8
) -> Result<Order, Box<dyn std::error::Error>> {
let path = format!("/order/{id}");
let val : u8 = self.connection.get(path.clone())?;
if val == 1 || val >= 4 {
self.connection.set(path, 0)?;
}
Ok(val.try_into()?)
}
2023-06-03 13:21:36 +00:00
}