From 159267c70bf837c4dc35400bed271b514c75ecae Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sat, 3 Jan 2026 17:17:36 +0800 Subject: [PATCH] fmt: tidy --- nix-js/src/codegen.rs | 13 ++++++++++--- nix-js/src/ir/downgrade.rs | 20 ++++++++++---------- nix-js/src/runtime.rs | 38 ++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/nix-js/src/codegen.rs b/nix-js/src/codegen.rs index e719236..5634993 100644 --- a/nix-js/src/codegen.rs +++ b/nix-js/src/codegen.rs @@ -22,7 +22,8 @@ impl Compile for Ir { }, Ir::Str(s) => { // Escape string for JavaScript - let escaped = s.val + let escaped = s + .val .replace('\\', "\\\\") .replace('"', "\\\"") .replace('\n', "\\n") @@ -295,11 +296,17 @@ impl Compile for HasAttr { match attr { Attr::Str(sym) => { let key = ctx.get_sym(*sym); - current = format!("(Nix.force({}) !== null && Nix.force({}) !== undefined && \"{}\" in Nix.force({}))", current, current, key, current); + current = format!( + "(Nix.force({}) !== null && Nix.force({}) !== undefined && \"{}\" in Nix.force({}))", + current, current, key, current + ); } Attr::Dynamic(expr_id) => { let key = ctx.get_ir(*expr_id).compile(ctx); - current = format!("(Nix.force({}) !== null && Nix.force({}) !== undefined && Nix.force({}) in Nix.force({}))", current, current, key, current); + current = format!( + "(Nix.force({}) !== null && Nix.force({}) !== undefined && Nix.force({}) in Nix.force({}))", + current, current, key, current + ); } } } diff --git a/nix-js/src/ir/downgrade.rs b/nix-js/src/ir/downgrade.rs index 6a571d0..8c0581e 100644 --- a/nix-js/src/ir/downgrade.rs +++ b/nix-js/src/ir/downgrade.rs @@ -57,7 +57,9 @@ impl Downgrade for ast::Path { fn downgrade(self, ctx: &mut Ctx) -> Result { // Collect all parts and check if there are any interpolations let parts_ast: Vec<_> = self.parts().collect(); - let has_interpolation = parts_ast.iter().any(|part| matches!(part, ast::InterpolPart::Interpolation(_))); + let has_interpolation = parts_ast + .iter() + .any(|part| matches!(part, ast::InterpolPart::Interpolation(_))); let parts = if !has_interpolation { // Pure literal path - resolve at compile time @@ -87,20 +89,18 @@ impl Downgrade for ast::Path { current_dir .join(&path_str) .canonicalize() - .map_err(|e| crate::error::Error::downgrade_error( - format!("Failed to resolve path {}: {}", path_str, e) - ))? + .map_err(|e| { + crate::error::Error::downgrade_error(format!( + "Failed to resolve path {}: {}", + path_str, e + )) + })? .to_string_lossy() .to_string() }; // Return single string part with resolved path - vec![ctx.new_expr( - Str { - val: resolved_path, - } - .to_ir(), - )] + vec![ctx.new_expr(Str { val: resolved_path }.to_ir())] } else { // Path with interpolation - do NOT resolve at compile time // Keep literal parts as-is and defer resolution to runtime diff --git a/nix-js/src/runtime.rs b/nix-js/src/runtime.rs index 1643091..dc2b3ab 100644 --- a/nix-js/src/runtime.rs +++ b/nix-js/src/runtime.rs @@ -111,9 +111,7 @@ fn op_import(#[string] path: String) -> std::result::Result { CONTEXT_HOLDER.with(|holder| { let mut ptr = holder .borrow() - .ok_or_else(|| -> NixError { - "No context available".to_string().into() - })?; + .ok_or_else(|| -> NixError { "No context available".to_string().into() })?; let ctx = unsafe { ptr.as_mut() }; // 1. Resolve path relative to current file (or CWD if top-level) @@ -137,10 +135,9 @@ fn op_import(#[string] path: String) -> std::result::Result { let _guard = ImportPathGuard::push(absolute_path.clone()); // 3. Read file - let content = std::fs::read_to_string(&absolute_path) - .map_err(|e| -> NixError { - format!("Failed to read {}: {}", absolute_path.display(), e).into() - })?; + let content = std::fs::read_to_string(&absolute_path).map_err(|e| -> NixError { + format!("Failed to read {}: {}", absolute_path.display(), e).into() + })?; // 4. Parse let root = rnix::Root::parse(&content); @@ -149,22 +146,19 @@ fn op_import(#[string] path: String) -> std::result::Result { "Parse error in {}: {:?}", absolute_path.display(), root.errors() - ).into()); + ) + .into()); } // 5. Downgrade to IR let expr = root .tree() .expr() - .ok_or_else(|| -> NixError { - "No expression in file".to_string().into() - })?; + .ok_or_else(|| -> NixError { "No expression in file".to_string().into() })?; let expr_id = ctx .downgrade_ctx() .downgrade(expr) - .map_err(|e| -> NixError { - format!("Downgrade error: {}", e).into() - })?; + .map_err(|e| -> NixError { format!("Downgrade error: {}", e).into() })?; // 6. Codegen - returns JS code string Ok(ctx.get_ir(expr_id).compile(ctx)) @@ -175,9 +169,7 @@ fn op_import(#[string] path: String) -> std::result::Result { #[string] fn op_read_file(#[string] path: String) -> std::result::Result { std::fs::read_to_string(&path) - .map_err(|e| -> NixError { - format!("Failed to read {}: {}", path, e).into() - }) + .map_err(|e| -> NixError { format!("Failed to read {}: {}", path, e).into() }) } #[deno_core::op2(fast)] @@ -207,9 +199,7 @@ fn op_resolve_path(#[string] path: String) -> std::result::Result NixError { - format!("Failed to resolve path {}: {}", path, e).into() - }) + .map_err(|e| -> NixError { format!("Failed to resolve path {}: {}", path, e).into() }) } // Runtime context for V8 value conversion @@ -271,7 +261,10 @@ pub fn run(script: String, ctx: &mut Context) -> Result { // Initialize V8 once INIT.call_once(|| { - JsRuntime::init_platform(Some(v8::new_default_platform(0, false).make_shared()), false); + JsRuntime::init_platform( + Some(v8::new_default_platform(0, false).make_shared()), + false, + ); }); // Create a new JsRuntime for each evaluation to avoid state issues @@ -439,7 +432,8 @@ fn to_value_working() { run( "({ test: [1., 9223372036854775807n, true, false, 'hello world!'] - })".into(), + })" + .into(), &mut ctx ) .unwrap(),