1#![recursion_limit = "1024"]
6#![allow(clippy::boxed_local)]
7
8#[allow(unused_macros)]
9macro_rules! println {
10 ($($rest:tt)*) => {
11 #[cfg(all(feature = "disable-println", not(test)))]
12 {
13 let _ = format!($($rest)*);
14 }
15 #[cfg(any(not(feature = "disable-println"), test))]
16 std::println!($($rest)*)
17 }
18}
19
20#[allow(unused_macros)]
21macro_rules! eprintln {
22 ($($rest:tt)*) => {
23 #[cfg(all(feature = "disable-println", not(test)))]
24 {
25 let _ = format!($($rest)*);
26 }
27 #[cfg(any(not(feature = "disable-println"), test))]
28 std::eprintln!($($rest)*)
29 }
30}
31
32#[allow(unused_macros)]
33macro_rules! print {
34 ($($rest:tt)*) => {
35 #[cfg(all(feature = "disable-println", not(test)))]
36 {
37 let _ = format!($($rest)*);
38 }
39 #[cfg(any(not(feature = "disable-println"), test))]
40 std::print!($($rest)*)
41 }
42}
43
44#[allow(unused_macros)]
45macro_rules! eprint {
46 ($($rest:tt)*) => {
47 #[cfg(all(feature = "disable-println", not(test)))]
48 {
49 let _ = format!($($rest)*);
50 }
51 #[cfg(any(not(feature = "disable-println"), test))]
52 std::eprint!($($rest)*)
53 }
54}
55#[cfg(feature = "dhat-heap")]
56#[global_allocator]
57static ALLOC: dhat::Alloc = dhat::Alloc;
58
59mod coredump;
60mod docs;
61mod engine;
62mod errors;
63mod execution;
64mod fs;
65pub mod lint;
66mod log;
67mod lsp;
68mod modules;
69mod parsing;
70mod settings;
71#[cfg(test)]
72mod simulation_tests;
73mod source_range;
74pub mod std;
75#[cfg(not(target_arch = "wasm32"))]
76pub mod test_server;
77mod thread;
78mod unparser;
79mod walk;
80#[cfg(target_arch = "wasm32")]
81mod wasm;
82
83pub use coredump::CoreDump;
84pub use engine::{EngineManager, EngineStats, ExecutionKind};
85pub use errors::{
86 CompilationError, ConnectionError, ExecError, KclError, KclErrorWithOutputs, Report, ReportWithOutputs,
87};
88pub use execution::{
89 bust_cache, clear_mem_cache, ExecOutcome, ExecState, ExecutorContext, ExecutorSettings, MetaSettings, Point2d,
90};
91pub use lsp::{
92 copilot::Backend as CopilotLspBackend,
93 kcl::{Backend as KclLspBackend, Server as KclLspServerSubCommand},
94};
95pub use modules::ModuleId;
96pub use parsing::ast::{modify::modify_ast_for_sketch, types::FormatOptions};
97pub use settings::types::{project::ProjectConfiguration, Configuration, UnitLength};
98pub use source_range::SourceRange;
99#[cfg(not(target_arch = "wasm32"))]
100pub use unparser::recast_dir;
101
102pub mod exec {
105 pub use crate::execution::{ArtifactCommand, DefaultPlanes, IdGenerator, KclValue, PlaneType, Sketch};
106}
107
108#[cfg(target_arch = "wasm32")]
109pub mod wasm_engine {
110 pub use crate::{
111 coredump::wasm::{CoreDumpManager, CoreDumper},
112 engine::conn_wasm::{EngineCommandManager, EngineConnection},
113 fs::wasm::{FileManager, FileSystemManager},
114 };
115}
116
117pub mod mock_engine {
118 pub use crate::engine::conn_mock::EngineConnection;
119}
120
121#[cfg(not(target_arch = "wasm32"))]
122pub mod native_engine {
123 pub use crate::engine::conn::EngineConnection;
124}
125
126pub mod std_utils {
127 pub use crate::std::utils::{get_tangential_arc_to_info, is_points_ccw_wasm, TangentialArcInfoInput};
128}
129
130pub mod pretty {
131 pub use crate::{parsing::token::NumericSuffix, unparser::format_number};
132}
133
134use serde::{Deserialize, Serialize};
135
136#[allow(unused_imports)]
137use crate::log::{log, logln};
138
139#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
140pub struct Program {
141 #[serde(flatten)]
142 pub ast: parsing::ast::types::Node<parsing::ast::types::Program>,
143 #[serde(skip)]
147 pub original_file_contents: String,
148}
149
150#[cfg(any(test, feature = "lsp-test-util"))]
151pub use lsp::test_util::copilot_lsp_server;
152#[cfg(any(test, feature = "lsp-test-util"))]
153pub use lsp::test_util::kcl_lsp_server;
154
155impl Program {
156 pub fn parse(input: &str) -> Result<(Option<Program>, Vec<CompilationError>), KclError> {
157 let module_id = ModuleId::default();
158 let tokens = parsing::token::lex(input, module_id)?;
159 let (ast, errs) = parsing::parse_tokens(tokens).0?;
160
161 Ok((
162 ast.map(|ast| Program {
163 ast,
164 original_file_contents: input.to_string(),
165 }),
166 errs,
167 ))
168 }
169
170 pub fn parse_no_errs(input: &str) -> Result<Program, KclError> {
171 let module_id = ModuleId::default();
172 let tokens = parsing::token::lex(input, module_id)?;
173 let ast = parsing::parse_tokens(tokens).parse_errs_as_err()?;
174
175 Ok(Program {
176 ast,
177 original_file_contents: input.to_string(),
178 })
179 }
180
181 pub fn compute_digest(&mut self) -> parsing::ast::digest::Digest {
182 self.ast.compute_digest()
183 }
184
185 pub fn meta_settings(&self) -> Result<Option<crate::MetaSettings>, KclError> {
187 self.ast.meta_settings()
188 }
189
190 pub fn change_meta_settings(&self, settings: crate::MetaSettings) -> Result<Self, KclError> {
192 Ok(Self {
193 ast: self.ast.change_meta_settings(settings)?,
194 original_file_contents: self.original_file_contents.clone(),
195 })
196 }
197
198 pub fn lint_all(&self) -> Result<Vec<lint::Discovered>, anyhow::Error> {
199 self.ast.lint_all()
200 }
201
202 pub fn lint<'a>(&'a self, rule: impl lint::Rule<'a>) -> Result<Vec<lint::Discovered>, anyhow::Error> {
203 self.ast.lint(rule)
204 }
205
206 pub fn recast(&self) -> String {
207 self.ast.recast(&Default::default(), 0)
209 }
210
211 pub fn recast_with_options(&self, options: &FormatOptions) -> String {
212 self.ast.recast(options, 0)
213 }
214}
215
216#[inline]
217fn try_f64_to_usize(f: f64) -> Option<usize> {
218 let i = f as usize;
219 if i as f64 == f {
220 Some(i)
221 } else {
222 None
223 }
224}
225
226#[inline]
227fn try_f64_to_u32(f: f64) -> Option<u32> {
228 let i = f as u32;
229 if i as f64 == f {
230 Some(i)
231 } else {
232 None
233 }
234}
235
236#[inline]
237fn try_f64_to_u64(f: f64) -> Option<u64> {
238 let i = f as u64;
239 if i as f64 == f {
240 Some(i)
241 } else {
242 None
243 }
244}
245
246#[inline]
247fn try_f64_to_i64(f: f64) -> Option<i64> {
248 let i = f as i64;
249 if i as f64 == f {
250 Some(i)
251 } else {
252 None
253 }
254}
255
256pub fn version() -> &'static str {
258 env!("CARGO_PKG_VERSION")
259}
260
261#[cfg(test)]
262mod test {
263 use super::*;
264
265 #[test]
266 fn convert_int() {
267 assert_eq!(try_f64_to_usize(0.0), Some(0));
268 assert_eq!(try_f64_to_usize(42.0), Some(42));
269 assert_eq!(try_f64_to_usize(0.00000000001), None);
270 assert_eq!(try_f64_to_usize(-1.0), None);
271 assert_eq!(try_f64_to_usize(f64::NAN), None);
272 assert_eq!(try_f64_to_usize(f64::INFINITY), None);
273 assert_eq!(try_f64_to_usize((0.1 + 0.2) * 10.0), None);
274
275 assert_eq!(try_f64_to_u32(0.0), Some(0));
276 assert_eq!(try_f64_to_u32(42.0), Some(42));
277 assert_eq!(try_f64_to_u32(0.00000000001), None);
278 assert_eq!(try_f64_to_u32(-1.0), None);
279 assert_eq!(try_f64_to_u32(f64::NAN), None);
280 assert_eq!(try_f64_to_u32(f64::INFINITY), None);
281 assert_eq!(try_f64_to_u32((0.1 + 0.2) * 10.0), None);
282
283 assert_eq!(try_f64_to_u64(0.0), Some(0));
284 assert_eq!(try_f64_to_u64(42.0), Some(42));
285 assert_eq!(try_f64_to_u64(0.00000000001), None);
286 assert_eq!(try_f64_to_u64(-1.0), None);
287 assert_eq!(try_f64_to_u64(f64::NAN), None);
288 assert_eq!(try_f64_to_u64(f64::INFINITY), None);
289 assert_eq!(try_f64_to_u64((0.1 + 0.2) * 10.0), None);
290
291 assert_eq!(try_f64_to_i64(0.0), Some(0));
292 assert_eq!(try_f64_to_i64(42.0), Some(42));
293 assert_eq!(try_f64_to_i64(0.00000000001), None);
294 assert_eq!(try_f64_to_i64(-1.0), Some(-1));
295 assert_eq!(try_f64_to_i64(f64::NAN), None);
296 assert_eq!(try_f64_to_i64(f64::INFINITY), None);
297 assert_eq!(try_f64_to_i64((0.1 + 0.2) * 10.0), None);
298 }
299}