From c6ba1620ad1f46df2204237d5635f9da7f7aae23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20Taha=20K=C3=B6ken?= Date: Wed, 28 Aug 2024 16:12:42 +0300 Subject: [PATCH 1/3] fix(toml): single quote issue --- packages/rust/src/utils/toml.ts | 60 +++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/packages/rust/src/utils/toml.ts b/packages/rust/src/utils/toml.ts index abb5dc4..23c22d1 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,50 @@ 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): string[] { + const lines: string[] = []; + for (const [key, value] of Object.entries(table)) { + if (isTable(value) && !TOML.isInline(value)) { + if (lines.length > 0) lines.push(''); + lines.push(`[${key}]`); + lines.push(...stringifyTable(value)); + } else { + lines.push(`${key} = ${formatValue(value)}`); + } + } + return lines; } - return tomlString; + const tomlLines = stringifyTable(cargoToml); + return tomlLines.join('\n'); } export function modifyCargoTable( From ccc31c0844a41ea04fb008ed08dd4b08c880a049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20Taha=20K=C3=B6ken?= Date: Wed, 28 Aug 2024 16:12:57 +0300 Subject: [PATCH 2/3] test: update cases --- .../src/generators/add-napi/generator.spec.ts | 22 +++--- .../src/generators/add-wasm/generator.spec.ts | 70 ++++++++----------- .../src/generators/binary/generator.spec.ts | 7 +- .../src/generators/library/generator.spec.ts | 7 +- 4 files changed, 42 insertions(+), 64 deletions(-) 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..a1bbe5f 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.release] - opt-level = 's' #Tell \`rustc\` to optimize for small code size. - " + [profile] + [release] + 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.release] - opt-level = 's' #Tell \`rustc\` to optimize for small code size. - " + [profile] + [release] + opt-level = \\"s\\"" `); }); }); diff --git a/packages/rust/src/generators/binary/generator.spec.ts b/packages/rust/src/generators/binary/generator.spec.ts index 3447aca..6fefe78 100644 --- a/packages/rust/src/generators/binary/generator.spec.ts +++ b/packages/rust/src/generators/binary/generator.spec.ts @@ -54,10 +54,9 @@ describe('rust generator', () => { const cargoToml = appTree.read('Cargo.toml')?.toString() ?? ''; expect(TOML.parse(cargoToml)).toMatchInlineSnapshot(` Object { - "profile": Object { - "release": Object { - "lto": true, - }, + "profile": Object {}, + "release": Object { + "lto": true, }, "workspace": Object { "members": Array [ diff --git a/packages/rust/src/generators/library/generator.spec.ts b/packages/rust/src/generators/library/generator.spec.ts index d13f64d..1e7f397 100644 --- a/packages/rust/src/generators/library/generator.spec.ts +++ b/packages/rust/src/generators/library/generator.spec.ts @@ -54,10 +54,9 @@ describe('rust generator', () => { const cargoToml = appTree.read('Cargo.toml')?.toString() ?? ''; expect(TOML.parse(cargoToml)).toMatchInlineSnapshot(` Object { - "profile": Object { - "release": Object { - "lto": true, - }, + "profile": Object {}, + "release": Object { + "lto": true, }, "workspace": Object { "members": Array [ From 6c0b11fdb745566b39a0f65aeb1caa1f5aadfef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20Taha=20K=C3=B6ken?= Date: Thu, 29 Aug 2024 11:16:52 +0300 Subject: [PATCH 3/3] fix(toml): nested objects --- .../src/generators/add-wasm/generator.spec.ts | 4 ++-- .../src/generators/binary/generator.spec.ts | 7 ++++--- .../src/generators/library/generator.spec.ts | 7 ++++--- packages/rust/src/utils/toml.ts | 18 ++++++------------ 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/packages/rust/src/generators/add-wasm/generator.spec.ts b/packages/rust/src/generators/add-wasm/generator.spec.ts index a1bbe5f..99b9f12 100644 --- a/packages/rust/src/generators/add-wasm/generator.spec.ts +++ b/packages/rust/src/generators/add-wasm/generator.spec.ts @@ -67,7 +67,7 @@ describe('add-wasm generator', () => { wasm-bindgen-test = \\"0.3\\" [profile] - [release] + [profile.release] opt-level = \\"s\\"" `); }); @@ -120,7 +120,7 @@ describe('add-wasm generator', () => { wasm-bindgen-test = \\"0.3\\" [profile] - [release] + [profile.release] opt-level = \\"s\\"" `); }); diff --git a/packages/rust/src/generators/binary/generator.spec.ts b/packages/rust/src/generators/binary/generator.spec.ts index 6fefe78..3447aca 100644 --- a/packages/rust/src/generators/binary/generator.spec.ts +++ b/packages/rust/src/generators/binary/generator.spec.ts @@ -54,9 +54,10 @@ describe('rust generator', () => { const cargoToml = appTree.read('Cargo.toml')?.toString() ?? ''; expect(TOML.parse(cargoToml)).toMatchInlineSnapshot(` Object { - "profile": Object {}, - "release": Object { - "lto": true, + "profile": Object { + "release": Object { + "lto": true, + }, }, "workspace": Object { "members": Array [ diff --git a/packages/rust/src/generators/library/generator.spec.ts b/packages/rust/src/generators/library/generator.spec.ts index 1e7f397..d13f64d 100644 --- a/packages/rust/src/generators/library/generator.spec.ts +++ b/packages/rust/src/generators/library/generator.spec.ts @@ -54,9 +54,10 @@ describe('rust generator', () => { const cargoToml = appTree.read('Cargo.toml')?.toString() ?? ''; expect(TOML.parse(cargoToml)).toMatchInlineSnapshot(` Object { - "profile": Object {}, - "release": Object { - "lto": true, + "profile": Object { + "release": Object { + "lto": true, + }, }, "workspace": Object { "members": Array [ diff --git a/packages/rust/src/utils/toml.ts b/packages/rust/src/utils/toml.ts index 23c22d1..6999b4b 100644 --- a/packages/rust/src/utils/toml.ts +++ b/packages/rust/src/utils/toml.ts @@ -36,12 +36,7 @@ export function parseCargoToml(cargoString: string) { 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) - ); + return typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date); } function formatValue(value: TOMLValue): string { @@ -51,9 +46,7 @@ export function stringifyCargoToml(cargoToml: CargoToml): string { return `[${value.map(formatValue).join(', ')}]`; } else if (isTable(value)) { if (TOML.isInline(value)) { - return `{ ${Object.entries(value) - .map(([k, v]) => `${k} = ${formatValue(v)}`) - .join(', ')} }`; + return `{ ${Object.entries(value).map(([k, v]) => `${k} = ${formatValue(v)}`).join(', ')} }`; } else { return ''; } @@ -62,13 +55,14 @@ export function stringifyCargoToml(cargoToml: CargoToml): string { } } - function stringifyTable(table: TOMLTable): string[] { + 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(''); - lines.push(`[${key}]`); - lines.push(...stringifyTable(value)); + const fullKey = prefix ? `${prefix}.${key}` : key; + lines.push(`[${fullKey}]`); + lines.push(...stringifyTable(value, fullKey)); } else { lines.push(`${key} = ${formatValue(value)}`); }