Removed PWP bc it hurt my brain
This commit is contained in:
Generated
-10
@@ -3869,15 +3869,6 @@ dependencies = [
|
|||||||
"syn 1.0.109",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pulse-macros"
|
|
||||||
version = "0.1.0-alpha.0"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn 2.0.118",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pulse-trader"
|
name = "pulse-trader"
|
||||||
version = "0.1.0-alpha.0"
|
version = "0.1.0-alpha.0"
|
||||||
@@ -3908,7 +3899,6 @@ name = "pulse-wire"
|
|||||||
version = "0.1.0-alpha.0"
|
version = "0.1.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"hypersdk",
|
"hypersdk",
|
||||||
"pulse-macros",
|
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -25,10 +25,9 @@ rand = "0.8.7"
|
|||||||
toml = "1.1.3"
|
toml = "1.1.3"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["pulse-macros", "pulse-ui", "pulse-wire"]
|
members = ["pulse-ui", "pulse-wire"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
pulse-macros = { path = "pulse-macros", version = "0.1.0-alpha.0" }
|
|
||||||
pulse-ui = { path = "pulse-ui", version = "0.1.0-alpha.0" }
|
pulse-ui = { path = "pulse-ui", version = "0.1.0-alpha.0" }
|
||||||
pulse-wire = { path = "pulse-wire", version = "0.1.0-alpha.0" }
|
pulse-wire = { path = "pulse-wire", version = "0.1.0-alpha.0" }
|
||||||
hypersdk = "0.2.14"
|
hypersdk = "0.2.14"
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "pulse-macros"
|
|
||||||
version = "0.1.0-alpha.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
syn = { version = "2", features = ["full"] }
|
|
||||||
quote = "1"
|
|
||||||
proc-macro2 = "1"
|
|
||||||
@@ -1,170 +0,0 @@
|
|||||||
use proc_macro::TokenStream;
|
|
||||||
use quote::quote;
|
|
||||||
use syn::{Fields, ItemEnum, ItemStruct, parse_macro_input};
|
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
|
||||||
pub fn pwp(_: TokenStream, item: TokenStream) -> TokenStream {
|
|
||||||
let input = parse_macro_input!(item as syn::Item);
|
|
||||||
|
|
||||||
match input {
|
|
||||||
syn::Item::Struct(s) => expand_struct(s),
|
|
||||||
syn::Item::Enum(e) => expand_enum(e),
|
|
||||||
_ => {
|
|
||||||
return syn::Error::new_spanned(input, "p_com only supports structs and enums")
|
|
||||||
.to_compile_error()
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_struct(mut input: ItemStruct) -> TokenStream {
|
|
||||||
let name = &input.ident;
|
|
||||||
|
|
||||||
match &mut input.fields {
|
|
||||||
Fields::Named(fields) => {
|
|
||||||
for field in fields.named.iter_mut() {
|
|
||||||
field.vis = syn::Visibility::Public(syn::token::Pub::default());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return syn::Error::new_spanned(input, "p_com only supports structs with named fields")
|
|
||||||
.to_compile_error()
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let fields = match &input.fields {
|
|
||||||
Fields::Named(fields) => &fields.named,
|
|
||||||
_ => {
|
|
||||||
return syn::Error::new_spanned(input, "p_com only supports structs with named fields")
|
|
||||||
.to_compile_error()
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let field_names = fields.iter().map(|f| f.ident.as_ref().unwrap());
|
|
||||||
let field_names2 = fields.iter().map(|f| f.ident.as_ref().unwrap());
|
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
#input
|
|
||||||
|
|
||||||
impl PulseWire for #name {
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
#(
|
|
||||||
vec.extend(self.#field_names.to_com());
|
|
||||||
)*
|
|
||||||
|
|
||||||
vec
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
Self {
|
|
||||||
#(
|
|
||||||
#field_names2: PulseWire::from_com(com),
|
|
||||||
)*
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_enum(input: ItemEnum) -> TokenStream {
|
|
||||||
let name = &input.ident;
|
|
||||||
|
|
||||||
let to_com = input.variants.iter().enumerate().map(|(i, variant)| {
|
|
||||||
let ident = &variant.ident;
|
|
||||||
let tag = i as u8;
|
|
||||||
|
|
||||||
match &variant.fields {
|
|
||||||
Fields::Unit => quote! {
|
|
||||||
Self::#ident => {
|
|
||||||
vec.push(#tag);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => quote! {
|
|
||||||
Self::#ident(v) => {
|
|
||||||
vec.push(#tag);
|
|
||||||
vec.extend(v.to_com());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
Fields::Named(fields) => {
|
|
||||||
let names = fields.named.iter().map(|f| f.ident.as_ref().unwrap());
|
|
||||||
|
|
||||||
let names2 = fields.named.iter().map(|f| f.ident.as_ref().unwrap());
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
Self::#ident { #( #names ),* } => {
|
|
||||||
vec.push(#tag);
|
|
||||||
#( vec.extend(#names2.to_com()); )*
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
panic!("tuple variants with >1 field are not supported");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let from_com = input.variants.iter().enumerate().map(|(i, variant)| {
|
|
||||||
let ident = &variant.ident;
|
|
||||||
let tag = i as u8;
|
|
||||||
|
|
||||||
match &variant.fields {
|
|
||||||
Fields::Unit => quote! {
|
|
||||||
#tag => Self::#ident,
|
|
||||||
},
|
|
||||||
|
|
||||||
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
|
|
||||||
quote! {
|
|
||||||
#tag => Self::#ident(PulseWire::from_com(com)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Fields::Named(fields) => {
|
|
||||||
let names = fields.named.iter().map(|f| f.ident.as_ref().unwrap());
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#tag => Self::#ident {
|
|
||||||
#(
|
|
||||||
#names: PulseWire::from_com(com),
|
|
||||||
)*
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => panic!("tuple variants with >1 field are not supported"),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
#input
|
|
||||||
|
|
||||||
impl PulseWire for #name {
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
match self {
|
|
||||||
#( #to_com )*
|
|
||||||
}
|
|
||||||
|
|
||||||
vec
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
let kind = com.remove(0);
|
|
||||||
|
|
||||||
match kind {
|
|
||||||
#( #from_com )*
|
|
||||||
_ => panic!("invalid {} discriminant {}", stringify!(#name), kind),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,5 @@ version = "0.1.0-alpha.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
pulse-macros = { workspace = true }
|
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
hypersdk = { workspace = true }
|
hypersdk = { workspace = true }
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
use pulse_macros::pwp;
|
use crate::units::{Direction, Symbol, USD};
|
||||||
|
|
||||||
use crate::{
|
|
||||||
PulseWire,
|
|
||||||
units::{Direction, Symbol, USD},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
pub enum MarketTrend {
|
pub enum MarketTrend {
|
||||||
|
|||||||
@@ -1,313 +0,0 @@
|
|||||||
use hypersdk::{
|
|
||||||
Address, Decimal,
|
|
||||||
hypercore::{Candle, CandleInterval, Subscription},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::PulseWire;
|
|
||||||
|
|
||||||
impl PulseWire for Subscription {
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
match u8::from_com(com) {
|
|
||||||
0 => Self::Bbo {
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
1 => Self::Trades {
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
2 => Self::L2Book {
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
n_sig_figs: PulseWire::from_com(com),
|
|
||||||
mantissa: PulseWire::from_com(com),
|
|
||||||
fast: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
3 => Self::Candle {
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
interval: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
4 => Self::AllMids {
|
|
||||||
dex: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
5 => Self::OrderUpdates {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
6 => Self::UserFills {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
7 => Self::UserEvents {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
8 => Self::UserTwapSliceFills {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
9 => Self::UserTwapHistory {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
10 => Self::ActiveAssetCtx {
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
11 => Self::ActiveAssetData {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
coin: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
12 => Self::WebData2 {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
dex: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
13 => Self::ClearinghouseState {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
dex: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
14 => Self::AllDexsClearinghouseState {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
15 => Self::OpenOrders {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
dex: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
16 => Self::SpotState {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
is_portfolio_margin: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
17 => Self::Notification {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
18 => Self::WebData3 {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
19 => Self::TwapStates {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
dex: Option::from_com(com),
|
|
||||||
},
|
|
||||||
20 => Self::UserFundings {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
21 => Self::UserNonFundingLedgerUpdates {
|
|
||||||
user: PulseWire::from_com(com),
|
|
||||||
},
|
|
||||||
22 => Self::AllDexsAssetCtxs,
|
|
||||||
23 => Self::FastAssetCtxs,
|
|
||||||
24 => Self::OutcomeMetaUpdates,
|
|
||||||
x => panic!("Invalid Subscription discriminant: {}", x),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
let mut com = Vec::new();
|
|
||||||
|
|
||||||
match self {
|
|
||||||
Self::Bbo { coin } => {
|
|
||||||
com.push(0);
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
}
|
|
||||||
Self::Trades { coin } => {
|
|
||||||
com.push(1);
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
}
|
|
||||||
Self::L2Book {
|
|
||||||
coin,
|
|
||||||
n_sig_figs,
|
|
||||||
mantissa,
|
|
||||||
fast,
|
|
||||||
} => {
|
|
||||||
com.push(2);
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
com.extend(n_sig_figs.to_com());
|
|
||||||
com.extend(mantissa.to_com());
|
|
||||||
com.extend(fast.to_com());
|
|
||||||
}
|
|
||||||
Self::Candle { coin, interval } => {
|
|
||||||
com.push(3);
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
com.extend(interval.to_com());
|
|
||||||
}
|
|
||||||
Self::AllMids { dex } => {
|
|
||||||
com.push(4);
|
|
||||||
com.extend(dex.to_com());
|
|
||||||
}
|
|
||||||
Self::OrderUpdates { user } => {
|
|
||||||
com.push(5);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::UserFills { user } => {
|
|
||||||
com.push(6);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::UserEvents { user } => {
|
|
||||||
com.push(7);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::UserTwapSliceFills { user } => {
|
|
||||||
com.push(8);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::UserTwapHistory { user } => {
|
|
||||||
com.push(9);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::ActiveAssetCtx { coin } => {
|
|
||||||
com.push(10);
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
}
|
|
||||||
Self::ActiveAssetData { user, coin } => {
|
|
||||||
com.push(11);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(coin.to_com());
|
|
||||||
}
|
|
||||||
Self::WebData2 { user, dex } => {
|
|
||||||
com.push(12);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(dex.to_com());
|
|
||||||
}
|
|
||||||
Self::ClearinghouseState { user, dex } => {
|
|
||||||
com.push(13);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(dex.to_com());
|
|
||||||
}
|
|
||||||
Self::AllDexsClearinghouseState { user } => {
|
|
||||||
com.push(14);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::OpenOrders { user, dex } => {
|
|
||||||
com.push(15);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(dex.to_com());
|
|
||||||
}
|
|
||||||
Self::SpotState {
|
|
||||||
user,
|
|
||||||
is_portfolio_margin,
|
|
||||||
} => {
|
|
||||||
com.push(16);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(is_portfolio_margin.to_com());
|
|
||||||
}
|
|
||||||
Self::Notification { user } => {
|
|
||||||
com.push(17);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::WebData3 { user } => {
|
|
||||||
com.push(18);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::TwapStates { user, dex } => {
|
|
||||||
com.push(19);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
com.extend(dex.to_com());
|
|
||||||
}
|
|
||||||
Self::UserFundings { user } => {
|
|
||||||
com.push(20);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::UserNonFundingLedgerUpdates { user } => {
|
|
||||||
com.push(21);
|
|
||||||
com.extend(user.to_com());
|
|
||||||
}
|
|
||||||
Self::AllDexsAssetCtxs => {
|
|
||||||
com.push(22);
|
|
||||||
}
|
|
||||||
Self::FastAssetCtxs => {
|
|
||||||
com.push(23);
|
|
||||||
}
|
|
||||||
Self::OutcomeMetaUpdates => {
|
|
||||||
com.push(24);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
com
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PulseWire for Address {
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
let bytes: [u8; 20] = com.drain(..20).collect::<Vec<_>>().try_into().unwrap();
|
|
||||||
Self::from_slice(&bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
self.as_slice().to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PulseWire for CandleInterval {
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
match u8::from_com(com) {
|
|
||||||
0 => Self::OneMinute,
|
|
||||||
1 => Self::ThreeMinutes,
|
|
||||||
2 => Self::FiveMinutes,
|
|
||||||
3 => Self::FifteenMinutes,
|
|
||||||
4 => Self::ThirtyMinutes,
|
|
||||||
5 => Self::OneHour,
|
|
||||||
6 => Self::TwoHours,
|
|
||||||
7 => Self::FourHours,
|
|
||||||
8 => Self::EightHours,
|
|
||||||
9 => Self::TwelveHours,
|
|
||||||
10 => Self::OneDay,
|
|
||||||
11 => Self::ThreeDays,
|
|
||||||
12 => Self::OneWeek,
|
|
||||||
13 => Self::OneMonth,
|
|
||||||
x => panic!("Invalid CandleInterval discriminant: {}", x),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
vec![match self {
|
|
||||||
Self::OneMinute => 0,
|
|
||||||
Self::ThreeMinutes => 1,
|
|
||||||
Self::FiveMinutes => 2,
|
|
||||||
Self::FifteenMinutes => 3,
|
|
||||||
Self::ThirtyMinutes => 4,
|
|
||||||
Self::OneHour => 5,
|
|
||||||
Self::TwoHours => 6,
|
|
||||||
Self::FourHours => 7,
|
|
||||||
Self::EightHours => 8,
|
|
||||||
Self::TwelveHours => 9,
|
|
||||||
Self::OneDay => 10,
|
|
||||||
Self::ThreeDays => 11,
|
|
||||||
Self::OneWeek => 12,
|
|
||||||
Self::OneMonth => 13,
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PulseWire for Candle {
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
Self {
|
|
||||||
open_time: u64::from_com(com),
|
|
||||||
close_time: u64::from_com(com),
|
|
||||||
coin: String::from_com(com),
|
|
||||||
interval: String::from_com(com),
|
|
||||||
open: Decimal::from_com(com),
|
|
||||||
high: Decimal::from_com(com),
|
|
||||||
low: Decimal::from_com(com),
|
|
||||||
close: Decimal::from_com(com),
|
|
||||||
volume: Decimal::from_com(com),
|
|
||||||
num_trades: u64::from_com(com),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
let mut com = Vec::new();
|
|
||||||
|
|
||||||
com.extend(self.open_time.to_com());
|
|
||||||
com.extend(self.close_time.to_com());
|
|
||||||
com.extend(self.coin.to_com());
|
|
||||||
com.extend(self.interval.to_com());
|
|
||||||
com.extend(self.open.to_com());
|
|
||||||
com.extend(self.high.to_com());
|
|
||||||
com.extend(self.low.to_com());
|
|
||||||
com.extend(self.close.to_com());
|
|
||||||
com.extend(self.volume.to_com());
|
|
||||||
com.extend(self.num_trades.to_com());
|
|
||||||
|
|
||||||
com
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PulseWire for Decimal {
|
|
||||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
|
||||||
Self::deserialize(com[..16].try_into().unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_com(&self) -> Vec<u8> {
|
|
||||||
self.serialize().to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,14 +2,12 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod general;
|
pub mod general;
|
||||||
mod hyper_types;
|
|
||||||
pub mod plugin;
|
pub mod plugin;
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
pub mod units;
|
pub mod units;
|
||||||
pub use hypersdk;
|
pub use hypersdk;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::PulseWire;
|
|
||||||
pub use crate::general::*;
|
pub use crate::general::*;
|
||||||
pub use crate::plugin::*;
|
pub use crate::plugin::*;
|
||||||
pub use crate::server_path;
|
pub use crate::server_path;
|
||||||
@@ -21,115 +19,3 @@ pub mod prelude {
|
|||||||
pub fn server_path() -> PathBuf {
|
pub fn server_path() -> PathBuf {
|
||||||
PathBuf::from("/tmp/pulse-engine.sock")
|
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);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user