Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: single quote conversion issue #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions packages/rust/src/generators/add-napi/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\\""
`);
});

Expand Down
66 changes: 26 additions & 40 deletions packages/rust/src/generators/add-wasm/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\\""
`);
});

Expand Down Expand Up @@ -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\\""
`);
});
});
54 changes: 47 additions & 7 deletions packages/rust/src/utils/toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof TOML.basic>;
type TOMLValue =
| string
| number
| boolean
| Date
| TOMLArray
| TOMLTable
| TOMLBasicString;
type TOMLArray = TOMLValue[];
type TOMLTable = { [key: string]: TOMLValue };

export function parseCargoTomlWithTree(
tree: Tree,
projectRoot: string,
Expand All @@ -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(
Expand Down
Loading