Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup exports #55

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion brush-core/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl ExpandAndEvaluate for ast::UnexpandedArithmeticExpr {
.map_err(|_e| EvalError::FailedToExpandExpression)?;

// Now parse.
let expr = brush_parser::parse_arithmetic_expression(&expanded_self)
let expr = brush_parser::arithmetic::parse(&expanded_self)
.map_err(|_e| EvalError::ParseError(expanded_self))?;

// Trace if applicable.
Expand Down
61 changes: 26 additions & 35 deletions brush-core/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use clap::builder::styling;
use clap::Parser;
use futures::future::BoxFuture;

use crate::commands::CommandArg;
use crate::context;
use crate::commands;
use crate::error;
use crate::ExecutionResult;

Expand Down Expand Up @@ -49,12 +48,11 @@ pub(crate) use minus_or_plus_flag_arg;
#[allow(clippy::module_name_repetitions)]
pub struct BuiltinResult {
/// The exit code from the command.
pub exit_code: BuiltinExitCode,
pub exit_code: ExitCode,
}

/// Exit codes for built-in commands.
#[allow(clippy::module_name_repetitions)]
pub enum BuiltinExitCode {
pub enum ExitCode {
/// The command was successful.
Success,
/// The inputs to the command were invalid.
Expand All @@ -73,20 +71,20 @@ pub enum BuiltinExitCode {
BreakLoop(u8),
}

impl From<ExecutionResult> for BuiltinExitCode {
impl From<ExecutionResult> for ExitCode {
fn from(result: ExecutionResult) -> Self {
if let Some(count) = result.continue_loop {
BuiltinExitCode::ContinueLoop(count)
ExitCode::ContinueLoop(count)
} else if let Some(count) = result.break_loop {
BuiltinExitCode::BreakLoop(count)
ExitCode::BreakLoop(count)
} else if result.return_from_function_or_script {
BuiltinExitCode::ReturnFromFunctionOrScript(result.exit_code)
ExitCode::ReturnFromFunctionOrScript(result.exit_code)
} else if result.exit_shell {
BuiltinExitCode::ExitShell(result.exit_code)
ExitCode::ExitShell(result.exit_code)
} else if result.exit_code == 0 {
BuiltinExitCode::Success
ExitCode::Success
} else {
BuiltinExitCode::Custom(result.exit_code)
ExitCode::Custom(result.exit_code)
}
}
}
Expand All @@ -97,16 +95,14 @@ impl From<ExecutionResult> for BuiltinExitCode {
///
/// * The context in which the command is being executed.
/// * The arguments to the command.
#[allow(clippy::module_name_repetitions)]
pub type BuiltinCommandExecuteFunc = fn(
context::CommandExecutionContext<'_>,
Vec<CommandArg>,
pub type CommandExecuteFunc = fn(
commands::ExecutionContext<'_>,
Vec<commands::CommandArg>,
) -> BoxFuture<'_, Result<BuiltinResult, error::Error>>;

/// Trait implemented by built-in shell commands.
#[allow(clippy::module_name_repetitions)]
#[async_trait::async_trait]
pub trait BuiltinCommand: Parser {
pub trait Command: Parser {
/// Instantiates the built-in command with the given arguments.
///
/// # Arguments
Expand Down Expand Up @@ -145,45 +141,41 @@ pub trait BuiltinCommand: Parser {
/// * `context` - The context in which the command is being executed.
async fn execute(
&self,
context: context::CommandExecutionContext<'_>,
) -> Result<BuiltinExitCode, error::Error>;
context: commands::ExecutionContext<'_>,
) -> Result<ExitCode, error::Error>;

/// Returns the textual help content associated with the command.
///
/// # Arguments
///
/// * `name` - The name of the command.
/// * `content_type` - The type of content to retrieve.
fn get_content(name: &str, content_type: BuiltinContentType) -> String {
fn get_content(name: &str, content_type: ContentType) -> String {
let mut clap_command = Self::command().styles(brush_help_styles());
clap_command.set_bin_name(name);

match content_type {
BuiltinContentType::DetailedHelp => clap_command.render_long_help().ansi().to_string(),
BuiltinContentType::ShortUsage => get_builtin_short_usage(name, &clap_command),
BuiltinContentType::ShortDescription => {
get_builtin_short_description(name, &clap_command)
}
ContentType::DetailedHelp => clap_command.render_long_help().ansi().to_string(),
ContentType::ShortUsage => get_builtin_short_usage(name, &clap_command),
ContentType::ShortDescription => get_builtin_short_description(name, &clap_command),
}
}
}

/// Trait implemented by built-in shell commands that take specially handled declarations
/// as arguments.
#[allow(clippy::module_name_repetitions)]
#[async_trait::async_trait]
pub trait BuiltinDeclarationCommand: BuiltinCommand {
pub trait DeclarationCommand: Command {
/// Stores the declarations within the command instance.
///
/// # Arguments
///
/// * `declarations` - The declarations to store.
fn set_declarations(&mut self, declarations: Vec<CommandArg>);
fn set_declarations(&mut self, declarations: Vec<commands::CommandArg>);
}

/// Type of help content, typically associated with a built-in command.
#[allow(clippy::module_name_repetitions)]
pub enum BuiltinContentType {
pub enum ContentType {
/// Detailed help content for the command.
DetailedHelp,
/// Short usage information for the command.
Expand All @@ -193,14 +185,13 @@ pub enum BuiltinContentType {
}

/// Encapsulates a registration for a built-in command.
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
pub struct BuiltinRegistration {
pub struct Registration {
/// Function to execute the builtin.
pub execute_func: BuiltinCommandExecuteFunc,
pub execute_func: CommandExecuteFunc,

/// Function to retrieve the builtin's content/help text.
pub content_func: fn(&str, BuiltinContentType) -> String,
pub content_func: fn(&str, ContentType) -> String,

/// Has this registration been disabled?
pub disabled: bool,
Expand Down
12 changes: 6 additions & 6 deletions brush-core/src/builtins/alias.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use std::io::Write;

use crate::builtin::{BuiltinCommand, BuiltinExitCode};
use crate::{builtin, commands};

/// Manage aliases within the shell.
#[derive(Parser)]
Expand All @@ -16,12 +16,12 @@ pub(crate) struct AliasCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for AliasCommand {
impl builtin::Command for AliasCommand {
async fn execute(
&self,
context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
let mut exit_code = BuiltinExitCode::Success;
context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
let mut exit_code = builtin::ExitCode::Success;

if self.print || self.aliases.is_empty() {
for (name, value) in &context.shell.aliases {
Expand All @@ -42,7 +42,7 @@ impl BuiltinCommand for AliasCommand {
"{}: {alias}: not found",
context.command_name
)?;
exit_code = BuiltinExitCode::Custom(1);
exit_code = builtin::ExitCode::Custom(1);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions brush-core/src/builtins/bg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use std::io::Write;

use crate::builtin::{BuiltinCommand, BuiltinExitCode};
use crate::{builtin, commands};

/// Moves a job to run in the background.
#[derive(Parser)]
Expand All @@ -11,12 +11,12 @@ pub(crate) struct BgCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for BgCommand {
impl builtin::Command for BgCommand {
async fn execute(
&self,
context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
let mut exit_code = BuiltinExitCode::Success;
context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
let mut exit_code = builtin::ExitCode::Success;

if !self.job_specs.is_empty() {
for job_spec in &self.job_specs {
Expand All @@ -29,15 +29,15 @@ impl BuiltinCommand for BgCommand {
context.command_name,
job_spec
)?;
exit_code = BuiltinExitCode::Custom(1);
exit_code = builtin::ExitCode::Custom(1);
}
}
} else {
if let Some(job) = context.shell.jobs.current_job_mut() {
job.move_to_background()?;
} else {
writeln!(context.stderr(), "{}: no current job", context.command_name)?;
exit_code = BuiltinExitCode::Custom(1);
exit_code = builtin::ExitCode::Custom(1);
}
}

Expand Down
12 changes: 6 additions & 6 deletions brush-core/src/builtins/break_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;

use crate::builtin::{BuiltinCommand, BuiltinExitCode};
use crate::{builtin, commands};

/// Breaks out of a control-flow loop.
#[derive(Parser)]
Expand All @@ -11,17 +11,17 @@ pub(crate) struct BreakCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for BreakCommand {
impl builtin::Command for BreakCommand {
async fn execute(
&self,
_context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
_context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
// If specified, which_loop needs to be positive.
if self.which_loop <= 0 {
return Ok(BuiltinExitCode::InvalidUsage);
return Ok(builtin::ExitCode::InvalidUsage);
}

#[allow(clippy::cast_sign_loss)]
Ok(BuiltinExitCode::BreakLoop((self.which_loop - 1) as u8))
Ok(builtin::ExitCode::BreakLoop((self.which_loop - 1) as u8))
}
}
19 changes: 8 additions & 11 deletions brush-core/src/builtins/builtin_.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use clap::Parser;
use std::io::Write;

use crate::{
builtin::{BuiltinCommand, BuiltinExitCode},
commands,
};
use crate::{builtin, commands};

/// Directly invokes a built-in, without going through typical search order.
#[derive(Parser)]
pub(crate) struct BuiltiCommand {
pub(crate) struct BuiltinCommand {
/// Name of built-in to invoke.
builtin_name: Option<String>,

Expand All @@ -18,11 +15,11 @@ pub(crate) struct BuiltiCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for BuiltiCommand {
impl builtin::Command for BuiltinCommand {
async fn execute(
&self,
mut context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
mut context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
if let Some(builtin_name) = &self.builtin_name {
if let Some(builtin) = context.shell.builtins.get(builtin_name) {
context.command_name.clone_from(builtin_name);
Expand All @@ -33,13 +30,13 @@ impl BuiltinCommand for BuiltiCommand {

(builtin.execute_func)(context, args)
.await
.map(|res: crate::builtin::BuiltinResult| res.exit_code)
.map(|res: builtin::BuiltinResult| res.exit_code)
} else {
writeln!(context.stderr(), "{builtin_name}: command not found")?;
Ok(BuiltinExitCode::Custom(1))
Ok(builtin::ExitCode::Custom(1))
}
} else {
Ok(BuiltinExitCode::Success)
Ok(builtin::ExitCode::Success)
}
}
}
14 changes: 7 additions & 7 deletions brush-core/src/builtins/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use clap::Parser;

use crate::builtin::{BuiltinCommand, BuiltinExitCode};
use crate::{builtin, commands};

/// Change the current working directory.
#[derive(Parser)]
Expand All @@ -29,11 +29,11 @@ pub(crate) struct CdCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for CdCommand {
impl builtin::Command for CdCommand {
async fn execute(
&self,
context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
// TODO: implement options
if self.force_follow_symlinks
|| self.use_physical_dir
Expand All @@ -50,18 +50,18 @@ impl BuiltinCommand for CdCommand {
PathBuf::from(home_var.to_string())
} else {
writeln!(context.stderr(), "HOME not set")?;
return Ok(BuiltinExitCode::Custom(1));
return Ok(builtin::ExitCode::Custom(1));
}
};

match context.shell.set_working_dir(&target_dir) {
Ok(()) => {}
Err(e) => {
writeln!(context.stderr(), "cd: {e}")?;
return Ok(BuiltinExitCode::Custom(1));
return Ok(builtin::ExitCode::Custom(1));
}
}

Ok(BuiltinExitCode::Success)
Ok(builtin::ExitCode::Success)
}
}
10 changes: 5 additions & 5 deletions brush-core/src/builtins/colon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;

use crate::builtin::{BuiltinCommand, BuiltinExitCode};
use crate::{builtin, commands};

/// No-op command.
#[derive(Parser)]
Expand All @@ -11,11 +11,11 @@ pub(crate) struct ColonCommand {
}

#[async_trait::async_trait]
impl BuiltinCommand for ColonCommand {
impl builtin::Command for ColonCommand {
async fn execute(
&self,
_context: crate::context::CommandExecutionContext<'_>,
) -> Result<crate::builtin::BuiltinExitCode, crate::error::Error> {
Ok(BuiltinExitCode::Success)
_context: commands::ExecutionContext<'_>,
) -> Result<crate::builtin::ExitCode, crate::error::Error> {
Ok(builtin::ExitCode::Success)
}
}
Loading
Loading