Create a renderer for Candela Obscura character sheets #275

Merged
savanni merged 6 commits from charsheet-plugin into main 2024-11-29 14:42:49 +00:00
5 changed files with 26 additions and 14 deletions
Showing only changes of commit d3db9d60c2 - Show all commits

View File

@ -9,6 +9,6 @@ tasks:
cmds: cmds:
- cargo watch -x test - cargo watch -x test
server: dev:
cmds: cmds:
- cargo watch -x run - cargo watch -x run

View File

@ -1,7 +1,5 @@
use std::{ use std::{
collections::{hash_map::Iter, HashMap}, collections::{hash_map::Iter, HashMap}, fmt::{self, Display}, fs, io::Read, path::PathBuf
fmt::{self, Display},
io::Read,
}; };
use mime::Mime; use mime::Mime;
@ -36,6 +34,12 @@ impl From<std::io::Error> for Error {
#[typeshare] #[typeshare]
pub struct AssetId(String); pub struct AssetId(String);
impl AssetId {
pub fn as_str<'a>(&'a self) -> &'a str {
&self.0
}
}
impl Display for AssetId { impl Display for AssetId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AssetId({})", self.0) write!(f, "AssetId({})", self.0)
@ -75,19 +79,25 @@ pub struct FsAssets {
} }
impl FsAssets { impl FsAssets {
pub fn new() -> Self { pub fn new(path: PathBuf) -> Self {
Self { let dir = fs::read_dir(path).unwrap();
assets: HashMap::new(), let mut assets = HashMap::new();
}
}
fn assets<'a>(&'a self) -> impl Iterator<Item = &'a AssetId> { for dir_ent in dir {
self.assets.keys() println!("{:?}", dir_ent);
let path = dir_ent.unwrap().path();
let file_name = path.file_name().unwrap().to_str().unwrap();
assets.insert(AssetId::from(file_name), path.to_str().unwrap().to_owned());
}
Self {
assets,
}
} }
} }
impl Assets for FsAssets { impl Assets for FsAssets {
fn assets<'a>(&'a self) -> AssetIter<'a> { fn assets<'a>(&'a self) -> AssetIter<'a> {
println!("FsAssets assets: {:?}", self.assets);
AssetIter(self.assets.iter()) AssetIter(self.assets.iter())
} }

View File

@ -99,6 +99,7 @@ impl Core {
} }
pub fn available_images(&self) -> Vec<AssetId> { pub fn available_images(&self) -> Vec<AssetId> {
println!("available_images");
self.0 self.0
.read() .read()
.unwrap() .unwrap()

View File

@ -63,7 +63,7 @@ pub async fn handle_available_images(core: Core) -> impl Reply {
let image_paths: Vec<String> = core let image_paths: Vec<String> = core
.available_images() .available_images()
.into_iter() .into_iter()
.map(|path| format!("{}", path)) .map(|path| format!("{}", path.as_str()))
.collect(); .collect();
Ok(Response::builder() Ok(Response::builder()
@ -116,6 +116,7 @@ pub async fn handle_connect_websocket(
client_id: String, client_id: String,
) -> impl Reply { ) -> impl Reply {
ws.on_upgrade(move |socket| { ws.on_upgrade(move |socket| {
println!("upgrading websocket");
let core = core.clone(); let core = core.clone();
async move { async move {
let (mut ws_sender, _) = socket.split(); let (mut ws_sender, _) = socket.split();

View File

@ -5,7 +5,7 @@ use handlers::{
}; };
use std::{ use std::{
convert::Infallible, convert::Infallible,
net::{IpAddr, Ipv4Addr, SocketAddr}, net::{IpAddr, Ipv4Addr, SocketAddr}, path::PathBuf,
}; };
use warp::{ use warp::{
// header, // header,
@ -96,7 +96,7 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
let core = core::Core::new(FsAssets::new()); let core = core::Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")));
let log = warp::log("visions::api"); let log = warp::log("visions::api");
let route_image = warp::path!("api" / "v1" / "image" / String) let route_image = warp::path!("api" / "v1" / "image" / String)