Skip to content

Commit

Permalink
Create a WS server for a live REPL (#61)
Browse files Browse the repository at this point in the history
In this PR:
- [x] Add serde
- [x] Add serde_json
- [x] Implement `Serialize` and `Deserialize` on all public types
- [x] Add a `serve` option to `stack-cli`
  • Loading branch information
Vandesm14 authored Aug 3, 2024
1 parent 2f0802d commit 62bb818
Show file tree
Hide file tree
Showing 19 changed files with 991 additions and 147 deletions.
414 changes: 358 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ members = ["stack-core", "stack-std", "stack-cli", "stack-debugger"]

[workspace.dependencies]
unicode-segmentation = "1"
compact_str = "=0.8.0-beta"
compact_str = { version = "=0.8.0-beta", features = ["serde"] }

test-case = "3"
clap = { version = "4", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ debug file:

serve:
cd docs; mdbook serve --open

ws-serve:
cargo run -p stack-cli -- serve

ws-connect:
rlwrap websocat ws://localhost:5001
5 changes: 5 additions & 0 deletions stack-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ stack-core = { path = "../stack-core" }
stack-std = { path = "../stack-std", optional = true }
codespan-reporting = "0.11.1"

# server
serde = { workspace = true }
ws = { version = "0.9.2" }
serde_json.workspace = true

[[bin]]
name = "stack"
path = "src/main.rs"
58 changes: 58 additions & 0 deletions stack-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use core::fmt;
use std::io::{self, prelude::Write};

use crossterm::{
cursor::{self, MoveTo},
style::Print,
terminal, QueueableCommand,
};
use stack_core::prelude::*;

pub mod server;

pub fn ok_or_exit<T, E>(result: Result<T, E>) -> T
where
E: fmt::Display,
{
match result {
Ok(x) => x,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}

pub fn print_stack(context: &Context) {
print!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| print!("{sep}{x:#}"));

println!()
}

pub fn eprint_stack(context: &Context) {
eprint!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| eprint!("{sep}{x:#}"));

eprintln!()
}

pub fn clear_screen() -> io::Result<()> {
let mut stdout = std::io::stdout();

stdout.queue(cursor::Hide)?;
let (_, num_lines) = terminal::size()?;
for _ in 0..2 * num_lines {
stdout.queue(Print("\n"))?;
}
stdout.queue(MoveTo(0, 0))?;
stdout.queue(cursor::Show)?;

stdout.flush()
}
62 changes: 8 additions & 54 deletions stack-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::fmt;
use std::{
io::{self, prelude::Write, Read},
io::Read,
path::{Path, PathBuf},
sync::Arc,
};
Expand All @@ -14,15 +13,13 @@ use codespan_reporting::{
termcolor::{ColorChoice, StandardStream},
},
};
use crossterm::{
cursor::{self, MoveTo},
style::Print,
terminal, QueueableCommand,
};
use notify::{
Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal};
use stack_cli::{
clear_screen, eprint_stack, ok_or_exit, print_stack, server::listen,
};
use stack_core::prelude::*;

fn main() {
Expand Down Expand Up @@ -223,56 +220,10 @@ fn main() {
}
}
}
Subcommand::Serve => listen(),
}
}

fn ok_or_exit<T, E>(result: Result<T, E>) -> T
where
E: fmt::Display,
{
match result {
Ok(x) => x,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}

fn print_stack(context: &Context) {
print!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| print!("{sep}{x:#}"));

println!()
}

fn eprint_stack(context: &Context) {
eprint!("stack:");

core::iter::repeat(" ")
.zip(context.stack())
.for_each(|(sep, x)| eprint!("{sep}{x:#}"));

eprintln!()
}

fn clear_screen() -> io::Result<()> {
let mut stdout = std::io::stdout();

stdout.queue(cursor::Hide)?;
let (_, num_lines) = terminal::size()?;
for _ in 0..2 * num_lines {
stdout.queue(Print("\n"))?;
}
stdout.queue(MoveTo(0, 0))?;
stdout.queue(cursor::Show)?;

stdout.flush()
}

#[derive(Debug, Clone, PartialEq, Eq, Default, clap::Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand Down Expand Up @@ -329,4 +280,7 @@ enum Subcommand {
#[arg(short, long)]
watch: bool,
},

// TODO: add host and port as options
Serve,
}
Loading

0 comments on commit 62bb818

Please sign in to comment.