KCL refactor: combine two fields into one struct (#4689)
This commit is contained in:
@ -13,7 +13,10 @@ use crate::{
|
|||||||
ObjectExpression, PipeExpression, TagDeclarator, UnaryExpression, UnaryOperator,
|
ObjectExpression, PipeExpression, TagDeclarator, UnaryExpression, UnaryOperator,
|
||||||
},
|
},
|
||||||
source_range::SourceRange,
|
source_range::SourceRange,
|
||||||
std::{args::Arg, FunctionKind},
|
std::{
|
||||||
|
args::{Arg, KwArgs},
|
||||||
|
FunctionKind,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const FLOAT_TO_INT_MAX_DELTA: f64 = 0.01;
|
const FLOAT_TO_INT_MAX_DELTA: f64 = 0.01;
|
||||||
@ -388,7 +391,14 @@ impl Node<CallExpressionKw> {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let args = crate::std::Args::new_kw(fn_args, unlabeled, self.into(), ctx.clone());
|
let args = crate::std::Args::new_kw(
|
||||||
|
KwArgs {
|
||||||
|
unlabeled,
|
||||||
|
labeled: fn_args,
|
||||||
|
},
|
||||||
|
self.into(),
|
||||||
|
ctx.clone(),
|
||||||
|
);
|
||||||
match ctx.stdlib.get_either(fn_name) {
|
match ctx.stdlib.get_either(fn_name) {
|
||||||
FunctionKind::Core(func) => {
|
FunctionKind::Core(func) => {
|
||||||
// Attempt to call the function.
|
// Attempt to call the function.
|
||||||
|
|||||||
@ -42,14 +42,19 @@ impl Arg {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct KwArgs {
|
||||||
|
/// Unlabeled keyword args. Currently only the first arg can be unlabeled.
|
||||||
|
pub unlabeled: Option<Arg>,
|
||||||
|
/// Labeled args.
|
||||||
|
pub labeled: HashMap<String, Arg>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// Positional args.
|
/// Positional args.
|
||||||
pub args: Vec<Arg>,
|
pub args: Vec<Arg>,
|
||||||
/// Keyword args.
|
pub kw_args: KwArgs,
|
||||||
pub kw_args: HashMap<String, Arg>,
|
|
||||||
/// Unlabeled keyword args. Currently only the first arg can be unlabeled.
|
|
||||||
pub unlabeled_kw_arg: Option<Arg>,
|
|
||||||
pub source_range: SourceRange,
|
pub source_range: SourceRange,
|
||||||
pub ctx: ExecutorContext,
|
pub ctx: ExecutorContext,
|
||||||
}
|
}
|
||||||
@ -59,23 +64,16 @@ impl Args {
|
|||||||
Self {
|
Self {
|
||||||
args,
|
args,
|
||||||
kw_args: Default::default(),
|
kw_args: Default::default(),
|
||||||
unlabeled_kw_arg: Default::default(),
|
|
||||||
source_range,
|
source_range,
|
||||||
ctx,
|
ctx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collect the given keyword arguments.
|
/// Collect the given keyword arguments.
|
||||||
pub fn new_kw(
|
pub fn new_kw(kw_args: KwArgs, source_range: SourceRange, ctx: ExecutorContext) -> Self {
|
||||||
kw_args: HashMap<String, Arg>,
|
|
||||||
unlabeled_kw_arg: Option<Arg>,
|
|
||||||
source_range: SourceRange,
|
|
||||||
ctx: ExecutorContext,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
args: Default::default(),
|
args: Default::default(),
|
||||||
kw_args,
|
kw_args,
|
||||||
unlabeled_kw_arg,
|
|
||||||
source_range,
|
source_range,
|
||||||
ctx,
|
ctx,
|
||||||
}
|
}
|
||||||
@ -88,7 +86,6 @@ impl Args {
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
args: Vec::new(),
|
args: Vec::new(),
|
||||||
kw_args: Default::default(),
|
kw_args: Default::default(),
|
||||||
unlabeled_kw_arg: Default::default(),
|
|
||||||
source_range: SourceRange::default(),
|
source_range: SourceRange::default(),
|
||||||
ctx: ExecutorContext {
|
ctx: ExecutorContext {
|
||||||
engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await?)),
|
engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await?)),
|
||||||
@ -105,7 +102,10 @@ impl Args {
|
|||||||
where
|
where
|
||||||
T: FromKclValue<'a>,
|
T: FromKclValue<'a>,
|
||||||
{
|
{
|
||||||
self.kw_args.get(label).and_then(|arg| T::from_kcl_val(&arg.value))
|
self.kw_args
|
||||||
|
.labeled
|
||||||
|
.get(label)
|
||||||
|
.and_then(|arg| T::from_kcl_val(&arg.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a keyword argument. If not set, returns Err.
|
/// Get a keyword argument. If not set, returns Err.
|
||||||
@ -126,7 +126,7 @@ impl Args {
|
|||||||
where
|
where
|
||||||
T: FromKclValue<'a>,
|
T: FromKclValue<'a>,
|
||||||
{
|
{
|
||||||
let Some(ref arg) = self.unlabeled_kw_arg else {
|
let Some(ref arg) = self.kw_args.unlabeled else {
|
||||||
return Err(KclError::Semantic(KclErrorDetails {
|
return Err(KclError::Semantic(KclErrorDetails {
|
||||||
source_ranges: vec![self.source_range],
|
source_ranges: vec![self.source_range],
|
||||||
message: format!("This function requires a value for the special unlabeled first parameter, '{label}'"),
|
message: format!("This function requires a value for the special unlabeled first parameter, '{label}'"),
|
||||||
|
|||||||
Reference in New Issue
Block a user