Refactor: Break functions into smaller functions (#2622)

* Factor ExecutionCtx into its own fn

* Add hyper for tests

* Further factor out functions
This commit is contained in:
Adam Chalmers
2024-06-06 16:01:41 -05:00
committed by GitHub
parent 9d083710e0
commit 197a47346a
4 changed files with 164 additions and 308 deletions

View File

@ -1152,9 +1152,9 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "hyper"
version = "0.14.28"
version = "0.14.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80"
checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33"
dependencies = [
"bytes",
"futures-channel",
@ -3444,6 +3444,7 @@ dependencies = [
"console_error_panic_hook",
"futures",
"gloo-utils",
"hyper",
"image",
"js-sys",
"kcl-lib",

View File

@ -24,6 +24,7 @@ wasm-bindgen-futures = "0.4.42"
[dev-dependencies]
anyhow = "1"
hyper = { version = "0.14.29", features = ["server", "http1"] }
image = { version = "0.25.1", default-features = false, features = ["png"] }
kittycad = { workspace = true, default-features = true }
pretty_assertions = "1.4.0"

View File

@ -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, FunctionExpression, KclNone, Value},
ast::types::{BodyItem, FunctionExpression, KclNone, Program, Value},
engine::EngineManager,
errors::{KclError, KclErrorDetails},
fs::FileManager,
@ -975,6 +975,8 @@ impl Default for PipeInfo {
}
/// The executor context.
/// Cloning will return another handle to the same engine connection/session,
/// as this uses `Arc` under the hood.
#[derive(Debug, Clone)]
pub struct ExecutorContext {
pub engine: Arc<Box<dyn EngineManager>>,
@ -1310,6 +1312,43 @@ impl ExecutorContext {
pub fn update_units(&mut self, units: crate::settings::types::UnitLength) {
self.settings.units = units;
}
/// Execute the program, then get a PNG screenshot.
pub async fn execute_and_prepare_snapshot(&self, program: Program) -> Result<kittycad::types::TakeSnapshot> {
let _ = self.run(program, None).await?;
// Zoom to fit.
self.engine
.send_modeling_cmd(
uuid::Uuid::new_v4(),
crate::executor::SourceRange::default(),
kittycad::types::ModelingCmd::ZoomToFit {
object_ids: Default::default(),
padding: 0.1,
},
)
.await?;
// Send a snapshot request to the engine.
let resp = self
.engine
.send_modeling_cmd(
uuid::Uuid::new_v4(),
crate::executor::SourceRange::default(),
kittycad::types::ModelingCmd::TakeSnapshot {
format: kittycad::types::ImageFormat::Png,
},
)
.await?;
let kittycad::types::OkWebSocketResponseData::Modeling {
modeling_response: kittycad::types::OkModelingCmdResponse::TakeSnapshot { data },
} = resp
else {
anyhow::bail!("Unexpected response from engine: {:?}", resp);
};
Ok(data)
}
}
/// For each argument given,

File diff suppressed because it is too large Load Diff