Skip to content

Commit

Permalink
Change to reuse exiting function
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Aug 13, 2024
1 parent 8f32150 commit 2930aa0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/wasm-lib/kcl/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2977,7 +2977,7 @@ impl MemberExpression {
source_ranges: vec![self.clone().into()],
})),
(being_indexed, _) => {
let t = human_friendly_type(being_indexed);
let t = human_friendly_type(&being_indexed);
Err(KclError::Semantic(KclErrorDetails {
message: format!("Only arrays and objects can be indexed, but you're trying to index a {t}"),
source_ranges: vec![self.clone().into()],
Expand Down Expand Up @@ -4071,7 +4071,7 @@ impl ConstraintLevels {
}
}

fn human_friendly_type(j: JValue) -> &'static str {
pub(crate) fn human_friendly_type(j: &JValue) -> &'static str {
match j {
JValue::Null => "null",
JValue::Bool(_) => "boolean (true/false value)",
Expand Down
13 changes: 3 additions & 10 deletions src/wasm-lib/kcl/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_json::Value as JValue;
use tower_lsp::lsp_types::{Position as LspPosition, Range as LspRange};

use crate::{
ast::types::{BodyItem, Expr, FunctionExpression, KclNone, Program, TagDeclarator},
ast::types::{human_friendly_type, BodyItem, Expr, FunctionExpression, KclNone, Program, TagDeclarator},
engine::EngineManager,
errors::{KclError, KclErrorDetails},
fs::FileManager,
Expand Down Expand Up @@ -314,16 +314,9 @@ impl KclValue {

/// Human readable type name used in error messages. Should not be relied
/// on for program logic.
pub(crate) fn display_type_name(&self) -> &'static str {
pub(crate) fn human_friendly_type(&self) -> &'static str {
match self {
KclValue::UserVal(u) => match u.value {
JValue::Null => "Null",
JValue::Bool(_) => "Bool",
JValue::Number(_) => "Number",
JValue::String(_) => "String",
JValue::Array(_) => "Array",
JValue::Object(_) => "Object",
},
KclValue::UserVal(u) => human_friendly_type(&u.value),
KclValue::TagDeclarator(_) => "TagDeclarator",
KclValue::TagIdentifier(_) => "TagIdentifier",
KclValue::SketchGroup(_) => "SketchGroup",
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/kcl/src/std/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ where
message: format!(
"Argument at index {i} was supposed to be type {} but found {}",
type_name::<T>(),
arg.display_type_name()
arg.human_friendly_type()
),
source_ranges: vec![args.source_range],
}));
Expand All @@ -482,7 +482,7 @@ where
message: format!(
"Argument at index {i} was supposed to be type {} but found {}",
type_name::<T>(),
arg.display_type_name()
arg.human_friendly_type()
),
source_ranges: vec![args.source_range],
}));
Expand Down
8 changes: 4 additions & 4 deletions src/wasm-lib/tests/executor/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ const part = rectShape([0, 0], 20, 20)
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([887, 936])], message: "Argument at index 0 was supposed to be type [f64; 2] but found String" }"#,
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([887, 936])], message: "Argument at index 0 was supposed to be type [f64; 2] but found string (text)" }"#,
);
}

Expand Down Expand Up @@ -1425,7 +1425,7 @@ const secondSketch = startSketchOn(part001, '')
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([272, 298])], message: "Argument at index 1 was supposed to be type kcl_lib::std::sketch::FaceTag but found String" }"#
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([272, 298])], message: "Argument at index 1 was supposed to be type kcl_lib::std::sketch::FaceTag but found string (text)" }"#
);
}

Expand Down Expand Up @@ -2236,7 +2236,7 @@ someFunction('INVALID')
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([37, 61]), SourceRange([65, 88])], message: "Argument at index 0 was supposed to be type kcl_lib::std::sketch::SketchData but found String" }"#
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([37, 61]), SourceRange([65, 88])], message: "Argument at index 0 was supposed to be type kcl_lib::std::sketch::SketchData but found string (text)" }"#
);
}

Expand All @@ -2257,7 +2257,7 @@ someFunction('INVALID')
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([89, 114]), SourceRange([126, 155]), SourceRange([159, 182])], message: "Argument at index 0 was supposed to be type kcl_lib::std::sketch::SketchData but found String" }"#
r#"semantic: KclErrorDetails { source_ranges: [SourceRange([89, 114]), SourceRange([126, 155]), SourceRange([159, 182])], message: "Argument at index 0 was supposed to be type kcl_lib::std::sketch::SketchData but found string (text)" }"#
);
}

Expand Down

0 comments on commit 2930aa0

Please sign in to comment.