Skip to content

Commit

Permalink
Make some type signatures more permissive
Browse files Browse the repository at this point in the history
  • Loading branch information
stepchowfun committed Jun 19, 2024
1 parent e98fe44 commit 25ccf6e
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 65 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.12.1] - 2024-06-19

### Changed
- The TypeScript deserialization functions now have a more permissive type signature.

## [0.12.0] - 2024-06-19

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typical"
version = "0.12.0"
version = "0.12.1"
authors = ["Stephan Boyer <[email protected]>"]
edition = "2021"
description = "Data interchange with algebraic data types."
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ let message = SendEmailRequestOut {
body: "It makes serialization easy and safe.".to_owned(),
};

let file = BufWriter::new(File::create(FILE_PATH)?);
message.serialize(file)?;
let mut file = BufWriter::new(File::create(REQUEST_FILE_PATH)?);
message.serialize(&mut file)?;
file.flush()?;
```

Another program could read the file and deserialize the message as follows:
Expand Down
20 changes: 11 additions & 9 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod types;
use {
std::{
fs::{remove_file, File},
io::{self, BufReader, BufWriter},
io::{self, BufReader, BufWriter, Write},
},
types::{
types::{
Expand All @@ -16,7 +16,7 @@ use {
const REQUEST_FILE_PATH: &str = "/tmp/request";
const RESPONSE_FILE_PATH: &str = "/tmp/response";

fn write_to_file() -> io::Result<()> {
fn write_to_files() -> io::Result<()> {
let request_message = SendEmailRequestOut {
to: "[email protected]".to_owned(),
subject: "I love Typical!".to_owned(),
Expand All @@ -25,14 +25,16 @@ fn write_to_file() -> io::Result<()> {

let response_message = SendEmailResponseOut::Error("Example error".to_string());

let request_file = BufWriter::new(File::create(REQUEST_FILE_PATH)?);
request_message.serialize(request_file)?;
let mut request_file = BufWriter::new(File::create(REQUEST_FILE_PATH)?);
request_message.serialize(&mut request_file)?;
request_file.flush()?;

let response_file = BufWriter::new(File::create(RESPONSE_FILE_PATH)?);
response_message.serialize(response_file)
let mut response_file = BufWriter::new(File::create(RESPONSE_FILE_PATH)?);
response_message.serialize(&mut response_file)?;
response_file.flush()
}

fn read_from_file() -> io::Result<()> {
fn read_from_files() -> io::Result<()> {
let request_file = BufReader::new(File::open(REQUEST_FILE_PATH)?);
let request_message = SendEmailRequestIn::deserialize(request_file)?;

Expand All @@ -52,8 +54,8 @@ fn read_from_file() -> io::Result<()> {
}

fn main() -> io::Result<()> {
write_to_file()?;
read_from_file()?;
write_to_files()?;
read_from_files()?;
remove_file(REQUEST_FILE_PATH)?;
remove_file(RESPONSE_FILE_PATH)
}
26 changes: 6 additions & 20 deletions examples/typescript/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const requestFilePath = '/tmp/request';
const responseFilePath = '/tmp/response';
const { SendEmailRequest, SendEmailResponse } = Types;

function writeToFile(): void {
function writeToFiles(): void {
const requestMessage = {
to: '[email protected]',
subject: 'I love Typical!',
Expand All @@ -23,29 +23,15 @@ function writeToFile(): void {
writeFileSync(responseFilePath, Buffer.from(responseArrayBuffer));
}

function readFromFile(): void {
function readFromFiles(): void {
const requestFileContents = readFileSync(requestFilePath);
const requestMessage = SendEmailRequest.deserialize(
new DataView(
requestFileContents.buffer,
requestFileContents.byteOffset,
requestFileContents.byteLength,
),
);

const requestMessage = SendEmailRequest.deserialize(requestFileContents);
if (requestMessage instanceof Error) {
throw requestMessage;
}

const responseFileContents = readFileSync(responseFilePath);
const responseMessage = SendEmailResponse.deserialize(
new DataView(
responseFileContents.buffer,
responseFileContents.byteOffset,
responseFileContents.byteLength,
),
);

const responseMessage = SendEmailResponse.deserialize(responseFileContents);
if (responseMessage instanceof Error) {
throw responseMessage;
}
Expand All @@ -72,7 +58,7 @@ function readFromFile(): void {
return undefined; // To satisfy ESLint's `consistent-return` rule
}

writeToFile();
readFromFile();
writeToFiles();
readFromFiles();
unlinkSync(requestFilePath);
unlinkSync(responseFilePath);
4 changes: 2 additions & 2 deletions integration_tests/typescript_node/src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ try {
export function assertMatch<O, I>(
size: (message: O) => number,
serialize: (message: O) => ArrayBuffer,
deserialize: (dataView: DataView) => I,
deserialize: (bytes: DataView | Uint8Array) => I,
actual: O,
expected: unknown,
): void {
Expand Down Expand Up @@ -44,7 +44,7 @@ export function assertMatch<O, I>(
export function assertRoundTrip<O, I, V extends O>(
size: (message: O) => number,
serialize: (message: O) => ArrayBuffer,
deserialize: (dataView: DataView) => I,
deserialize: (bytes: DataView | Uint8Array) => I,
message: V,
): void {
assertMatch(size, serialize, deserialize, message, message);
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/typescript_web/src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function deepStrictEqual<T, U>(x: T, y: U): void {
export function assertMatch<O, I>(
size: (message: O) => number,
serialize: (message: O) => ArrayBuffer,
deserialize: (dataView: DataView) => I,
deserialize: (bytes: DataView | Uint8Array) => I,
actual: O,
expected: unknown,
): void {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function assertMatch<O, I>(
export function assertRoundTrip<O, I, V extends O>(
size: (message: O) => number,
serialize: (message: O) => ArrayBuffer,
deserialize: (dataView: DataView) => I,
deserialize: (bytes: DataView | Uint8Array) => I,
message: V,
): void {
assertMatch(size, serialize, deserialize, message, message);
Expand Down
50 changes: 48 additions & 2 deletions src/generate_typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,13 +1486,59 @@ fn write_deserialize_function<T: Write>(
name: &Identifier,
) -> Result<(), fmt::Error> {
write_indentation(buffer, indentation)?;
write!(buffer, "export function deserialize(dataView: DataView): ")?;
writeln!(buffer, "export function deserialize(")?;
write_indentation(buffer, indentation + 1)?;
writeln!(buffer, "bytes:")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| ArrayBuffer")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| DataView")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Int8Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Uint8Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Uint8ClampedArray")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Int16Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Uint16Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Int32Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Uint32Array")?;
// `Float16Array` was omitted here since it apparently TypeScript doesn't know about it.
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Float32Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| Float64Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| BigInt64Array")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "| BigUint64Array,")?;
write_indentation(buffer, indentation)?;
write!(buffer, "): ")?;
write_identifier(buffer, name, Pascal, Some(In))?;
writeln!(buffer, " | Error {{")?;
write_indentation(buffer, indentation + 1)?;
writeln!(buffer, "try {{")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "return deserializeUnsafe(dataView);")?;
writeln!(buffer, "if (bytes instanceof ArrayBuffer) {{")?;
write_indentation(buffer, indentation + 3)?;
writeln!(buffer, "return deserializeUnsafe(new DataView(bytes));")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "}}")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "if (bytes instanceof DataView) {{")?;
write_indentation(buffer, indentation + 3)?;
writeln!(buffer, "return deserializeUnsafe(bytes);")?;
write_indentation(buffer, indentation + 2)?;
writeln!(buffer, "}}")?;
write_indentation(buffer, indentation + 2)?;
writeln!(
buffer,
"return deserializeUnsafe(new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength));",
)?;
write_indentation(buffer, indentation + 1)?;
writeln!(buffer, "}} catch (e) {{")?;
write_indentation(buffer, indentation + 2)?;
Expand Down
Loading

0 comments on commit 25ccf6e

Please sign in to comment.