feat: impl FromNapiValue for serde_json::Number, fix it for Null, throw for impossible types ()

fix 

Co-authored-by: zeroslope <jsx55242@foxmail.com>
This commit is contained in:
AlCalzone 2022-02-06 04:58:17 +01:00 committed by GitHub
parent c001038852
commit ed12bd76bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 22 deletions

View file

@ -123,6 +123,7 @@ Generated by [AVA](https://avajs.dev).
}␊
export function readPackageJson(): PackageJson␊
export function getPackageJsonName(packageJson: PackageJson): string␊
export function testSerdeRoundtrip(data: any): any␊
export function contains(source: string, target: string): boolean␊
export function concatStr(s: string): string␊
export function concatUtf16(s: string): string␊

View file

@ -80,6 +80,7 @@ import {
receiveMutClassOrNumber,
getStrFromObject,
returnJsFunction,
testSerdeRoundtrip,
} from '../'
test('export const', (t) => {
@ -299,6 +300,34 @@ test('serde-json', (t) => {
t.is(getPackageJsonName(packageJson), 'napi-rs')
})
test('serde-roundtrip', (t) => {
t.is(testSerdeRoundtrip(1), 1)
t.is(testSerdeRoundtrip(1.2), 1.2)
t.is(testSerdeRoundtrip(-1), -1)
t.deepEqual(testSerdeRoundtrip([1, 1.2, -1]), [1, 1.2, -1])
t.deepEqual(testSerdeRoundtrip({ a: 1, b: 1.2, c: -1 }), {
a: 1,
b: 1.2,
c: -1,
})
t.throws(() => testSerdeRoundtrip(NaN))
t.is(testSerdeRoundtrip(null), null)
let err = t.throws(() => testSerdeRoundtrip(undefined))
t.is(err!.message, 'undefined cannot be represented as a serde_json::Value')
err = t.throws(() => testSerdeRoundtrip(() => {}))
t.is(
err!.message,
'JS functions cannot be represented as a serde_json::Value',
)
err = t.throws(() => testSerdeRoundtrip(Symbol.for('foo')))
t.is(err!.message, 'JS symbols cannot be represented as a serde_json::Value')
})
test('buffer', (t) => {
let buf = getBuffer()
t.is(buf.toString('utf-8'), 'Hello world')

View file

@ -113,6 +113,7 @@ export interface PackageJson {
}
export function readPackageJson(): PackageJson
export function getPackageJsonName(packageJson: PackageJson): string
export function testSerdeRoundtrip(data: any): any
export function contains(source: string, target: string): boolean
export function concatStr(s: string): string
export function concatUtf16(s: string): string

View file

@ -25,3 +25,8 @@ fn read_package_json() -> Result<PackageJson> {
fn get_package_json_name(package_json: PackageJson) -> String {
package_json.name
}
#[napi]
fn test_serde_roundtrip(data: Value) -> Value {
data
}