Merge nucleic/sleek-thistle-egret-fyej into dev
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "brush-experimental-builtins"
|
||||
description = "*EXPERIMENTAL builtins for a POSIX/bash shell (used by brush-shell)"
|
||||
version = "0.1.0"
|
||||
categories.workspace = true
|
||||
edition.workspace = true
|
||||
keywords.workspace = true
|
||||
license.workspace = true
|
||||
readme.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
|
||||
[features]
|
||||
default = ['builtin.save']
|
||||
'builtin.save' = ["brush-core/serde", "dep:serde_json"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
brush-core = { version = "^0.5.0", path = "../brush-core" }
|
||||
clap = { version = "4.6.0", features = ["derive", "wrap_help"] }
|
||||
serde_json = { version = "1.0.149", optional = true }
|
||||
+1
@@ -0,0 +1 @@
|
||||
../LICENSE
|
||||
@@ -0,0 +1,33 @@
|
||||
//! Experimental builtins.
|
||||
|
||||
#[cfg(feature = "builtin.save")]
|
||||
mod save;
|
||||
|
||||
#[allow(unused_imports, reason = "not all builtins are used in all configs")]
|
||||
use brush_core::builtins::{self, builtin, decl_builtin, raw_arg_builtin, simple_builtin};
|
||||
|
||||
/// Returns the set of experimental built-in commands.
|
||||
pub fn experimental_builtins<SE: brush_core::extensions::ShellExtensions>()
|
||||
-> std::collections::HashMap<String, builtins::Registration<SE>> {
|
||||
let mut m = std::collections::HashMap::<String, builtins::Registration<SE>>::new();
|
||||
|
||||
#[cfg(feature = "builtin.save")]
|
||||
m.insert("save".into(), builtin::<save::SaveCommand, SE>());
|
||||
|
||||
m
|
||||
}
|
||||
|
||||
/// Extension trait that simplifies adding experimental builtins to a shell builder.
|
||||
pub trait ShellBuilderExt {
|
||||
/// Add experimental builtins to the shell being built.
|
||||
#[must_use]
|
||||
fn experimental_builtins(self) -> Self;
|
||||
}
|
||||
|
||||
impl<SE: brush_core::extensions::ShellExtensions, S: brush_core::ShellBuilderState> ShellBuilderExt
|
||||
for brush_core::ShellBuilder<SE, S>
|
||||
{
|
||||
fn experimental_builtins(self) -> Self {
|
||||
self.builtins(crate::experimental_builtins())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
use brush_core::{ExecutionResult, builtins};
|
||||
use clap::Parser;
|
||||
use std::io::Write;
|
||||
|
||||
/// (*EXPERIMENTAL*) Serializes the current shell state to JSON and writes it to stdout.
|
||||
/// Beware that the serialized state may include sensitive information, such as any
|
||||
/// secrets stored in shell variables or referenced in command history.
|
||||
#[derive(Parser)]
|
||||
pub(crate) struct SaveCommand {}
|
||||
|
||||
impl builtins::Command for SaveCommand {
|
||||
type Error = brush_core::Error;
|
||||
|
||||
async fn execute<SE: brush_core::ShellExtensions>(
|
||||
&self,
|
||||
context: brush_core::ExecutionContext<'_, SE>,
|
||||
) -> Result<ExecutionResult, Self::Error> {
|
||||
let serialized_str = serde_json::to_string(&context.shell).map_err(|e| {
|
||||
brush_core::Error::from(brush_core::ErrorKind::InternalError(e.to_string()))
|
||||
})?;
|
||||
|
||||
writeln!(context.stdout(), "{serialized_str}")?;
|
||||
|
||||
Ok(ExecutionResult::success())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user