diff --git a/packages/rust/src/generators/add-napi/generator.spec.ts b/packages/rust/src/generators/add-napi/generator.spec.ts index bfc2631..e5d11b2 100644 --- a/packages/rust/src/generators/add-napi/generator.spec.ts +++ b/packages/rust/src/generators/add-napi/generator.spec.ts @@ -26,26 +26,20 @@ describe('add-napi generator', () => { await generator(appTree, options); const cargoString = appTree.read('./test/Cargo.toml')?.toString() ?? ''; expect(cargoString).toMatchInlineSnapshot(` - " - [package] - name = 'test' - version = '0.1.0' - edition = '2021' + "[package] + name = \\"test\\" + version = \\"0.1.0\\" + edition = \\"2021\\" [dependencies] - napi = { version = '2.10.2', default-features = false, features = [ - 'napi4', - ] } - napi-derive = '2.9.3' + napi = { version = \\"2.10.2\\", default-features = false, features = [\\"napi4\\"] } + napi-derive = \\"2.9.3\\" [lib] - crate-type = [ - 'cdylib', - ] + crate-type = [\\"cdylib\\"] [build-dependencies] - napi-build = '2.0.1' - " + napi-build = \\"2.0.1\\"" `); }); diff --git a/packages/rust/src/generators/add-wasm/generator.spec.ts b/packages/rust/src/generators/add-wasm/generator.spec.ts index 2a6f532..99b9f12 100644 --- a/packages/rust/src/generators/add-wasm/generator.spec.ts +++ b/packages/rust/src/generators/add-wasm/generator.spec.ts @@ -47,34 +47,28 @@ describe('add-wasm generator', () => { const cargoString = appTree.read('./test_lib/Cargo.toml')?.toString() ?? ''; expect(cargoString).toMatchInlineSnapshot(` - " - [package] - name = 'test_lib' - version = '0.1.0' - edition = '2021' + "[package] + name = \\"test_lib\\" + version = \\"0.1.0\\" + edition = \\"2021\\" [dependencies] - wasm-bindgen = '0.2' - console_error_panic_hook = { version = '0.1.6', optional = true } - wee_alloc = { version = '0.4', optional = true } + wasm-bindgen = \\"0.2\\" + console_error_panic_hook = { version = \\"0.1.6\\", optional = true } + wee_alloc = { version = \\"0.4\\", optional = true } [lib] - crate-type = [ - 'cdylib', - 'rlib', - ] + crate-type = [\\"cdylib\\", \\"rlib\\"] [feature] - default = [ - 'console_error_panic_hook', - ] + default = [\\"console_error_panic_hook\\"] [dev-dependencies] - wasm-bindgen-test = '0.3' + wasm-bindgen-test = \\"0.3\\" + [profile] [profile.release] - opt-level = 's' #Tell \`rustc\` to optimize for small code size. - " + opt-level = \\"s\\"" `); }); @@ -104,38 +98,30 @@ describe('add-wasm generator', () => { const cargoString = appTree.read('./test_lib/Cargo.toml')?.toString() ?? ''; expect(cargoString).toMatchInlineSnapshot(` - " - [package] - name = 'test_lib' - version = '0.1.0' - edition = '2021' + "[package] + name = \\"test_lib\\" + version = \\"0.1.0\\" + edition = \\"2021\\" [dependencies] - wasm-bindgen = '0.2' - js-sys = '0.3' - web-sys = { version = '0.3', features = [ - 'Window', - ] } - console_error_panic_hook = { version = '0.1.6', optional = true } - wee_alloc = { version = '0.4', optional = true } + wasm-bindgen = \\"0.2\\" + js-sys = \\"0.3\\" + web-sys = { version = \\"0.3\\", features = [\\"Window\\"] } + console_error_panic_hook = { version = \\"0.1.6\\", optional = true } + wee_alloc = { version = \\"0.4\\", optional = true } [lib] - crate-type = [ - 'cdylib', - 'rlib', - ] + crate-type = [\\"cdylib\\", \\"rlib\\"] [feature] - default = [ - 'console_error_panic_hook', - ] + default = [\\"console_error_panic_hook\\"] [dev-dependencies] - wasm-bindgen-test = '0.3' + wasm-bindgen-test = \\"0.3\\" + [profile] [profile.release] - opt-level = 's' #Tell \`rustc\` to optimize for small code size. - " + opt-level = \\"s\\"" `); }); }); diff --git a/packages/rust/src/utils/toml.ts b/packages/rust/src/utils/toml.ts index abb5dc4..6999b4b 100644 --- a/packages/rust/src/utils/toml.ts +++ b/packages/rust/src/utils/toml.ts @@ -2,6 +2,18 @@ import TOML from '@ltd/j-toml'; import { Tree, logger } from '@nx/devkit'; import { CargoToml } from '../models/cargo.toml'; +type TOMLBasicString = ReturnType; +type TOMLValue = + | string + | number + | boolean + | Date + | TOMLArray + | TOMLTable + | TOMLBasicString; +type TOMLArray = TOMLValue[]; +type TOMLTable = { [key: string]: TOMLValue }; + export function parseCargoTomlWithTree( tree: Tree, projectRoot: string, @@ -22,16 +34,44 @@ export function parseCargoToml(cargoString: string) { }) as unknown as CargoToml; } -export function stringifyCargoToml(cargoToml: CargoToml) { - const tomlString = TOML.stringify(cargoToml, { - newlineAround: 'section', - }); +export function stringifyCargoToml(cargoToml: CargoToml): string { + function isTable(value: TOMLValue): value is TOMLTable { + return typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date); + } + + function formatValue(value: TOMLValue): string { + if (typeof value === 'string') { + return value.includes('"') ? `'${value}'` : `"${value}"`; + } else if (Array.isArray(value)) { + return `[${value.map(formatValue).join(', ')}]`; + } else if (isTable(value)) { + if (TOML.isInline(value)) { + return `{ ${Object.entries(value).map(([k, v]) => `${k} = ${formatValue(v)}`).join(', ')} }`; + } else { + return ''; + } + } else { + return String(value); + } + } - if (Array.isArray(tomlString)) { - return tomlString.join('\n'); + function stringifyTable(table: TOMLTable, prefix: string = ''): string[] { + const lines: string[] = []; + for (const [key, value] of Object.entries(table)) { + if (isTable(value) && !TOML.isInline(value)) { + if (lines.length > 0) lines.push(''); + const fullKey = prefix ? `${prefix}.${key}` : key; + lines.push(`[${fullKey}]`); + lines.push(...stringifyTable(value, fullKey)); + } else { + lines.push(`${key} = ${formatValue(value)}`); + } + } + return lines; } - return tomlString; + const tomlLines = stringifyTable(cargoToml); + return tomlLines.join('\n'); } export function modifyCargoTable(