Compare commits

..

No commits in common. "visions-core-test" and "main" have entirely different histories.

7 changed files with 75 additions and 238 deletions

98
Cargo.lock generated
View File

@ -258,7 +258,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -287,7 +287,7 @@ dependencies = [
"sha2",
"sqlformat",
"sqlx",
"thiserror 1.0.64",
"thiserror",
"tokio",
"uuid 0.4.0",
]
@ -368,7 +368,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -470,7 +470,7 @@ dependencies = [
"glib",
"libc",
"once_cell",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -605,7 +605,7 @@ dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -652,7 +652,7 @@ dependencies = [
"cool_asserts",
"serde 1.0.210",
"serde_json",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -712,7 +712,7 @@ version = "0.1.0"
dependencies = [
"nom",
"proptest",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -955,7 +955,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -984,7 +984,7 @@ dependencies = [
"serde_derive",
"serde_json",
"tempfile",
"thiserror 1.0.64",
"thiserror",
"uuid 0.8.2",
]
@ -1130,7 +1130,7 @@ dependencies = [
"serde_json",
"sha2",
"tempdir",
"thiserror 1.0.64",
"thiserror",
"tokio",
"uuid 0.4.0",
"warp",
@ -1153,7 +1153,7 @@ dependencies = [
"glib-build-tools 0.18.0",
"gtk4",
"libadwaita",
"thiserror 1.0.64",
"thiserror",
"tokio",
]
@ -1230,7 +1230,7 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a530c4694a6a8d528794ee9bbd8ba0122e779629ac908d15ad5a7ae7763a33d"
dependencies = [
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -1373,7 +1373,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -1540,7 +1540,7 @@ dependencies = [
"once_cell",
"pin-project-lite",
"smallvec",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -1576,7 +1576,7 @@ dependencies = [
"memchr",
"once_cell",
"smallvec",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -1608,7 +1608,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -2120,7 +2120,7 @@ version = "0.1.0"
dependencies = [
"chrono",
"serde 1.0.210",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -2801,7 +2801,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -2836,7 +2836,7 @@ dependencies = [
"serde 1.0.210",
"serde_json",
"sgf",
"thiserror 1.0.64",
"thiserror",
"uuid 0.8.2",
]
@ -3044,7 +3044,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -3084,7 +3084,7 @@ dependencies = [
"nix",
"once_cell",
"pipewire-sys",
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -3584,7 +3584,7 @@ dependencies = [
name = "result-extended"
version = "0.1.0"
dependencies = [
"thiserror 1.0.64",
"thiserror",
]
[[package]]
@ -3776,7 +3776,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -3836,7 +3836,7 @@ dependencies = [
"nary_tree",
"nom",
"serde 1.0.210",
"thiserror 1.0.64",
"thiserror",
"typeshare",
"uuid 0.8.2",
]
@ -4025,7 +4025,7 @@ dependencies = [
"sha2",
"smallvec",
"sqlformat",
"thiserror 1.0.64",
"thiserror",
"tokio",
"tokio-stream",
"tracing",
@ -4107,7 +4107,7 @@ dependencies = [
"smallvec",
"sqlx-core",
"stringprep",
"thiserror 1.0.64",
"thiserror",
"tracing",
"whoami",
]
@ -4145,7 +4145,7 @@ dependencies = [
"smallvec",
"sqlx-core",
"stringprep",
"thiserror 1.0.64",
"thiserror",
"tracing",
"whoami",
]
@ -4209,9 +4209,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.87"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
@ -4302,16 +4302,7 @@ version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
dependencies = [
"thiserror-impl 1.0.64",
]
[[package]]
name = "thiserror"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa"
dependencies = [
"thiserror-impl 2.0.3",
"thiserror-impl",
]
[[package]]
@ -4322,18 +4313,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
]
[[package]]
name = "thiserror-impl"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -4458,7 +4438,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -4578,7 +4558,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -4620,7 +4600,7 @@ dependencies = [
"log 0.4.22",
"rand 0.8.5",
"sha1",
"thiserror 1.0.64",
"thiserror",
"url 2.5.2",
"utf-8",
]
@ -4674,7 +4654,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a615d6c2764852a2e88a4f16e9ce1ea49bb776b5872956309e170d63a042a34f"
dependencies = [
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]
@ -4877,14 +4857,12 @@ name = "visions"
version = "0.1.0"
dependencies = [
"authdb",
"cool_asserts",
"futures",
"http 1.1.0",
"mime 0.3.17",
"mime_guess 2.0.5",
"serde 1.0.210",
"serde_json",
"thiserror 2.0.3",
"tokio",
"tokio-stream",
"typeshare",
@ -4980,7 +4958,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
"wasm-bindgen-shared",
]
@ -5014,7 +4992,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -5285,7 +5263,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn 2.0.79",
]
[[package]]

View File

@ -19,5 +19,3 @@ futures = "0.3.31"
tokio-stream = "0.1.16"
typeshare = "1.0.4"
urlencoding = "2.1.3"
cool_asserts = "2.0.3"
thiserror = "2.0.3"

View File

@ -1,106 +0,0 @@
use std::{
collections::{hash_map::Iter, HashMap},
fmt::{self, Display},
};
use thiserror::Error;
#[derive(Debug, Error)]
enum Error {
#[error("Asset could not be found: {0}")]
AssetNotFound(AssetId),
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct AssetId(String);
impl Display for AssetId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AssetId({})", self.0)
}
}
impl From<&str> for AssetId {
fn from(s: &str) -> Self {
AssetId(s.to_owned())
}
}
impl From<String> for AssetId {
fn from(s: String) -> Self {
AssetId(s)
}
}
pub struct AssetIter<'a>(Iter<'a, AssetId, String>);
impl <'a> Iterator for AssetIter<'a> {
type Item = (&'a AssetId, &'a String);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub trait Assets {
fn assets<'a>(&'a self) -> AssetIter<'a>;
fn get(&self, asset_id: AssetId) -> Result<Vec<u8>, Error>;
}
pub struct FsAssets {
assets: HashMap<AssetId, String>,
}
impl FsAssets {
pub fn new() -> Self {
Self {
assets: HashMap::new(),
}
}
fn assets<'a>(&'a self) -> impl Iterator<Item = &'a AssetId> {
self.assets.keys()
}
}
impl Assets for FsAssets {
fn assets<'a>(&'a self) -> AssetIter<'a> {
AssetIter(self.assets.iter())
}
fn get(&self, asset_id: AssetId) -> Result<Vec<u8>, Error> {
unimplemented!()
}
}
#[cfg(test)]
pub mod mocks {
use std::collections::HashMap;
use super::*;
pub struct MemoryAssets {
assets: HashMap<AssetId, String>,
}
impl MemoryAssets {
pub fn new(data: Vec<(AssetId, String)>) -> Self {
let mut m = HashMap::new();
data.into_iter().for_each(|(asset, path)| {
m.insert(asset, path);
});
Self { assets: m }
}
}
impl Assets for MemoryAssets {
fn assets<'a>(&'a self) -> AssetIter<'a> {
AssetIter(self.assets.iter())
}
fn get(&self, asset_id: AssetId) -> Result<Vec<u8>, Error> {
unimplemented!()
}
}
}

View File

@ -9,42 +9,32 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use urlencoding::decode;
use uuid::Uuid;
use crate::{
asset_db::{AssetId, Assets},
types::{AppError, Message, Tabletop, RGB},
};
use crate::types::{AppError, Message, Tabletop, RGB};
#[derive(Debug)]
struct WebsocketClient {
sender: Option<UnboundedSender<Message>>,
}
#[derive(Debug)]
pub struct AppState {
pub asset_db: Box<dyn Assets + Sync + Send + 'static>,
pub clients: HashMap<String, WebsocketClient>,
pub image_base: PathBuf,
pub tabletop: Tabletop,
pub clients: HashMap<String, WebsocketClient>,
}
#[derive(Clone)]
pub struct Core(Arc<RwLock<AppState>>);
#[derive(Clone, Debug)]
pub struct Core(pub Arc<RwLock<AppState>>);
impl Core {
pub fn new<A>(assetdb: A) -> Self
where
A: Assets + Sync + Send + 'static,
{
pub fn new() -> Self {
Self(Arc::new(RwLock::new(AppState {
asset_db: Box::new(assetdb),
clients: HashMap::new(),
image_base: PathBuf::from("/home/savanni/Pictures"),
tabletop: Tabletop {
background_color: RGB {
red: 0xca,
green: 0xb9,
blue: 0xbb,
},
background_color: RGB{ red: 0xca, green: 0xb9, blue: 0xbb },
background_image: None,
},
clients: HashMap::new(),
})))
}
@ -78,37 +68,35 @@ impl Core {
}
}
pub fn tabletop(&self) -> Tabletop {
self.0.read().unwrap().tabletop.clone()
}
pub async fn get_file(&self, file_name: String) -> Vec<u8> {
/*
pub fn get_file(&self, file_name: String) -> Vec<u8> {
let file_name = decode(&file_name).expect("UTF-8");
let mut full_path = self.0.read().unwrap().image_base.clone();
full_path.push(file_name.to_string());
let mut content: Vec<u8> = Vec::new();
let mut file = std::fs::File::open(&full_path).unwrap();
file.read_to_end(&mut content).unwrap();
content
*/
unimplemented!()
}
pub fn available_images(&self) -> Vec<AssetId> {
self.0
.read()
pub fn available_images(&self) -> Vec<String> {
std::fs::read_dir(&self.0.read().unwrap().image_base)
.unwrap()
.asset_db
.assets()
.filter_map(|(asset_id, value)| {
println!("[{:?}] {}", mime_guess::from_path(&value).first(), value);
match mime_guess::from_path(&value).first() {
Some(mime) if mime.type_() == mime::IMAGE => Some(asset_id.clone()),
.filter_map(|entry| match entry {
Ok(entry_) => match mime_guess::from_path(entry_.path()).first() {
Some(mime) if mime.type_() == mime::IMAGE => Some(
entry_
.path()
.file_name()
.and_then(|filename| filename.to_str())
.and_then(|filename| Some(filename.to_owned()))
.unwrap(),
),
_ => None,
}
},
Err(_) => None,
})
.collect()
}
@ -133,24 +121,3 @@ impl Core {
});
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::asset_db::mocks::MemoryAssets;
#[tokio::test]
async fn it_lists_available_images() {
let assets = MemoryAssets::new(vec![
(AssetId::from("asset_1"), "asset_1.png".to_owned()),
(AssetId::from("asset_2"), "asset_2.jpg".to_owned()),
(AssetId::from("asset_3"), "asset_3".to_owned()),
(AssetId::from("asset_4"), "asset_4".to_owned()),
(AssetId::from("asset_5"), "asset_5".to_owned()),
]);
let core = Core::new(assets);
let image_paths = core.available_images();
assert_eq!(image_paths.len(), 2);
}
}

View File

@ -1,5 +1,8 @@
use std::{pin::Pin, time::Duration};
use futures::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use tokio_stream::wrappers::UnboundedReceiverStream;
use warp::{http::Response, reply::Reply, ws::Message};
use crate::core::Core;
@ -30,7 +33,7 @@ pub async fn handle_auth(
pub async fn handle_file(core: Core, file_name: String) -> impl Reply {
let mimetype = mime_guess::from_path(&file_name).first().unwrap();
let bytes = core.get_file(file_name).await;
let bytes = core.get_file(file_name);
Response::builder()
.header("application-type", mimetype.to_string())
.body(bytes)
@ -38,13 +41,10 @@ pub async fn handle_file(core: Core, file_name: String) -> impl Reply {
}
pub async fn handle_available_images(core: Core) -> impl Reply {
let image_paths: Vec<String> = core.available_images().into_iter()
.map(|path| format!("{}", path)).collect();
Response::builder()
.header("Access-Control-Allow-Origin", "*")
.header("Content-Type", "application/json")
.body(serde_json::to_string(&image_paths).unwrap())
.body(serde_json::to_string(&core.available_images()).unwrap())
.unwrap()
}
@ -56,7 +56,7 @@ pub struct RegisterResponse {
url: String,
}
pub async fn handle_register_client(core: Core, _request: RegisterRequest) -> impl Reply {
pub async fn handle_register_client(core: Core, request: RegisterRequest) -> impl Reply {
let client_id = core.register_client();
Response::builder()
@ -82,6 +82,7 @@ pub async fn handle_connect_websocket(
ws: warp::ws::Ws,
client_id: String,
) -> impl Reply {
// println!("handle_connect_websocket: {}", client_id);
ws.on_upgrade(move |socket| {
let core = core.clone();
async move {
@ -89,7 +90,7 @@ pub async fn handle_connect_websocket(
let mut receiver = core.connect_client(client_id.clone());
tokio::task::spawn(async move {
let tabletop = core.tabletop();
let tabletop = core.0.read().unwrap().tabletop.clone();
let _ = ws_sender
.send(Message::text(
serde_json::to_string(&crate::types::Message::UpdateTabletop(tabletop))

View File

@ -1,4 +1,3 @@
use asset_db::FsAssets;
use authdb::AuthError;
use handlers::{
handle_available_images, handle_connect_websocket, handle_file, handle_register_client, handle_set_background_image, handle_unregister_client, RegisterRequest
@ -14,9 +13,10 @@ use warp::{
Filter,
};
mod asset_db;
mod core;
mod handlers;
// use handlers::handle_auth;
mod types;
@ -96,7 +96,7 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible
#[tokio::main]
pub async fn main() {
let core = core::Core::new(FsAssets::new());
let core = core::Core::new();
let log = warp::log("visions::api");
let route_image = warp::path!("api" / "v1" / "image" / String)

View File

@ -3,7 +3,6 @@ use typeshare::typeshare;
#[derive(Debug)]
pub enum AppError {
NotFound(String),
JsonError(serde_json::Error),
}