clippable/dblib/src/lib.rs
shockrah 85b32d5e96 + Adding last trait methods required for video meta storage to be properly done
! Still need to add file handling however I think I'm going to do that in the trait itself
This will probably require some thought so I'm gonna wait until the frontend
catches back up
2021-09-30 19:47:44 -07:00

81 lines
2.1 KiB
Rust

use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::env;
use serde::{Serialize, Deserialize};
pub mod request;
pub mod keystore;
pub mod videostore;
pub mod err;
use request::ApiKey;
fn filename() -> String {
match env::var("CLIP_KEY_STORE") {
Ok(val) => val,
Err(_) => "store.json".to_string()
}
}
fn dev_urandom() -> std::io::Result<String> {
// Creates a random hex string that is generated from 48 bytes
let mut file = File::open("/dev/urandom")?;
let mut buf: [u8;48] = [0;48];
file.read_exact(&mut buf)?;
let mut output = String::new();
for byte in buf.iter() {
let hex = format!("{:02X}", byte);
output.push_str(hex.as_str());
}
// Return the hex encoded random bytes
return Ok(output);
}
fn uid() -> std::io::Result<String> {
let raw = dev_urandom()?;
Ok(raw[..8].to_string())
}
#[derive(Serialize, Deserialize)]
pub struct VideoMeta {
pub id: String,
pub name: String,
pub desc: Option<String>,
pub ext: String,
}
#[derive(Serialize, Deserialize)]
struct VideoMetaEntry {
pub name: String,
pub desc: Option<String>,
pub ext: String
}
#[derive(Serialize, Deserialize)]
pub struct DB {
keys: Vec<ApiKey>,
// We map the video meta id to its actual body content
videos: HashMap<String, VideoMetaEntry>
}
// TODO: add some proper lifetime management here and some docs
pub trait KeyStore {
fn new_entry(name: String) -> err::Result<String>;
fn remove_entry(name: String) -> err::Result<bool>;
fn get_entry(name: String) -> err::Result<Option<String>>;
fn new_store(new_filename: String) -> err::Result<()>;
}
pub trait VideoStore {
fn new_video(name: &str, desc: &str, ext: &str) -> err::Result<()>;
fn del_video(id: String) -> err::Result<()>;
fn get_video_by_name(name: &str) -> err::Result<Option<VideoMeta>>;
fn get_video_by_id(id: &str) -> err::Result<Option<VideoMeta>>;
fn rename_video(id: String, new: &str) -> err::Result<bool>;
fn redescribe_video(id: String, new: &str) -> err::Result<bool>;
}