Skip to content

Commit 572c4a5

Browse files
committed
chore: refactor, cli and rhai-common shared crate
1 parent a7264cb commit 572c4a5

File tree

120 files changed

+577
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+577
-303
lines changed

crates/lsp/bin/rhai/main.rs

-69
This file was deleted.

crates/rhai-cli/Cargo.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "rhai-cli"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
rhai-common = { version = "0.1.0", path = "../rhai-common" }
9+
rhai-lsp = { version = "0.1.0", path = "../rhai-lsp" }
10+
11+
clap = { version = "3.2.16", features = ["derive", "cargo"] }
12+
rhai = "1.8.0"
13+
anyhow = "1.0.59"
14+
async-ctrlc = { version = "1.2.0", features = ["stream"] }
15+
tracing = "0.1.36"
16+
17+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
18+
atty = "0.2.14"
19+
tokio = { version = "1.19.2", features = [
20+
"sync",
21+
"fs",
22+
"time",
23+
"io-std",
24+
"rt-multi-thread",
25+
"parking_lot",
26+
] }
27+
lsp-async-stub = { version = "0.6.0", features = ["tokio-tcp", "tokio-stdio"] }
28+
29+
[target.'cfg(target_arch = "wasm32")'.dependencies]
30+
tokio = { version = "1.19.2", features = ["sync", "parking_lot", "io-util"] }

crates/rhai-cli/src/args.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use clap::{crate_version, ArgEnum, Parser, Subcommand};
2+
3+
#[derive(Clone, Parser)]
4+
#[clap(name = "rhai")]
5+
#[clap(bin_name = "rhai")]
6+
#[clap(version = crate_version!())]
7+
pub struct RhaiArgs {
8+
#[clap(long, arg_enum, global = true, default_value = "auto")]
9+
pub colors: Colors,
10+
/// Enable a verbose logging format.
11+
#[clap(long, global = true)]
12+
pub verbose: bool,
13+
/// Enable logging spans.
14+
#[clap(long, global = true)]
15+
pub log_spans: bool,
16+
#[clap(subcommand)]
17+
pub cmd: RootCommand,
18+
}
19+
20+
#[derive(Clone, Subcommand)]
21+
pub enum RootCommand {
22+
/// Language server operations.
23+
Lsp {
24+
#[clap(subcommand)]
25+
cmd: LspCommand,
26+
},
27+
}
28+
29+
#[derive(Clone, Subcommand)]
30+
pub enum LspCommand {
31+
/// Run the language server and listen on a TCP address.
32+
Tcp {
33+
/// The address to listen on.
34+
#[clap(long, default_value = "0.0.0.0:9181")]
35+
address: String,
36+
},
37+
/// Run the language server over the standard input and output.
38+
Stdio {},
39+
}
40+
41+
#[derive(Clone, Copy, ArgEnum)]
42+
pub enum Colors {
43+
/// Determine whether to colorize output automatically.
44+
Auto,
45+
/// Always colorize output.
46+
Always,
47+
/// Never colorize output.
48+
Never,
49+
}

crates/rhai-cli/src/bin/rhai.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use clap::Parser;
2+
use rhai_cli::{
3+
args::{Colors, RhaiArgs},
4+
Rhai,
5+
};
6+
use rhai_common::{environment::native::NativeEnvironment, log::setup_stderr_logging};
7+
use std::process::exit;
8+
use tracing::Instrument;
9+
10+
#[tokio::main]
11+
async fn main() {
12+
let cli = RhaiArgs::parse();
13+
setup_stderr_logging(
14+
NativeEnvironment,
15+
cli.log_spans,
16+
cli.verbose,
17+
match cli.colors {
18+
Colors::Auto => None,
19+
Colors::Always => Some(true),
20+
Colors::Never => Some(false),
21+
},
22+
);
23+
24+
match Rhai::new(NativeEnvironment)
25+
.execute(cli)
26+
.instrument(tracing::info_span!("rhai"))
27+
.await
28+
{
29+
Ok(_) => {
30+
exit(0);
31+
}
32+
Err(error) => {
33+
tracing::error!(error = %format!("{error:#}"), "operation failed");
34+
exit(1);
35+
}
36+
}
37+
}

crates/rhai-cli/src/execute/lsp.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use rhai_common::environment::{native::NativeEnvironment, Environment};
2+
3+
use crate::{args::LspCommand, Rhai};
4+
5+
impl<E: Environment> Rhai<E> {
6+
pub async fn execute_lsp(&self, cmd: LspCommand) -> Result<(), anyhow::Error> {
7+
let server = rhai_lsp::create_server();
8+
let world = rhai_lsp::create_world(NativeEnvironment);
9+
10+
match cmd {
11+
LspCommand::Tcp { address } => {
12+
server
13+
.listen_tcp(world, &address, async_ctrlc::CtrlC::new().unwrap())
14+
.await
15+
}
16+
LspCommand::Stdio {} => {
17+
server
18+
.listen_stdio(world, async_ctrlc::CtrlC::new().unwrap())
19+
.await
20+
}
21+
}
22+
}
23+
}

crates/rhai-cli/src/execute/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::{
2+
args::{RhaiArgs, RootCommand},
3+
Rhai,
4+
};
5+
use rhai_common::environment::Environment;
6+
7+
mod lsp;
8+
9+
impl<E: Environment> Rhai<E> {
10+
pub async fn execute(&mut self, args: RhaiArgs) -> Result<(), anyhow::Error> {
11+
match args.cmd {
12+
RootCommand::Lsp { cmd } => self.execute_lsp(cmd).await,
13+
}
14+
}
15+
}

crates/rhai-cli/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub mod args;
2+
mod execute;
3+
4+
use rhai_common::environment::Environment;
5+
6+
pub struct Rhai<E: Environment> {
7+
#[allow(dead_code)]
8+
env: E,
9+
}
10+
11+
impl<E: Environment> Rhai<E> {
12+
pub fn new(env: E) -> Self {
13+
Self { env }
14+
}
15+
}

crates/rhai-common/Cargo.toml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "rhai-common"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
anyhow = "1.0.59"
10+
async-trait = "0.1.57"
11+
futures = "0.3.21"
12+
globset = "0.4.9"
13+
percent-encoding = "2.1.0"
14+
serde = { version = "1.0.142", features = ["derive"] }
15+
serde_json = "1.0.83"
16+
tracing = "0.1.36"
17+
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
18+
url = "2.2.2"
19+
20+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
21+
tokio = { version = "1.19.2", features = [
22+
"rt",
23+
"rt-multi-thread",
24+
"sync",
25+
"parking_lot",
26+
"macros",
27+
"fs",
28+
"time",
29+
] }
30+
glob = "0.3.0"
31+
atty = "0.2.14"
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
use std::path::{Path, PathBuf};
2-
3-
use figment::{providers::Serialized, Figment};
4-
use serde::{Deserialize, Serialize};
5-
use serde_json::Value;
6-
71
use crate::{
82
environment::Environment,
9-
utils::{GlobRule, Normalize},
3+
util::{GlobRule, Normalize},
104
};
5+
use serde::{Deserialize, Serialize};
6+
use std::path::Path;
117

128
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
13-
pub struct RhaiConfig {
14-
pub source: RhaiSourceConfig,
9+
pub struct Config {
10+
pub source: SourceConfig,
1511
}
1612

17-
impl RhaiConfig {
13+
impl Config {
1814
pub fn prepare(&mut self, e: &impl Environment, base: &Path) -> anyhow::Result<()> {
1915
self.source.prepare(e, base)
2016
}
2117
}
2218

2319
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
24-
pub struct RhaiSourceConfig {
20+
pub struct SourceConfig {
2521
/// A list of UNIX-style glob patterns
2622
/// for Rhai files that should be included.
2723
pub include: Option<Vec<String>>,
@@ -33,7 +29,7 @@ pub struct RhaiSourceConfig {
3329
pub file_rule: Option<GlobRule>,
3430
}
3531

36-
impl RhaiSourceConfig {
32+
impl SourceConfig {
3733
pub fn prepare(&mut self, e: &impl Environment, base: &Path) -> anyhow::Result<()> {
3834
self.make_absolute(e, base);
3935

@@ -76,54 +72,3 @@ impl RhaiSourceConfig {
7672
}
7773
}
7874
}
79-
80-
#[derive(Debug, Clone, Serialize, Deserialize)]
81-
#[serde(rename_all = "camelCase")]
82-
pub struct InitConfig {
83-
pub cache_path: Option<PathBuf>,
84-
#[serde(default = "default_configuration_section")]
85-
pub configuration_section: String,
86-
}
87-
88-
impl Default for InitConfig {
89-
fn default() -> Self {
90-
Self {
91-
cache_path: Default::default(),
92-
configuration_section: default_configuration_section(),
93-
}
94-
}
95-
}
96-
97-
fn default_configuration_section() -> String {
98-
String::from("rhai")
99-
}
100-
101-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
102-
#[serde(rename_all = "camelCase")]
103-
pub struct LspConfig {
104-
pub syntax: SyntaxConfig,
105-
}
106-
107-
impl LspConfig {
108-
pub fn update_from_json(&mut self, json: &Value) -> Result<(), anyhow::Error> {
109-
*self = Figment::new()
110-
.merge(Serialized::defaults(&self))
111-
.merge(Serialized::defaults(json))
112-
.extract()?;
113-
Ok(())
114-
}
115-
}
116-
117-
#[derive(Debug, Clone, Serialize, Deserialize)]
118-
#[serde(rename_all = "camelCase")]
119-
pub struct SyntaxConfig {
120-
pub semantic_tokens: bool,
121-
}
122-
123-
impl Default for SyntaxConfig {
124-
fn default() -> Self {
125-
Self {
126-
semantic_tokens: true,
127-
}
128-
}
129-
}

0 commit comments

Comments
 (0)