136 lines
2.9 KiB
Rust
136 lines
2.9 KiB
Rust
#[cfg(target_os = "macos")]
|
|
use std::path::PathBuf;
|
|
|
|
pub mod general;
|
|
mod hyper_types;
|
|
pub mod plugin;
|
|
pub mod terminal;
|
|
pub mod units;
|
|
pub use hypersdk;
|
|
|
|
pub mod prelude {
|
|
pub use crate::PulseWire;
|
|
pub use crate::general::*;
|
|
pub use crate::plugin::*;
|
|
pub use crate::server_path;
|
|
pub use crate::terminal::*;
|
|
pub use crate::units::*;
|
|
pub use hypersdk;
|
|
}
|
|
|
|
pub fn server_path() -> PathBuf {
|
|
PathBuf::from("/tmp/pulse-engine.sock")
|
|
}
|
|
|
|
pub trait PulseWire {
|
|
fn to_com(&self) -> Vec<u8>;
|
|
fn from_com(_com: &mut Vec<u8>) -> Self;
|
|
}
|
|
|
|
impl<T: PulseWire> PulseWire for Vec<T> {
|
|
fn to_com(&self) -> Vec<u8> {
|
|
let mut vec = Vec::new();
|
|
|
|
vec.extend_from_slice(&(self.len() as u32).to_le_bytes());
|
|
|
|
for item in self {
|
|
vec.extend(item.to_com());
|
|
}
|
|
|
|
vec
|
|
}
|
|
|
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
let len_bytes: [u8; 4] = com.drain(..4).collect::<Vec<_>>().try_into().unwrap();
|
|
|
|
let len = u32::from_le_bytes(len_bytes) as usize;
|
|
|
|
let mut result = Vec::with_capacity(len);
|
|
|
|
for _ in 0..len {
|
|
result.push(T::from_com(com));
|
|
}
|
|
|
|
result
|
|
}
|
|
}
|
|
|
|
impl<T: PulseWire> PulseWire for Option<T> {
|
|
fn to_com(&self) -> Vec<u8> {
|
|
if let Some(v) = self {
|
|
vec![1].into_iter().chain(v.to_com()).collect()
|
|
} else {
|
|
vec![0]
|
|
}
|
|
}
|
|
|
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
if com[0] > 0 {
|
|
Some(T::from_com(com))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PulseWire for String {
|
|
fn to_com(&self) -> Vec<u8> {
|
|
let bytes = self.as_bytes();
|
|
let mut out = Vec::with_capacity(4 + bytes.len());
|
|
|
|
out.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
|
|
out.extend_from_slice(bytes);
|
|
|
|
out
|
|
}
|
|
|
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
let len = u32::from_le_bytes(com[..4].try_into().unwrap()) as usize;
|
|
com.drain(..4);
|
|
|
|
let bytes: Vec<u8> = com.drain(..len).collect();
|
|
String::from_utf8(bytes).unwrap()
|
|
}
|
|
}
|
|
|
|
impl PulseWire for bool {
|
|
fn to_com(&self) -> Vec<u8> {
|
|
if *self { vec![1] } else { vec![0] }
|
|
}
|
|
|
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
com[0] > 0
|
|
}
|
|
}
|
|
|
|
macro_rules! int_com {
|
|
($t:ty) => {
|
|
impl $crate::PulseWire for $t {
|
|
fn to_com(&self) -> Vec<u8> {
|
|
self.to_le_bytes().to_vec()
|
|
}
|
|
|
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
const N: usize = std::mem::size_of::<$t>();
|
|
let bytes: [u8; N] = com.drain(..N).collect::<Vec<_>>().try_into().unwrap();
|
|
<$t>::from_le_bytes(bytes)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
int_com!(i8);
|
|
int_com!(i16);
|
|
int_com!(i32);
|
|
int_com!(i64);
|
|
int_com!(isize);
|
|
|
|
int_com!(u8);
|
|
int_com!(u16);
|
|
int_com!(u32);
|
|
int_com!(u64);
|
|
int_com!(usize);
|
|
|
|
int_com!(f64);
|
|
int_com!(f32);
|