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

feat: restrict value type for builtin functions (cd, mv) to be of type Text #400

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/modules/builtin/cd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use heraclitus_compiler::prelude::*;
use crate::modules::expression::expr::Expr;
use crate::docs::module::DocumentationModule;
use crate::modules::types::{Type, Typed};
use crate::translate::module::TranslateModule;
use crate::utils::{ParserMetadata, TranslateMetadata};

Expand All @@ -20,7 +21,15 @@ impl SyntaxModule<ParserMetadata> for Cd {

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
token(meta, "cd")?;
let tok = meta.get_current_token();
syntax(meta, &mut self.value)?;
let path_type = self.value.get_type();
if path_type != Type::Text {
return error!(meta, tok => {
message: "Builtin function `cd` can only be used with values of type Text",
comment: format!("Given type: {}, expected type: {}", path_type, Type::Text)
});
}
MuhamedMagdi marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/modules/builtin/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::modules::expression::expr::Expr;
use crate::modules::condition::failed::Failed;
use crate::translate::module::TranslateModule;
use crate::docs::module::DocumentationModule;
use crate::modules::types::{Type, Typed};
use crate::utils::{ParserMetadata, TranslateMetadata};
use crate::modules::command::modifier::CommandModifier;

Expand Down Expand Up @@ -32,8 +33,24 @@ impl SyntaxModule<ParserMetadata> for Mv {
syntax(meta, &mut self.modifier)?;
self.modifier.use_modifiers(meta, |_this, meta| {
token(meta, "mv")?;
let mut tok = meta.get_current_token();
syntax(meta, &mut self.source)?;
let mut path_type = self.source.get_type();
if path_type != Type::Text {
return error!(meta, tok => {
message: "Builtin function `mv` can only be used with values of type Text",
comment: format!("Given type: {}, expected type: {}", path_type, Type::Text)
});
}
tok = meta.get_current_token();
syntax(meta, &mut self.destination)?;
path_type = self.destination.get_type();
if path_type != Type::Text {
return error!(meta, tok => {
message: "Builtin function `mv` can only be used with values of type Text",
comment: format!("Given type: {}, expected type: {}", path_type, Type::Text)
});
}
syntax(meta, &mut self.failed)?;
Ok(())
})
Expand Down