fix: use coerceToString

This commit is contained in:
2026-01-16 10:08:29 +08:00
parent 5341ad6c27
commit e620f39a4a
8 changed files with 16 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
import type { NixValue, NixAttrs, NixString } from "../types";
import { isStringWithContext } from "../types";
import { forceNixString, forceAttrs, forceBool, forceList, forceString } from "../type-assert";
import { forceNixString, forceAttrs, forceList, forceString } from "../type-assert";
import { force } from "../thunk";
import {
type NixStringContext,

View File

@@ -130,7 +130,7 @@ export interface CoerceResult {
*/
export const coerceToString = (
value: NixValue,
mode: StringCoercionMode = StringCoercionMode.ToString,
mode: StringCoercionMode,
copyToStore: boolean = false,
outContext?: NixStringContext,
): string => {

View File

@@ -1,14 +1,9 @@
import type { NixValue, NixAttrs } from "../types";
import { forceString, forceList, forceNixString } from "../type-assert";
import { forceString, forceList } from "../type-assert";
import { force } from "../thunk";
import { type DerivationData, type OutputInfo, generateAterm } from "../derivation-helpers";
import { coerceToString, StringCoercionMode } from "./conversion";
import {
type NixStringContext,
extractInputDrvsAndSrcs,
isStringWithContext,
HAS_CONTEXT,
} from "../string-context";
import { type NixStringContext, extractInputDrvsAndSrcs, isStringWithContext } from "../string-context";
import { nixValueToJson } from "../conversion";
const forceAttrs = (value: NixValue): NixAttrs => {

View File

@@ -4,7 +4,7 @@
import { CatchableError, HAS_CONTEXT, type NixValue } from "../types";
import { force } from "../thunk";
import { forceString } from "../type-assert";
import { coerceToString, StringCoercionMode } from "./conversion";
export const seq =
(e1: NixValue) =>
@@ -34,7 +34,7 @@ export const abort = (s: NixValue): never => {
};
export const throwFunc = (s: NixValue): never => {
throw new CatchableError(forceString(s));
throw new CatchableError(coerceToString(s, StringCoercionMode.Base));
};
export const trace = (e1: NixValue, e2: NixValue): NixValue => {

View File

@@ -6,13 +6,13 @@
import { forceAttrs, forceBool, forceString } from "../type-assert";
import type { NixValue, NixAttrs } from "../types";
import { force } from "../thunk";
import { coerceToString, StringCoercionMode } from "./conversion";
// Declare Deno.core.ops global (provided by deno_core runtime)
export const importFunc = (path: NixValue): NixValue => {
// For MVP: only support string paths
// TODO: After implementing path type, also accept path values
const pathStr = forceString(path);
// TODO: context?
const pathStr = coerceToString(path, StringCoercionMode.Base);
// Call Rust op - returns JS code string
const code = Deno.core.ops.op_import(pathStr);

View File

@@ -98,12 +98,7 @@ function nextComponent(s: string, startIdx: number): ComponentResult {
}
} else {
// Otherwise, consume the longest sequence of non-digit, non-separator characters
while (
p < s.length &&
!(s[p] >= "0" && s[p] <= "9") &&
s[p] !== "." &&
s[p] !== "-"
) {
while (p < s.length && !(s[p] >= "0" && s[p] <= "9") && s[p] !== "." && s[p] !== "-") {
p++;
}
}

View File

@@ -4,6 +4,7 @@
import type { NixInt, NixValue } from "../types";
import { forceString, forceList, forceInt } from "../type-assert";
import { coerceToString, StringCoercionMode } from "./conversion";
export const stringLength = (e: NixValue): NixInt => BigInt(forceString(e).length);
@@ -20,7 +21,10 @@ export const substring =
export const concatStringsSep =
(sep: NixValue) =>
(list: NixValue): string =>
forceList(list).join(forceString(sep));
// FIXME: context?
forceList(list)
.map((elem) => coerceToString(elem, StringCoercionMode.Interpolation))
.join(forceString(sep));
export const baseNameOf = (x: NixValue): string => {
const str = forceString(x);

View File

@@ -35,6 +35,7 @@ export const nixValueToJson = (
if (seen.has(v)) {
throw new Error("derivation: circular reference detected in __structuredAttrs");
}
// FIXME: tryAttrsToString
seen.add(v);
}