Skip to content

Commit

Permalink
refactor: cleanup stew and example plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar authored and buffet committed Mar 14, 2023
1 parent 1259cfd commit 713f582
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 69 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bazed-input-mapper = { path = "crates/bazed-input-mapper", version = "0.0.0" }
bazed-stew-macros = { path = "crates/bazed-stew-macros", version = "0.0.0" }
bazed-stew-interface = { path = "crates/bazed-stew-interface", version = "0.0.0" }
bazed-stew = { path = "crates/bazed-stew", version = "0.0.0" }
copilot-interface = { path = "crates/copilot-interface", version = "0.0.0" }
example-plugin-interface = { path = "crates/example-plugin-interface", version = "0.0.0" }

[workspace.dependencies.tracing-subscriber]
version = "0.3.16"
Expand Down
6 changes: 3 additions & 3 deletions crates/bazed-stew/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl PluginExecutable {
return None;
}
let file_name = path.file_name()?.to_string_lossy();
let version = file_name.split('=').nth(1)?.to_string();
let version = file_name.split('@').nth(1)?.to_string();
Version::parse(&version).is_ok().then_some(Self(path))
}

Expand All @@ -33,12 +33,12 @@ impl PluginExecutable {

pub fn name(&self) -> String {
let file_name = self.0.file_name().unwrap().to_string_lossy();
file_name.split('=').next().unwrap().to_string()
file_name.split('@').next().unwrap().to_string()
}

pub fn version(&self) -> Version {
let file_name = self.0.file_name().unwrap().to_string_lossy();
let version = file_name.split('=').nth(1).unwrap().to_string();
let version = file_name.split('@').nth(1).unwrap().to_string();
Version::parse(&version).unwrap()
}

Expand Down
8 changes: 4 additions & 4 deletions crates/bazed-stew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ pub async fn run_stew(load_path: Vec<PathBuf>) {
plugins: Arc::new(DashMap::new()),
rpc_call_send,
};
let copilot = search_plugin(&load_path, "copilot", &"*".parse().unwrap());
if let Some(copilot) = copilot {
stew.start_plugin(&copilot).await;
let example_plugin = search_plugin(&load_path, "example-plugin", &"*".parse().unwrap());
if let Some(example_plugin) = example_plugin {
stew.start_plugin(&example_plugin).await;
} else {
tracing::error!("Failed to find copilot plugin");
tracing::error!("Failed to find example plugin");
}
tracing::info!("Starting to listen to rpc calls");

Expand Down
2 changes: 0 additions & 2 deletions crates/copilot-interface/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions crates/copilot-interface/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "copilot-impl"
name = "example-plugin-impl"
version = "0.0.0"
edition = "2021"
description = "The bazed editor's github copilot plugin"
description = "A basic example of a plugin implementation"
authors.workspace = true
categories.workspace = true
keywords.workspace = true
Expand All @@ -21,5 +21,5 @@ tokio.workspace = true
tracing-subscriber.workspace = true
tracing-error.workspace = true

copilot-interface.workspace = true
example-plugin-interface.workspace = true
color-eyre.workspace = true
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use copilot_interface::CopilotClient;
use example_plugin_interface::ExamplePluginClient;
use tracing_error::ErrorLayer;
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

struct Plugin {
counter: usize,
}

#[async_trait::async_trait]
impl copilot_interface::Copilot for Plugin {
async fn plus(&mut self, n: usize) {
impl example_plugin_interface::ExamplePlugin for Plugin {
async fn increase(&mut self, n: usize) {
self.counter += n;
}

async fn minus(&mut self, n: usize) -> Result<(), String> {
async fn decrease(&mut self, n: usize) -> Result<(), String> {
if self.counter < n {
Err("Can't subtract more than the current value".to_string())
} else {
self.counter -= n;
Ok(())
}
}

async fn value(&mut self) -> usize {
self.counter
}
Expand All @@ -30,22 +29,24 @@ impl copilot_interface::Copilot for Plugin {
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
init_logging();
tracing::info!("Copilot started");
tracing::info!("Example plugin started");
let plugin = Plugin { counter: 0 };
let mut stew_session = bazed_stew_interface::init_session_with_state(plugin);
tracing::info!("Stew session running");

copilot_interface::server::initialize(&mut stew_session).await?;
example_plugin_interface::server::initialize(&mut stew_session).await?;
tracing::info!("Initialized");

let mut other_plugin = CopilotClient::load(stew_session.clone()).await?;
let mut other_plugin = ExamplePluginClient::load(stew_session.clone()).await?;

other_plugin.plus(5).await?;
other_plugin.plus(5).await?;
other_plugin.increase(5).await?;
other_plugin.increase(5).await?;
let result = other_plugin.value().await?;
assert_eq!(result, 10);
tracing::info!("Result value: {result:?}");
let result = other_plugin.minus(15).await?;
let result = other_plugin.decrease(15).await?;
tracing::info!("Result minus: {result:?}");
assert!(result.is_err());

loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "copilot-interface"
name = "example-plugin-interface"
version = "0.0.0"
edition = "2021"
description = "A basic example of a plugin implementation"
authors.workspace = true
categories.workspace = true
keywords.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions crates/example-plugin-interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[bazed_stew_macros::plugin(name = "example-plugin", version = "0.1.0", stew_version = "0.1")]
#[async_trait::async_trait]
pub trait ExamplePlugin {
/// Add a value to the counter.
async fn increase(&mut self, n: usize);
/// Subtract a value from the counter. Fails if the counter would be < 0.
async fn decrease(&mut self, n: usize) -> Result<(), String>;
/// Get the current value.
async fn value(&mut self) -> usize;
}

0 comments on commit 713f582

Please sign in to comment.