Skip to main content

remowt_plugin/
lib.rs

1use std::future::pending;
2
3use anyhow::Result;
4use bifrostlink::{Rpc, Rtt};
5use bifrostlink_ports::stdio::from_stdio;
6use tokio::runtime::Builder;
7
8pub mod host;
9
10pub use bifrostlink;
11pub use remowt_link_shared::{self, Address, BifConfig};
12
13pub fn plugin_index() -> Result<u16> {
14	let arg = std::env::args()
15		.nth(1)
16		.ok_or_else(|| anyhow::anyhow!("missing plugin index argument"))?;
17	arg.parse()
18		.map_err(|e| anyhow::anyhow!("invalid plugin index {arg:?}: {e}"))
19}
20
21pub fn host_address() -> Result<Address> {
22	let arg = std::env::args()
23		.nth(2)
24		.ok_or_else(|| anyhow::anyhow!("missing host address argument"))?;
25	serde_json::from_str(&arg).map_err(|e| anyhow::anyhow!("invalid host address {arg:?}: {e}"))
26}
27
28pub fn run<F>(register: F) -> Result<()>
29where
30	F: FnOnce(&mut Rpc<BifConfig>),
31{
32	tracing_subscriber::fmt()
33		.with_writer(std::io::stderr)
34		.init();
35
36	let index = plugin_index()?;
37	let host = host_address()?;
38	let runtime = Builder::new_current_thread().enable_all().build()?;
39	runtime.block_on(async move {
40		let mut rpc = Rpc::<BifConfig>::new(Address::Plugin(index));
41		rpc.add_direct(host, from_stdio(), Rtt(0));
42		register(&mut rpc);
43		let _rpc = rpc;
44		pending::<Result<()>>().await
45	})
46}