Pulse wire crate
This commit is contained in:
+4
-6
@@ -1,12 +1,10 @@
|
||||
pub mod ptc;
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use ptc::PulseCom;
|
||||
use pulse_wire::PulseCom;
|
||||
|
||||
fn main() {
|
||||
let input = ptc::EventLog {
|
||||
kind: ptc::LogKind::Warn,
|
||||
let input = pulse_wire::terminal::EventLog {
|
||||
kind: pulse_wire::terminal::LogKind::Warn,
|
||||
name: "Test".to_string(),
|
||||
message: "Hello, WOrld".to_string(),
|
||||
};
|
||||
@@ -14,7 +12,7 @@ fn main() {
|
||||
let start = Instant::now();
|
||||
|
||||
let mut val = input.to_com();
|
||||
let out = ptc::EventLog::from_com(&mut val);
|
||||
let out = pulse_wire::terminal::EventLog::from_com(&mut val);
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
include!("../pc.rs");
|
||||
include!("../ptc.rs");
|
||||
@@ -1,105 +0,0 @@
|
||||
pub trait PulseCom {
|
||||
fn to_com(&self) -> Vec<u8>;
|
||||
fn from_com(_com: &mut Vec<u8>) -> Self;
|
||||
}
|
||||
|
||||
impl<T: PulseCom> PulseCom 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 PulseCom 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()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! int_com {
|
||||
($t:ty) => {
|
||||
impl PulseCom 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);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! p_com {
|
||||
(struct $name:ident { $($n:ident: $v:ty),* $(,)? }) => {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct $name { $(pub $n: $v),* }
|
||||
|
||||
impl PulseCom for $name {
|
||||
fn to_com(&self) -> Vec<u8> {
|
||||
let mut vec = Vec::new();
|
||||
$(vec.extend(self.$n.to_com());)*
|
||||
vec
|
||||
}
|
||||
|
||||
fn from_com(com: &mut Vec<u8>) -> Self {
|
||||
Self {
|
||||
$($n: <$v>::from_com(com),)*
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
use pulse_macros::p_com;
|
||||
|
||||
#[p_com]
|
||||
pub struct WatchListItem {
|
||||
symbol: String,
|
||||
price: f64,
|
||||
trend: f64,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct ActivePosition {
|
||||
symbol: String,
|
||||
profit: f64,
|
||||
amount: f64,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum MarketTrend {
|
||||
Bullish,
|
||||
Bearish,
|
||||
Neutral,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum Volatility {
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct MarketOverview {
|
||||
trend: MarketTrend,
|
||||
volatility: Volatility,
|
||||
pressure: f64,
|
||||
|
||||
alerts: Vec<Alert>,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum Feed {
|
||||
Connected,
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct Status {
|
||||
feed: Feed,
|
||||
exchange: String,
|
||||
dex: String,
|
||||
latency: u16,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum SignalKind {
|
||||
Buy,
|
||||
Sell,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum SignalParameter {
|
||||
Lim,
|
||||
Stl,
|
||||
Tap,
|
||||
Chk,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct Signal {
|
||||
kind: SignalKind,
|
||||
symbol: String,
|
||||
param: SignalParameter,
|
||||
price: f64,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum LogKind {
|
||||
Info,
|
||||
Warn,
|
||||
Err,
|
||||
Debug,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct EventLog {
|
||||
kind: LogKind,
|
||||
name: String,
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum AlertLevel {
|
||||
High,
|
||||
Medium,
|
||||
Low,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub struct Alert {
|
||||
level: AlertLevel,
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[p_com]
|
||||
pub enum InspectTarget {
|
||||
None,
|
||||
Symbol(WatchListItem),
|
||||
Position(ActivePosition),
|
||||
Signal(Signal),
|
||||
Alert(Alert),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for MarketTrend {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Bullish => write!(f, "Bullish"),
|
||||
Self::Bearish => write!(f, "Bearish"),
|
||||
Self::Neutral => write!(f, "Neutral"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AlertLevel {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::High => write!(f, "H"),
|
||||
Self::Medium => write!(f, "M"),
|
||||
Self::Low => write!(f, "L"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Volatility {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Low => write!(f, "Low"),
|
||||
Self::Medium => write!(f, "Medium"),
|
||||
Self::High => write!(f, "High"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for LogKind {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Info => write!(f, "INFO"),
|
||||
Self::Warn => write!(f, "WARN"),
|
||||
Self::Err => write!(f, "ERR"),
|
||||
Self::Debug => write!(f, "DEBUG"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SignalKind {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Buy => write!(f, "BUY"),
|
||||
Self::Sell => write!(f, "SELL"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SignalParameter {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Lim => write!(f, "LIM"),
|
||||
Self::Stl => write!(f, "STL"),
|
||||
Self::Tap => write!(f, "TAP"),
|
||||
Self::Chk => write!(f, "CHK"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{PulseTradeApp, ptc::EventLog};
|
||||
use crate::PulseTradeApp;
|
||||
|
||||
impl PulseTradeApp {
|
||||
pub async fn execute_command(&mut self, ctx: &pulse_ui::state::Context, command: &str) {
|
||||
@@ -18,8 +18,8 @@ impl PulseTradeApp {
|
||||
}
|
||||
|
||||
_ => {
|
||||
self.logs.lock().await.push(EventLog {
|
||||
kind: crate::ptc::LogKind::Err,
|
||||
self.logs.lock().await.push(pulse_wire::terminal::EventLog {
|
||||
kind: pulse_wire::terminal::LogKind::Err,
|
||||
name: "cmd".to_string(),
|
||||
message: format!("Command '{}' not found", command),
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::ptc::{
|
||||
use pulse_wire::terminal::{
|
||||
ActivePosition, Alert, AlertLevel, EventLog, InspectTarget, LogKind, MarketOverview, Signal,
|
||||
SignalKind, Status, WatchListItem,
|
||||
};
|
||||
|
||||
+17
-20
@@ -1,6 +1,5 @@
|
||||
pub mod command;
|
||||
pub mod formatting;
|
||||
pub mod ptc;
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
@@ -18,12 +17,10 @@ use pulse_ui::{
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
formatting::{Formatted, apply_padding},
|
||||
ptc::{
|
||||
ActivePosition, Alert, EventLog, InspectTarget, MarketOverview, Signal, Status,
|
||||
WatchListItem,
|
||||
},
|
||||
use crate::formatting::{Formatted, apply_padding};
|
||||
|
||||
use pulse_wire::terminal::{
|
||||
ActivePosition, Alert, EventLog, InspectTarget, MarketOverview, Signal, Status, WatchListItem,
|
||||
};
|
||||
|
||||
pub struct PulseTradeApp {
|
||||
@@ -88,30 +85,30 @@ impl App for PulseTradeApp {
|
||||
let mut signals = self.signals.lock().await;
|
||||
|
||||
signals.push(Signal {
|
||||
kind: ptc::SignalKind::Buy,
|
||||
kind: pulse_wire::terminal::SignalKind::Buy,
|
||||
symbol: "BTC".to_string(),
|
||||
param: ptc::SignalParameter::Lim,
|
||||
param: pulse_wire::terminal::SignalParameter::Lim,
|
||||
price: 118_800.0,
|
||||
});
|
||||
|
||||
signals.push(Signal {
|
||||
kind: ptc::SignalKind::Buy,
|
||||
kind: pulse_wire::terminal::SignalKind::Buy,
|
||||
symbol: "BTC".to_string(),
|
||||
param: ptc::SignalParameter::Tap,
|
||||
param: pulse_wire::terminal::SignalParameter::Tap,
|
||||
price: 120_000.0,
|
||||
});
|
||||
|
||||
signals.push(Signal {
|
||||
kind: ptc::SignalKind::Buy,
|
||||
kind: pulse_wire::terminal::SignalKind::Buy,
|
||||
symbol: "BTC".to_string(),
|
||||
param: ptc::SignalParameter::Stl,
|
||||
param: pulse_wire::terminal::SignalParameter::Stl,
|
||||
price: 118_000.0,
|
||||
});
|
||||
|
||||
let mut logs = self.logs.lock().await;
|
||||
|
||||
logs.push(EventLog {
|
||||
kind: ptc::LogKind::Warn,
|
||||
kind: pulse_wire::terminal::LogKind::Warn,
|
||||
name: "pulse.init".to_string(),
|
||||
message: "We're still not done yet ;)".to_string(),
|
||||
});
|
||||
@@ -119,17 +116,17 @@ impl App for PulseTradeApp {
|
||||
let mut market_overview = self.market_overview.lock().await;
|
||||
|
||||
market_overview.alerts.push(Alert {
|
||||
level: ptc::AlertLevel::High,
|
||||
level: pulse_wire::terminal::AlertLevel::High,
|
||||
message: "BTC funding rate elevated".to_string(),
|
||||
});
|
||||
|
||||
market_overview.alerts.push(Alert {
|
||||
level: ptc::AlertLevel::Medium,
|
||||
level: pulse_wire::terminal::AlertLevel::Medium,
|
||||
message: "Market volatility increasing".to_string(),
|
||||
});
|
||||
|
||||
market_overview.alerts.push(Alert {
|
||||
level: ptc::AlertLevel::Low,
|
||||
level: pulse_wire::terminal::AlertLevel::Low,
|
||||
message: "ETH volatility returning to normal".to_string(),
|
||||
});
|
||||
}
|
||||
@@ -282,13 +279,13 @@ async fn main() {
|
||||
logs: ctx.use_state(Vec::new()),
|
||||
inspect: ctx.use_state(InspectTarget::None),
|
||||
market_overview: ctx.use_state(MarketOverview {
|
||||
trend: ptc::MarketTrend::Bullish,
|
||||
volatility: ptc::Volatility::High,
|
||||
trend: pulse_wire::terminal::MarketTrend::Bullish,
|
||||
volatility: pulse_wire::terminal::Volatility::High,
|
||||
pressure: 0.324,
|
||||
alerts: Vec::new(),
|
||||
}),
|
||||
status: ctx.use_state(Status {
|
||||
feed: ptc::Feed::Connected,
|
||||
feed: pulse_wire::terminal::Feed::Connected,
|
||||
exchange: "Binance".to_string(),
|
||||
dex: "DEX SCREENER".to_string(),
|
||||
latency: 18,
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
include!("../pc.rs");
|
||||
include!("../ptc.rs");
|
||||
Reference in New Issue
Block a user