Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ syntect-assets = "0.23.6"
pulldown-cmark = "0.12.2"
notify = "8"

# TEMPORARY: track the xs feat/engine-base branch for `Store::with_base_engine`
# (xs PR #144). Return to a published version once that lands and is released.
[dependencies.cross-stream]
version = "0.13.3"
git = "https://github.com/cablehead/xs"
branch = "feat/engine-base"
optional = true

[features]
Expand Down
20 changes: 17 additions & 3 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ impl Engine {
};
let res = xs::nu::add_core_commands(&mut xe, store)
.and_then(|()| xs::nu::add_read_commands(&mut xe, store, xs::nu::ReadMode::Stream))
.and_then(|()| {
xs::nu::add_write_commands(&mut xe, store, xs::nu::AppendMode::Direct)
});
.and_then(|()| xs::nu::add_write_commands(&mut xe, store, xs::nu::AppendMode::Direct));
self.state = xe.state;
res.map_err(|e| Error::from(e.to_string()))
}
Expand All @@ -389,6 +387,22 @@ impl Engine {
Box::new(MjCompileCommand::with_store(store.clone())),
])
}

/// Add http-nu's `.mj` (store-backed), `.md`, and highlight commands to this
/// engine, to use as an xs processor base via `Store::with_base_engine` so
/// actors, services, and actions can use them. See xs ADR 0007.
#[cfg(feature = "cross-stream")]
pub fn add_processor_commands(&mut self, store: &xs::store::Store) -> Result<(), Error> {
self.add_commands(vec![
Box::new(MjCommand::with_store(store.clone())),
Box::new(MjCompileCommand::with_store(store.clone())),
Box::new(MjRenderCommand::new()),
Box::new(MdCommand::new()),
Box::new(HighlightCommand::new()),
Box::new(HighlightThemeCommand::new()),
Box::new(HighlightLangCommand::new()),
])
}
}

/// Creates an engine from a script by cloning a base engine and parsing the closure.
Expand Down
11 changes: 11 additions & 0 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ impl Store {
) -> Result<Self, xs::store::StoreError> {
let inner = xs::store::Store::new(path.clone())?;

// Hand the processors a base engine with http-nu's `.mj`, `.md`, and
// highlight commands so actors, services, and actions can use them, not
// just HTTP handlers. prepared_base clones this base per spawn. See xs
// ADR 0007. Set before any clone so all share it.
let inner = {
let mut base = crate::Engine::new().expect("Failed to build processor base engine");
base.add_processor_commands(&inner)
.expect("Failed to register processor base commands");
inner.with_base_engine(base.state)
};

// API server
let store_for_api = inner.clone();
tokio::spawn(async move {
Expand Down
27 changes: 27 additions & 0 deletions src/test_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ fn eval_engine() -> Engine {
engine
}

#[cfg(feature = "cross-stream")]
#[test]
fn test_processor_base_command_surface() {
use nu_protocol::engine::StateWorkingSet;

let tmp = tempfile::TempDir::new().unwrap();
let store = xs::store::Store::new(tmp.path().to_path_buf()).unwrap();
let mut engine = Engine::new().unwrap();
engine.add_processor_commands(&store).unwrap();

// .mj and .md are present...
let ws = StateWorkingSet::new(&engine.state);
assert!(
ws.find_decl(b".mj").is_some(),
".mj must be on the processor base"
);
assert!(
ws.find_decl(b".md").is_some(),
".md must be on the processor base"
);
// ...and .static, an HTTP-handler command, is not.
assert!(
ws.find_decl(b".static").is_none(),
".static must not be on the processor base"
);
}

#[test]
fn test_engine_eval() {
let mut engine = Engine::new().unwrap();
Expand Down
Loading