Per-file units (use per-file settings for conversion functions) (#5064)
* Use the project default units for the per-file unit default values Signed-off-by: Nick Cameron <nrc@ncameron.org> * Use per-file units in conversion functions Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -815,7 +815,7 @@ fn generate_code_block_test(fn_name: &str, code_block: &str, index: usize) -> pr
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new(&ctx.settings)).await {
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", #fn_name, #index),
|
||||
|
@ -14,7 +14,10 @@ mod test_examples_someFn {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "someFn", 0usize),
|
||||
|
@ -14,7 +14,10 @@ mod test_examples_someFn {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "someFn", 0usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_show {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "show", 0usize),
|
||||
@ -69,7 +72,10 @@ mod test_examples_show {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "show", 1usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_show {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "show", 0usize),
|
||||
|
@ -16,7 +16,10 @@ mod test_examples_my_func {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "my_func", 0usize),
|
||||
@ -70,7 +73,10 @@ mod test_examples_my_func {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "my_func", 1usize),
|
||||
|
@ -16,7 +16,10 @@ mod test_examples_line_to {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "line_to", 0usize),
|
||||
@ -70,7 +73,10 @@ mod test_examples_line_to {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "line_to", 1usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_min {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "min", 0usize),
|
||||
@ -69,7 +72,10 @@ mod test_examples_min {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "min", 1usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_show {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "show", 0usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_import {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "import", 0usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_import {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "import", 0usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_import {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "import", 0usize),
|
||||
|
@ -15,7 +15,10 @@ mod test_examples_show {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "show", 0usize),
|
||||
|
@ -14,7 +14,10 @@ mod test_examples_some_function {
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
|
||||
if let Err(e) = ctx
|
||||
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
|
||||
.await
|
||||
{
|
||||
return Err(miette::Report::new(crate::errors::Report {
|
||||
error: e,
|
||||
filename: format!("{}{}", "some_function", 0usize),
|
||||
|
@ -164,7 +164,7 @@ async fn snapshot_endpoint(body: Bytes, state: ExecutorContext) -> Response<Body
|
||||
};
|
||||
|
||||
eprintln!("Executing {test_name}");
|
||||
let mut exec_state = ExecState::new();
|
||||
let mut exec_state = ExecState::new(&state.settings);
|
||||
// This is a shitty source range, I don't know what else to use for it though.
|
||||
// There's no actual KCL associated with this reset_scene call.
|
||||
if let Err(e) = state
|
||||
|
@ -16,7 +16,7 @@ pub async fn kcl_to_engine_core(code: &str) -> Result<String> {
|
||||
let ctx = ExecutorContext::new_forwarded_mock(Arc::new(Box::new(
|
||||
crate::conn_mock_core::EngineConnection::new(ref_result).await?,
|
||||
)));
|
||||
ctx.run(program.into(), &mut ExecState::new()).await?;
|
||||
ctx.run(program.into(), &mut ExecState::new(&ctx.settings)).await?;
|
||||
|
||||
let result = result.lock().expect("mutex lock").clone();
|
||||
Ok(result)
|
||||
|
@ -30,13 +30,16 @@ impl From<KclErrorWithOutputs> for ExecError {
|
||||
#[derive(Debug)]
|
||||
pub struct ExecErrorWithState {
|
||||
pub error: ExecError,
|
||||
pub exec_state: crate::ExecState,
|
||||
pub exec_state: Option<crate::ExecState>,
|
||||
}
|
||||
|
||||
impl ExecErrorWithState {
|
||||
#[cfg_attr(target_arch = "wasm32", expect(dead_code))]
|
||||
pub fn new(error: ExecError, exec_state: crate::ExecState) -> Self {
|
||||
Self { error, exec_state }
|
||||
Self {
|
||||
error,
|
||||
exec_state: Some(exec_state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +47,7 @@ impl From<ExecError> for ExecErrorWithState {
|
||||
fn from(error: ExecError) -> Self {
|
||||
Self {
|
||||
error,
|
||||
exec_state: Default::default(),
|
||||
exec_state: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -53,7 +56,7 @@ impl From<ConnectionError> for ExecErrorWithState {
|
||||
fn from(error: ConnectionError) -> Self {
|
||||
Self {
|
||||
error: error.into(),
|
||||
exec_state: Default::default(),
|
||||
exec_state: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -601,10 +601,24 @@ impl TryFrom<NumericSuffix> for UnitLen {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)]
|
||||
impl From<crate::UnitLength> for UnitLen {
|
||||
fn from(unit: crate::UnitLength) -> Self {
|
||||
match unit {
|
||||
crate::UnitLength::Cm => UnitLen::Cm,
|
||||
crate::UnitLength::Ft => UnitLen::Feet,
|
||||
crate::UnitLength::In => UnitLen::Inches,
|
||||
crate::UnitLength::M => UnitLen::M,
|
||||
crate::UnitLength::Mm => UnitLen::Mm,
|
||||
crate::UnitLength::Yd => UnitLen::Yards,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum UnitAngle {
|
||||
#[default]
|
||||
Degrees,
|
||||
Radians,
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ type Point2D = kcmc::shared::Point2d<f64>;
|
||||
type Point3D = kcmc::shared::Point3d<f64>;
|
||||
|
||||
pub use function_param::FunctionParam;
|
||||
pub use kcl_value::{KclObjectFields, KclValue};
|
||||
pub use kcl_value::{KclObjectFields, KclValue, UnitAngle, UnitLen};
|
||||
use uuid::Uuid;
|
||||
|
||||
mod annotations;
|
||||
@ -77,7 +77,7 @@ pub struct GlobalState {
|
||||
pub artifact_commands: Vec<ArtifactCommand>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ModuleState {
|
||||
/// Program variable bindings.
|
||||
@ -115,21 +115,15 @@ pub struct ExecOutcome {
|
||||
pub artifact_commands: Vec<ArtifactCommand>,
|
||||
}
|
||||
|
||||
impl Default for ExecState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExecState {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(exec_settings: &ExecutorSettings) -> Self {
|
||||
ExecState {
|
||||
global: GlobalState::new(),
|
||||
mod_local: ModuleState::default(),
|
||||
mod_local: ModuleState::new(exec_settings),
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
fn reset(&mut self, exec_settings: &ExecutorSettings) {
|
||||
let mut id_generator = self.global.id_generator.clone();
|
||||
// We do not pop the ids, since we want to keep the same id generator.
|
||||
// This is for the front end to keep track of the ids.
|
||||
@ -140,7 +134,7 @@ impl ExecState {
|
||||
|
||||
*self = ExecState {
|
||||
global,
|
||||
mod_local: ModuleState::default(),
|
||||
mod_local: ModuleState::new(exec_settings),
|
||||
};
|
||||
}
|
||||
|
||||
@ -204,6 +198,14 @@ impl ExecState {
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn length_unit(&self) -> UnitLen {
|
||||
self.mod_local.settings.default_length_units
|
||||
}
|
||||
|
||||
pub fn angle_unit(&self) -> UnitAngle {
|
||||
self.mod_local.settings.default_angle_units
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
@ -232,6 +234,23 @@ impl GlobalState {
|
||||
}
|
||||
}
|
||||
|
||||
impl ModuleState {
|
||||
fn new(exec_settings: &ExecutorSettings) -> Self {
|
||||
ModuleState {
|
||||
memory: Default::default(),
|
||||
dynamic_state: Default::default(),
|
||||
pipe_value: Default::default(),
|
||||
module_exports: Default::default(),
|
||||
import_stack: Default::default(),
|
||||
operations: Default::default(),
|
||||
settings: MetaSettings {
|
||||
default_length_units: exec_settings.units.into(),
|
||||
default_angle_units: Default::default(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@ -240,15 +259,6 @@ pub struct MetaSettings {
|
||||
pub default_angle_units: kcl_value::UnitAngle,
|
||||
}
|
||||
|
||||
impl Default for MetaSettings {
|
||||
fn default() -> Self {
|
||||
MetaSettings {
|
||||
default_length_units: kcl_value::UnitLen::Mm,
|
||||
default_angle_units: kcl_value::UnitAngle::Degrees,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MetaSettings {
|
||||
fn update_from_annotation(&mut self, annotation: &NonCodeValue, source_range: SourceRange) -> Result<(), KclError> {
|
||||
let properties = annotations::expect_properties(annotations::SETTINGS, annotation, source_range)?;
|
||||
@ -1711,7 +1721,7 @@ pub struct ExecutorContext {
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct ExecutorSettings {
|
||||
/// The unit to use in modeling dimensions.
|
||||
/// The project-default unit to use in modeling dimensions.
|
||||
pub units: UnitLength,
|
||||
/// Highlight edges of 3D objects?
|
||||
pub highlight_edges: bool,
|
||||
@ -2214,7 +2224,7 @@ impl ExecutorContext {
|
||||
|
||||
if cache_result.clear_scene && !self.is_mock() {
|
||||
// Pop the execution state, since we are starting fresh.
|
||||
exec_state.reset();
|
||||
exec_state.reset(&self.settings);
|
||||
|
||||
// We don't do this in mock mode since there is no engine connection
|
||||
// anyways and from the TS side we override memory and don't want to clear it.
|
||||
@ -2457,7 +2467,7 @@ impl ExecutorContext {
|
||||
|
||||
let mut local_state = ModuleState {
|
||||
import_stack: exec_state.mod_local.import_stack.clone(),
|
||||
..Default::default()
|
||||
..ModuleState::new(&self.settings)
|
||||
};
|
||||
local_state.import_stack.push(info.path.clone());
|
||||
std::mem::swap(&mut exec_state.mod_local, &mut local_state);
|
||||
@ -2830,7 +2840,7 @@ mod tests {
|
||||
settings: Default::default(),
|
||||
context_type: ContextType::Mock,
|
||||
};
|
||||
let mut exec_state = ExecState::default();
|
||||
let mut exec_state = ExecState::new(&ctx.settings);
|
||||
ctx.run(program.clone().into(), &mut exec_state).await?;
|
||||
|
||||
Ok((program, ctx, exec_state))
|
||||
@ -3302,6 +3312,25 @@ let shape = layer() |> patternTransform(10, transform, %)
|
||||
assert_eq!(7.4, mem_get_json(exec_state.memory(), "thing").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_unit_default() {
|
||||
let ast = r#"const inMm = 25.4 * mm()
|
||||
const inInches = 1.0 * inch()"#;
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(25.4, mem_get_json(exec_state.memory(), "inMm").as_f64().unwrap());
|
||||
assert_eq!(25.4, mem_get_json(exec_state.memory(), "inInches").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_unit_overriden() {
|
||||
let ast = r#"@settings(defaultLengthUnit = inch)
|
||||
const inMm = 25.4 * mm()
|
||||
const inInches = 1.0 * inch()"#;
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(1.0, mem_get_json(exec_state.memory(), "inMm").as_f64().unwrap().round());
|
||||
assert_eq!(1.0, mem_get_json(exec_state.memory(), "inInches").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_zero_param_fn() {
|
||||
let ast = r#"const sigmaAllow = 35000 // psi
|
||||
@ -4045,7 +4074,7 @@ shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
.unwrap();
|
||||
let old_program = crate::Program::parse_no_errs(code).unwrap();
|
||||
// Execute the program.
|
||||
let mut exec_state = Default::default();
|
||||
let mut exec_state = ExecState::new(&ctx.settings);
|
||||
let cache_info = crate::CacheInformation {
|
||||
old: None,
|
||||
new_ast: old_program.ast.clone(),
|
||||
|
@ -49,7 +49,7 @@ use crate::{
|
||||
token::TokenStream,
|
||||
PIPE_OPERATOR,
|
||||
},
|
||||
CacheInformation, ModuleId, OldAstState, Program, SourceRange,
|
||||
CacheInformation, ExecState, ModuleId, OldAstState, Program, SourceRange,
|
||||
};
|
||||
const SEMANTIC_TOKEN_TYPES: [SemanticTokenType; 10] = [
|
||||
SemanticTokenType::NUMBER,
|
||||
@ -693,7 +693,7 @@ impl Backend {
|
||||
let mut exec_state = if let Some(last_successful_ast_state) = last_successful_ast_state.clone() {
|
||||
last_successful_ast_state.exec_state
|
||||
} else {
|
||||
Default::default()
|
||||
ExecState::new(&executor_ctx.settings)
|
||||
};
|
||||
|
||||
if let Err(err) = executor_ctx
|
||||
|
@ -96,25 +96,6 @@ impl Args {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn new_test_args() -> Result<Self> {
|
||||
use std::sync::Arc;
|
||||
|
||||
Ok(Self {
|
||||
args: Vec::new(),
|
||||
kw_args: Default::default(),
|
||||
source_range: SourceRange::default(),
|
||||
ctx: ExecutorContext {
|
||||
engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await?)),
|
||||
fs: Arc::new(crate::fs::FileManager::new()),
|
||||
stdlib: Arc::new(crate::std::StdLib::new()),
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
},
|
||||
pipe_value: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a keyword argument. If not set, returns None.
|
||||
pub(crate) fn get_kw_arg_opt<'a, T>(&'a self, label: &str) -> Option<T>
|
||||
where
|
||||
|
@ -5,14 +5,13 @@ use derive_docs::stdlib;
|
||||
|
||||
use crate::{
|
||||
errors::KclError,
|
||||
execution::{ExecState, KclValue},
|
||||
settings::types::UnitLength,
|
||||
execution::{ExecState, KclValue, UnitLen},
|
||||
std::Args,
|
||||
};
|
||||
|
||||
/// Millimeters conversion factor for current projects units.
|
||||
pub async fn mm(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_mm(&args)?;
|
||||
pub async fn mm(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_mm(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -40,20 +39,20 @@ pub async fn mm(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
|
||||
name = "mm",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_mm(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(1.0),
|
||||
UnitLength::In => Ok(measurements::Length::from_millimeters(1.0).as_inches()),
|
||||
UnitLength::Ft => Ok(measurements::Length::from_millimeters(1.0).as_feet()),
|
||||
UnitLength::M => Ok(measurements::Length::from_millimeters(1.0).as_meters()),
|
||||
UnitLength::Cm => Ok(measurements::Length::from_millimeters(1.0).as_centimeters()),
|
||||
UnitLength::Yd => Ok(measurements::Length::from_millimeters(1.0).as_yards()),
|
||||
fn inner_mm(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(1.0),
|
||||
UnitLen::Inches => Ok(measurements::Length::from_millimeters(1.0).as_inches()),
|
||||
UnitLen::Feet => Ok(measurements::Length::from_millimeters(1.0).as_feet()),
|
||||
UnitLen::M => Ok(measurements::Length::from_millimeters(1.0).as_meters()),
|
||||
UnitLen::Cm => Ok(measurements::Length::from_millimeters(1.0).as_centimeters()),
|
||||
UnitLen::Yards => Ok(measurements::Length::from_millimeters(1.0).as_yards()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inches conversion factor for current projects units.
|
||||
pub async fn inch(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_inch(&args)?;
|
||||
pub async fn inch(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_inch(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -81,20 +80,20 @@ pub async fn inch(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
|
||||
name = "inch",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_inch(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(measurements::Length::from_inches(1.0).as_millimeters()),
|
||||
UnitLength::In => Ok(1.0),
|
||||
UnitLength::Ft => Ok(measurements::Length::from_inches(1.0).as_feet()),
|
||||
UnitLength::M => Ok(measurements::Length::from_inches(1.0).as_meters()),
|
||||
UnitLength::Cm => Ok(measurements::Length::from_inches(1.0).as_centimeters()),
|
||||
UnitLength::Yd => Ok(measurements::Length::from_inches(1.0).as_yards()),
|
||||
fn inner_inch(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(measurements::Length::from_inches(1.0).as_millimeters()),
|
||||
UnitLen::Inches => Ok(1.0),
|
||||
UnitLen::Feet => Ok(measurements::Length::from_inches(1.0).as_feet()),
|
||||
UnitLen::M => Ok(measurements::Length::from_inches(1.0).as_meters()),
|
||||
UnitLen::Cm => Ok(measurements::Length::from_inches(1.0).as_centimeters()),
|
||||
UnitLen::Yards => Ok(measurements::Length::from_inches(1.0).as_yards()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Feet conversion factor for current projects units.
|
||||
pub async fn ft(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_ft(&args)?;
|
||||
pub async fn ft(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_ft(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -123,20 +122,20 @@ pub async fn ft(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
|
||||
name = "ft",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_ft(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(measurements::Length::from_feet(1.0).as_millimeters()),
|
||||
UnitLength::In => Ok(measurements::Length::from_feet(1.0).as_inches()),
|
||||
UnitLength::Ft => Ok(1.0),
|
||||
UnitLength::M => Ok(measurements::Length::from_feet(1.0).as_meters()),
|
||||
UnitLength::Cm => Ok(measurements::Length::from_feet(1.0).as_centimeters()),
|
||||
UnitLength::Yd => Ok(measurements::Length::from_feet(1.0).as_yards()),
|
||||
fn inner_ft(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(measurements::Length::from_feet(1.0).as_millimeters()),
|
||||
UnitLen::Inches => Ok(measurements::Length::from_feet(1.0).as_inches()),
|
||||
UnitLen::Feet => Ok(1.0),
|
||||
UnitLen::M => Ok(measurements::Length::from_feet(1.0).as_meters()),
|
||||
UnitLen::Cm => Ok(measurements::Length::from_feet(1.0).as_centimeters()),
|
||||
UnitLen::Yards => Ok(measurements::Length::from_feet(1.0).as_yards()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Meters conversion factor for current projects units.
|
||||
pub async fn m(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_m(&args)?;
|
||||
pub async fn m(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_m(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -165,20 +164,20 @@ pub async fn m(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclE
|
||||
name = "m",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_m(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(measurements::Length::from_meters(1.0).as_millimeters()),
|
||||
UnitLength::In => Ok(measurements::Length::from_meters(1.0).as_inches()),
|
||||
UnitLength::Ft => Ok(measurements::Length::from_meters(1.0).as_feet()),
|
||||
UnitLength::M => Ok(1.0),
|
||||
UnitLength::Cm => Ok(measurements::Length::from_meters(1.0).as_centimeters()),
|
||||
UnitLength::Yd => Ok(measurements::Length::from_meters(1.0).as_yards()),
|
||||
fn inner_m(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(measurements::Length::from_meters(1.0).as_millimeters()),
|
||||
UnitLen::Inches => Ok(measurements::Length::from_meters(1.0).as_inches()),
|
||||
UnitLen::Feet => Ok(measurements::Length::from_meters(1.0).as_feet()),
|
||||
UnitLen::M => Ok(1.0),
|
||||
UnitLen::Cm => Ok(measurements::Length::from_meters(1.0).as_centimeters()),
|
||||
UnitLen::Yards => Ok(measurements::Length::from_meters(1.0).as_yards()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Centimeters conversion factor for current projects units.
|
||||
pub async fn cm(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_cm(&args)?;
|
||||
pub async fn cm(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_cm(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -207,20 +206,20 @@ pub async fn cm(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
|
||||
name = "cm",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_cm(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(measurements::Length::from_centimeters(1.0).as_millimeters()),
|
||||
UnitLength::In => Ok(measurements::Length::from_centimeters(1.0).as_inches()),
|
||||
UnitLength::Ft => Ok(measurements::Length::from_centimeters(1.0).as_feet()),
|
||||
UnitLength::M => Ok(measurements::Length::from_centimeters(1.0).as_meters()),
|
||||
UnitLength::Cm => Ok(1.0),
|
||||
UnitLength::Yd => Ok(measurements::Length::from_centimeters(1.0).as_yards()),
|
||||
fn inner_cm(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(measurements::Length::from_centimeters(1.0).as_millimeters()),
|
||||
UnitLen::Inches => Ok(measurements::Length::from_centimeters(1.0).as_inches()),
|
||||
UnitLen::Feet => Ok(measurements::Length::from_centimeters(1.0).as_feet()),
|
||||
UnitLen::M => Ok(measurements::Length::from_centimeters(1.0).as_meters()),
|
||||
UnitLen::Cm => Ok(1.0),
|
||||
UnitLen::Yards => Ok(measurements::Length::from_centimeters(1.0).as_yards()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Yards conversion factor for current projects units.
|
||||
pub async fn yd(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_yd(&args)?;
|
||||
pub async fn yd(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let result = inner_yd(exec_state)?;
|
||||
|
||||
Ok(args.make_user_val_from_f64(result))
|
||||
}
|
||||
@ -249,92 +248,13 @@ pub async fn yd(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
|
||||
name = "yd",
|
||||
tags = ["units"],
|
||||
}]
|
||||
fn inner_yd(args: &Args) -> Result<f64, KclError> {
|
||||
match args.ctx.settings.units {
|
||||
UnitLength::Mm => Ok(measurements::Length::from_yards(1.0).as_millimeters()),
|
||||
UnitLength::In => Ok(measurements::Length::from_yards(1.0).as_inches()),
|
||||
UnitLength::Ft => Ok(measurements::Length::from_yards(1.0).as_feet()),
|
||||
UnitLength::M => Ok(measurements::Length::from_yards(1.0).as_meters()),
|
||||
UnitLength::Cm => Ok(measurements::Length::from_yards(1.0).as_centimeters()),
|
||||
UnitLength::Yd => Ok(1.0),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_mm() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_mm(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::In;
|
||||
let result = inner_mm(&args).unwrap();
|
||||
assert_eq!(result, 1.0 / 25.4);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_inch() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::In;
|
||||
let result = inner_inch(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_inch(&args).unwrap();
|
||||
assert_eq!(result, 25.4);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_ft() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::Ft;
|
||||
let result = inner_ft(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_ft(&args).unwrap();
|
||||
assert_eq!(result, 304.8);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_m() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::M;
|
||||
let result = inner_m(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_m(&args).unwrap();
|
||||
assert_eq!(result, 1000.0);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_cm() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::Cm;
|
||||
let result = inner_cm(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_cm(&args).unwrap();
|
||||
assert_eq!(result, 10.0);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_units_inner_yd() {
|
||||
let mut args = Args::new_test_args().await.unwrap();
|
||||
args.ctx.settings.units = UnitLength::Yd;
|
||||
let result = inner_yd(&args).unwrap();
|
||||
assert_eq!(result, 1.0);
|
||||
|
||||
args.ctx.settings.units = UnitLength::Mm;
|
||||
let result = inner_yd(&args).unwrap();
|
||||
assert_eq!(result, 914.4);
|
||||
fn inner_yd(exec_state: &ExecState) -> Result<f64, KclError> {
|
||||
match exec_state.length_unit() {
|
||||
UnitLen::Mm => Ok(measurements::Length::from_yards(1.0).as_millimeters()),
|
||||
UnitLen::Inches => Ok(measurements::Length::from_yards(1.0).as_inches()),
|
||||
UnitLen::Feet => Ok(measurements::Length::from_yards(1.0).as_feet()),
|
||||
UnitLen::M => Ok(measurements::Length::from_yards(1.0).as_meters()),
|
||||
UnitLen::Cm => Ok(measurements::Length::from_yards(1.0).as_centimeters()),
|
||||
UnitLen::Yards => Ok(1.0),
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use crate::{
|
||||
errors::ExecErrorWithState,
|
||||
execution::{new_zoo_client, ArtifactCommand, ExecutorContext, ExecutorSettings, Operation, ProgramMemory},
|
||||
settings::types::UnitLength,
|
||||
ConnectionError, ExecError, KclErrorWithOutputs, Program,
|
||||
ConnectionError, ExecError, ExecState, KclErrorWithOutputs, Program,
|
||||
};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
@ -72,7 +72,7 @@ async fn do_execute_and_snapshot(
|
||||
ctx: &ExecutorContext,
|
||||
program: Program,
|
||||
) -> Result<(crate::execution::ExecState, image::DynamicImage), ExecErrorWithState> {
|
||||
let mut exec_state = Default::default();
|
||||
let mut exec_state = ExecState::new(&ctx.settings);
|
||||
let snapshot_png_bytes = ctx
|
||||
.execute_and_prepare_snapshot(&program, &mut exec_state)
|
||||
.await
|
||||
|
@ -80,7 +80,7 @@ pub async fn execute(
|
||||
kcl_lib::ExecutorContext::new(engine_manager, fs_manager, settings.into()).await?
|
||||
};
|
||||
|
||||
let mut exec_state = ExecState::default();
|
||||
let mut exec_state = ExecState::new(&ctx.settings);
|
||||
let mut old_ast_memory = None;
|
||||
|
||||
// Populate from the old exec state if it exists.
|
||||
|
@ -18,7 +18,7 @@ async fn cache_test(
|
||||
.ok_or_else(|| anyhow::anyhow!("No variations provided for test '{}'", test_name))?;
|
||||
|
||||
let mut ctx = kcl_lib::ExecutorContext::new_with_client(first.settings.clone(), None, None).await?;
|
||||
let mut exec_state = kcl_lib::ExecState::default();
|
||||
let mut exec_state = kcl_lib::ExecState::new(&ctx.settings);
|
||||
|
||||
let mut old_ast_state = None;
|
||||
let mut img_results = Vec::new();
|
||||
|
@ -10,7 +10,7 @@ use pretty_assertions::assert_eq;
|
||||
async fn setup(code: &str, name: &str) -> Result<(ExecutorContext, Program, ModuleId, uuid::Uuid)> {
|
||||
let program = Program::parse_no_errs(code)?;
|
||||
let ctx = kcl_lib::ExecutorContext::new_with_default_client(Default::default()).await?;
|
||||
let mut exec_state = ExecState::default();
|
||||
let mut exec_state = ExecState::new(&ctx.settings);
|
||||
ctx.run(program.clone().into(), &mut exec_state).await?;
|
||||
|
||||
// We need to get the sketch ID.
|
||||
|
Reference in New Issue
Block a user