Add some utility functions to API

This commit is contained in:
NGnius (Graham) 2022-06-20 20:03:18 -04:00
parent d242cb9d70
commit 248c5837b5
16 changed files with 114 additions and 23 deletions

6
Cargo.lock generated
View file

@ -926,7 +926,7 @@ dependencies = [
[[package]]
name = "usdpl-back"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"bytes",
"tokio",
@ -936,14 +936,14 @@ dependencies = [
[[package]]
name = "usdpl-core"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"base64",
]
[[package]]
name = "usdpl-front"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"console_error_panic_hook",
"js-sys",

View file

@ -12,6 +12,10 @@ readme = "README.md"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
debug = false
strip = true
lto = true
codegen-units = 4
[workspace]
members = [

View file

@ -6,15 +6,16 @@
Universal Steam Deck Plugin Library
A faster, lighter way to write plugins
A faster, safer way to write plugin back-ends
### Goals
- [x] Minimum viable plugin
- [x] Call back-end API from front-end UI
- [x] External API documentation
- [ ] Internal API documentation
- [ ] Internal protocol documentation
- [x] Async support
- [ ] Sync support
- [ ] Encryption
- [ ] Plugin templates
- [ ] PluginLoader/Decky support
- [ ] Crankshaft support
- [ ] Unnamed plugin system support

View file

@ -1,14 +1,15 @@
//! Universal Steam Deck Plugin Library
//!
//! A faster, lighter way to write plugins
//! A faster, safer way to write plugin back-ends
//!
//! ## Goals
//! - [x] Minimum viable plugin
//! - [x] Call back-end API from front-end UI
//! - [x] External API documentation
//! - [ ] Internal API documentation
//! - [ ] Internal protocol documentation
//! - [x] Async support
//! - [ ] Sync support
//! - [ ] Encryption
//! - [ ] Plugin templates
//! - [ ] PluginLoader/Decky support
//! - [ ] Crankshaft support
//! - [ ] Unnamed plugin system support

View file

@ -1,6 +1,6 @@
[package]
name = "usdpl-back"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs"
@ -14,7 +14,7 @@ crankshaft = ["usdpl-core/crankshaft"]
blocking = ["tokio"] # synchronous API for async functionality, using tokio
[dependencies]
usdpl-core = { version = "0.4.0", path = "../usdpl-core" }
usdpl-core = { version = "0.5.0", path = "../usdpl-core" }
# HTTP web framework
warp = { version = "0.3" }

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,22 @@
//! Directories that may be hard to determine when running from the plugin framework's environment
use std::path::{Path, PathBuf};
use std::process::Command;
//use std::io;
/// The home directory of the user currently running the Steam Deck UI (specifically: running gamescope).
pub fn home() -> Option<PathBuf> {
let pid_out = Command::new("pidof")
.args(["gamescope"])
.output().ok()?;
let pid_out_str = String::from_utf8_lossy(pid_out.stdout.as_slice());
let pid_str = pid_out_str.split(" ").next()?;
//let pid: u32 = pid_str.parse().ok()?;
let user_info = Command::new("bash")
.args([format!("id `cat /proc/{}/loginuid`", pid_str)])
.output().ok()?;
let user_out_str = String::from_utf8_lossy(user_info.stdout.as_slice());
let user_str = user_out_str.split(")").next()?;
let user = &user_str[user_str.find("(")?+1..];
Some(Path::new("/home").join(user))
}

View file

@ -0,0 +1,22 @@
//! Common low-level file operations
use std::fmt::Display;
use std::path::Path;
use std::fs::File;
use std::io::{Read, Write, self};
use std::str::FromStr;
/// Write something to a file.
/// Useful for kernel configuration files.
pub fn write_single<P: AsRef<Path>, D: Display>(path: P, display: D) -> Result<(), io::Error> {
let mut file = File::create(path)?;
write!(file, "{}\n", display)
}
/// Read something from a file.
/// Useful for kernel configuration files.
pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, (Option<io::Error>, Option<E>)> {
let mut file = File::create(path).map_err(|e| (Some(e), None))?;
let mut string = String::new();
file.read_to_string(&mut string).map_err(|e| (Some(e), None))?;
string.parse().map_err(|e| (None, Some(e)))
}

View file

@ -0,0 +1,2 @@
pub mod dirs;
pub mod files;

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -5,6 +5,14 @@
//!
#![warn(missing_docs)]
#[cfg(not(any(feature = "decky", feature = "crankshaft")))]
mod api_any;
mod api_common;
#[cfg(all(feature = "crankshaft", not(any(feature = "decky"))))]
mod api_crankshaft;
#[cfg(all(feature = "decky", not(any(feature = "crankshaft"))))]
mod api_decky;
mod callable;
//mod errors;
mod instance;
@ -13,6 +21,18 @@ pub use callable::Callable;
pub use instance::Instance;
//pub use errors::{ServerError, ServerResult};
/// USDPL backend API.
/// This contains functionality used exclusively by the back-end.
pub mod api {
#[cfg(not(any(feature = "decky", feature = "crankshaft")))]
pub use super::api_any::*;
pub use super::api_common::*;
#[cfg(all(feature = "crankshaft", not(any(feature = "decky"))))]
pub use super::api_crankshaft::*;
#[cfg(all(feature = "decky", not(any(feature = "crankshaft"))))]
pub use super::api_decky::*;
}
/// usdpl-core re-export
pub mod core {
pub use usdpl_core::*;

View file

@ -1,6 +1,6 @@
[package]
name = "usdpl-core"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs"

View file

@ -90,12 +90,6 @@ impl Dumpable for Primitive {
}
}
impl std::convert::Into<Primitive> for String {
fn into(self) -> Primitive {
Primitive::String(self)
}
}
impl std::convert::Into<Primitive> for &str {
fn into(self) -> Primitive {
Primitive::String(self.to_string())
@ -108,6 +102,28 @@ impl std::convert::Into<Primitive> for () {
}
}
macro_rules! into_impl {
($type:ty, $variant:ident) => {
impl std::convert::Into<Primitive> for $type {
fn into(self) -> Primitive {
Primitive::$variant(self)
}
}
}
}
into_impl! {String, String}
into_impl! {bool, Bool}
into_impl! {u32, U32}
into_impl! {u64, U64}
into_impl! {i32, I32}
into_impl! {i64, I64}
into_impl! {f32, F32}
into_impl! {f64, F64}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -1,6 +1,6 @@
[package]
name = "usdpl-front"
version = "0.4.0"
version = "0.5.0"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021"
license = "GPL-3.0-only"
@ -35,7 +35,7 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
web-sys = { version = "0.3", features = [
'Headers',
#'Headers',
'Request',
'RequestInit',
'RequestMode',
@ -44,7 +44,7 @@ web-sys = { version = "0.3", features = [
]}
js-sys = { version = "0.3" }
usdpl-core = { version = "0.4.0", path = "../usdpl-core" }
usdpl-core = { version = "0.5.0", path = "../usdpl-core" }
[dev-dependencies]
wasm-bindgen-test = { version = "0.3.13" }

View file

@ -18,7 +18,7 @@ pub async fn send_js(packet: socket::Packet, port: u16) -> Result<Vec<Primitive>
opts.method("POST");
opts.mode(RequestMode::Cors);
let url = format!("http://localhost:{}/usdpl/call", port);
let url = format!("http://{}:{}/usdpl/call", socket::HOST_STR, port);
let mut buffer = [0u8; socket::PACKET_BUFFER_SIZE];
let len = packet
@ -29,7 +29,7 @@ pub async fn send_js(packet: socket::Packet, port: u16) -> Result<Vec<Primitive>
let request = Request::new_with_str_and_init(&url, &opts)?;
request.headers().set("Accept", "application/bytes")?;
//request.headers().set("Accept", "text/base64")?;
//.set("Authorization", "wasm TODO_KEY")?;
let window = web_sys::window().unwrap();