fmt: tidy

This commit is contained in:
2026-01-03 17:17:36 +08:00
parent 40884c21ad
commit 159267c70b
3 changed files with 36 additions and 35 deletions

View File

@@ -22,7 +22,8 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
}, },
Ir::Str(s) => { Ir::Str(s) => {
// Escape string for JavaScript // Escape string for JavaScript
let escaped = s.val let escaped = s
.val
.replace('\\', "\\\\") .replace('\\', "\\\\")
.replace('"', "\\\"") .replace('"', "\\\"")
.replace('\n', "\\n") .replace('\n', "\\n")
@@ -295,11 +296,17 @@ impl<Ctx: CodegenContext> Compile<Ctx> for HasAttr {
match attr { match attr {
Attr::Str(sym) => { Attr::Str(sym) => {
let key = ctx.get_sym(*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) => { Attr::Dynamic(expr_id) => {
let key = ctx.get_ir(*expr_id).compile(ctx); 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
);
} }
} }
} }

View File

@@ -57,7 +57,9 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Path {
fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId> { fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId> {
// Collect all parts and check if there are any interpolations // Collect all parts and check if there are any interpolations
let parts_ast: Vec<_> = self.parts().collect(); 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 { let parts = if !has_interpolation {
// Pure literal path - resolve at compile time // Pure literal path - resolve at compile time
@@ -87,20 +89,18 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Path {
current_dir current_dir
.join(&path_str) .join(&path_str)
.canonicalize() .canonicalize()
.map_err(|e| crate::error::Error::downgrade_error( .map_err(|e| {
format!("Failed to resolve path {}: {}", path_str, e) crate::error::Error::downgrade_error(format!(
))? "Failed to resolve path {}: {}",
path_str, e
))
})?
.to_string_lossy() .to_string_lossy()
.to_string() .to_string()
}; };
// Return single string part with resolved path // Return single string part with resolved path
vec![ctx.new_expr( vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]
Str {
val: resolved_path,
}
.to_ir(),
)]
} else { } else {
// Path with interpolation - do NOT resolve at compile time // Path with interpolation - do NOT resolve at compile time
// Keep literal parts as-is and defer resolution to runtime // Keep literal parts as-is and defer resolution to runtime

View File

@@ -111,9 +111,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
CONTEXT_HOLDER.with(|holder| { CONTEXT_HOLDER.with(|holder| {
let mut ptr = holder let mut ptr = holder
.borrow() .borrow()
.ok_or_else(|| -> NixError { .ok_or_else(|| -> NixError { "No context available".to_string().into() })?;
"No context available".to_string().into()
})?;
let ctx = unsafe { ptr.as_mut() }; let ctx = unsafe { ptr.as_mut() };
// 1. Resolve path relative to current file (or CWD if top-level) // 1. Resolve path relative to current file (or CWD if top-level)
@@ -137,8 +135,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
let _guard = ImportPathGuard::push(absolute_path.clone()); let _guard = ImportPathGuard::push(absolute_path.clone());
// 3. Read file // 3. Read file
let content = std::fs::read_to_string(&absolute_path) let content = std::fs::read_to_string(&absolute_path).map_err(|e| -> NixError {
.map_err(|e| -> NixError {
format!("Failed to read {}: {}", absolute_path.display(), e).into() format!("Failed to read {}: {}", absolute_path.display(), e).into()
})?; })?;
@@ -149,22 +146,19 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
"Parse error in {}: {:?}", "Parse error in {}: {:?}",
absolute_path.display(), absolute_path.display(),
root.errors() root.errors()
).into()); )
.into());
} }
// 5. Downgrade to IR // 5. Downgrade to IR
let expr = root let expr = root
.tree() .tree()
.expr() .expr()
.ok_or_else(|| -> NixError { .ok_or_else(|| -> NixError { "No expression in file".to_string().into() })?;
"No expression in file".to_string().into()
})?;
let expr_id = ctx let expr_id = ctx
.downgrade_ctx() .downgrade_ctx()
.downgrade(expr) .downgrade(expr)
.map_err(|e| -> NixError { .map_err(|e| -> NixError { format!("Downgrade error: {}", e).into() })?;
format!("Downgrade error: {}", e).into()
})?;
// 6. Codegen - returns JS code string // 6. Codegen - returns JS code string
Ok(ctx.get_ir(expr_id).compile(ctx)) Ok(ctx.get_ir(expr_id).compile(ctx))
@@ -175,9 +169,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
#[string] #[string]
fn op_read_file(#[string] path: String) -> std::result::Result<String, NixError> { fn op_read_file(#[string] path: String) -> std::result::Result<String, NixError> {
std::fs::read_to_string(&path) std::fs::read_to_string(&path)
.map_err(|e| -> NixError { .map_err(|e| -> NixError { format!("Failed to read {}: {}", path, e).into() })
format!("Failed to read {}: {}", path, e).into()
})
} }
#[deno_core::op2(fast)] #[deno_core::op2(fast)]
@@ -207,9 +199,7 @@ fn op_resolve_path(#[string] path: String) -> std::result::Result<String, NixErr
.join(&path) .join(&path)
.canonicalize() .canonicalize()
.map(|p| p.to_string_lossy().to_string()) .map(|p| p.to_string_lossy().to_string())
.map_err(|e| -> NixError { .map_err(|e| -> NixError { format!("Failed to resolve path {}: {}", path, e).into() })
format!("Failed to resolve path {}: {}", path, e).into()
})
} }
// Runtime context for V8 value conversion // Runtime context for V8 value conversion
@@ -271,7 +261,10 @@ pub fn run(script: String, ctx: &mut Context) -> Result<Value> {
// Initialize V8 once // Initialize V8 once
INIT.call_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 // Create a new JsRuntime for each evaluation to avoid state issues
@@ -439,7 +432,8 @@ fn to_value_working() {
run( run(
"({ "({
test: [1., 9223372036854775807n, true, false, 'hello world!'] test: [1., 9223372036854775807n, true, false, 'hello world!']
})".into(), })"
.into(),
&mut ctx &mut ctx
) )
.unwrap(), .unwrap(),