Skip to main content

hx_plugins/api/
project.rs

1//! Project information API for plugins.
2//!
3//! Provides: (hx/project-name), (hx/project-root), (hx/ghc-version), etc.
4
5use crate::context::with_context;
6use crate::error::Result;
7use steel::SteelVal;
8use steel::steel_vm::engine::Engine;
9use steel::steel_vm::register_fn::RegisterFn;
10
11/// Register project API functions.
12pub fn register(engine: &mut Engine) -> Result<()> {
13    engine.register_fn("hx/project-name", project_name);
14    engine.register_fn("hx/project-root", project_root);
15    engine.register_fn("hx/ghc-version", ghc_version);
16    engine.register_fn("hx/cabal-file", cabal_file);
17    Ok(())
18}
19
20/// Get the project name.
21fn project_name() -> SteelVal {
22    with_context(|ctx| SteelVal::StringV(ctx.project_name.clone().into()))
23        .unwrap_or(SteelVal::BoolV(false))
24}
25
26/// Get the project root directory.
27fn project_root() -> SteelVal {
28    with_context(|ctx| SteelVal::StringV(ctx.project_root.to_string_lossy().to_string().into()))
29        .unwrap_or(SteelVal::BoolV(false))
30}
31
32/// Get the GHC version.
33fn ghc_version() -> SteelVal {
34    with_context(|ctx| {
35        ctx.ghc_version
36            .as_ref()
37            .map(|v| SteelVal::StringV(v.clone().into()))
38            .unwrap_or(SteelVal::BoolV(false))
39    })
40    .unwrap_or(SteelVal::BoolV(false))
41}
42
43/// Get the cabal file path.
44fn cabal_file() -> SteelVal {
45    with_context(|ctx| {
46        ctx.cabal_file
47            .as_ref()
48            .map(|p| SteelVal::StringV(p.to_string_lossy().to_string().into()))
49            .unwrap_or(SteelVal::BoolV(false))
50    })
51    .unwrap_or(SteelVal::BoolV(false))
52}