MCPcopy Create free account
hub / github.com/chirpstack/chirpstack / new

Method new

chirpstack/src/adr/plugin.rs:17–58  ·  view source on GitHub ↗
(file_path: &str)

Source from the content-addressed store, hash-verified

15
16impl Plugin {
17 pub fn new(file_path: &str) -> Result<Self> {
18 let rt = rquickjs::Runtime::new()?;
19 let ctx = rquickjs::Context::full(&rt)?;
20 let script = fs::read_to_string(file_path).context("Read ADR plugin")?;
21
22 let (id, name) = ctx.with::<_, Result<(String, String)>>(|ctx| {
23 let m = rquickjs::Module::declare(ctx.clone(), "script", script.clone())
24 .catch(&ctx)
25 .map_err(|e| anyhow!("Declare script: JS error: {}", e))?;
26 let (m, m_promise) = m
27 .eval()
28 .catch(&ctx)
29 .map_err(|e| anyhow!("Evaluate script: JS error: {}", e))?;
30 () = m_promise
31 .finish()
32 .catch(&ctx)
33 .map_err(|e| anyhow!("Evaluate script: JS error: {}", e))?;
34 let id_func: rquickjs::Function = m
35 .get("id")
36 .catch(&ctx)
37 .map_err(|e| anyhow!("Get id function: JS error: {}", e))?;
38 let name_func: rquickjs::Function = m
39 .get("name")
40 .catch(&ctx)
41 .map_err(|e| anyhow!("Get name function: JS error: {}", e))?;
42
43 let id: String = id_func
44 .call(())
45 .catch(&ctx)
46 .map_err(|e| anyhow!("Call id function: JS error: {}", e))?;
47 let name: String = name_func
48 .call(())
49 .catch(&ctx)
50 .map_err(|e| anyhow!("Call name function: JS error: {}", e))?;
51
52 Ok((id, name))
53 })?;
54
55 let p = Plugin { script, id, name };
56
57 Ok(p)
58 }
59}
60
61#[async_trait]

Callers

nothing calls this directly

Calls 2

getMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected