-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate how to construct and eliminate choices
- Loading branch information
1 parent
4ad1f33
commit b9cbee6
Showing
5 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,37 +6,54 @@ use { | |
io::{self, BufReader, BufWriter}, | ||
}, | ||
types::{ | ||
types::{SendEmailRequestIn, SendEmailRequestOut}, | ||
types::{ | ||
SendEmailRequestIn, SendEmailRequestOut, SendEmailResponseIn, SendEmailResponseOut, | ||
}, | ||
Deserialize, Serialize, | ||
}, | ||
}; | ||
|
||
const FILE_PATH: &str = "/tmp/message"; | ||
const REQUEST_FILE_PATH: &str = "/tmp/request"; | ||
const RESPONSE_FILE_PATH: &str = "/tmp/response"; | ||
|
||
fn write_to_file() -> io::Result<()> { | ||
let message = SendEmailRequestOut { | ||
let request_message = SendEmailRequestOut { | ||
to: "[email protected]".to_owned(), | ||
subject: "I love Typical!".to_owned(), | ||
body: "It makes serialization easy and safe.".to_owned(), | ||
}; | ||
|
||
let file = BufWriter::new(File::create(FILE_PATH)?); | ||
message.serialize(file) | ||
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 response_file = BufWriter::new(File::create(RESPONSE_FILE_PATH)?); | ||
response_message.serialize(response_file) | ||
} | ||
|
||
fn read_from_file() -> io::Result<()> { | ||
let file = BufReader::new(File::open(FILE_PATH)?); | ||
let message = SendEmailRequestIn::deserialize(file)?; | ||
let request_file = BufReader::new(File::open(REQUEST_FILE_PATH)?); | ||
let request_message = SendEmailRequestIn::deserialize(request_file)?; | ||
|
||
let response_file = BufReader::new(File::open(RESPONSE_FILE_PATH)?); | ||
let response_message = SendEmailResponseIn::deserialize(response_file)?; | ||
|
||
println!("to: {}", request_message.to); | ||
println!("subject: {}", request_message.subject); | ||
println!("body: {}", request_message.body); | ||
|
||
println!("to: {}", message.to); | ||
println!("subject: {}", message.subject); | ||
println!("body: {}", message.body); | ||
match response_message { | ||
SendEmailResponseIn::Success => println!("The email was sent!"), | ||
SendEmailResponseIn::Error(message) => println!("An error occurred: {message}"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
write_to_file()?; | ||
read_from_file()?; | ||
remove_file(FILE_PATH) | ||
remove_file(REQUEST_FILE_PATH)?; | ||
remove_file(RESPONSE_FILE_PATH) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,78 @@ | ||
import { readFileSync, writeFileSync, unlinkSync } from 'fs'; | ||
import { Types } from '../generated/types'; | ||
import { Types, unreachable } from '../generated/types'; | ||
|
||
const filePath = '/tmp/message'; | ||
const { SendEmailRequest } = Types; | ||
const requestFilePath = '/tmp/request'; | ||
const responseFilePath = '/tmp/response'; | ||
const { SendEmailRequest, SendEmailResponse } = Types; | ||
|
||
function writeToFile(): void { | ||
const message = { | ||
const requestMessage = { | ||
to: '[email protected]', | ||
subject: 'I love Typical!', | ||
body: 'It makes serialization easy and safe.', | ||
}; | ||
|
||
const arrayBuffer = SendEmailRequest.serialize(message); | ||
writeFileSync(filePath, Buffer.from(arrayBuffer)); | ||
const responseMessage = { | ||
error: 'Example error', | ||
}; | ||
|
||
const requestArrayBuffer = SendEmailRequest.serialize(requestMessage); | ||
writeFileSync(requestFilePath, Buffer.from(requestArrayBuffer)); | ||
|
||
const responseArrayBuffer = SendEmailResponse.serialize(responseMessage); | ||
writeFileSync(responseFilePath, Buffer.from(responseArrayBuffer)); | ||
} | ||
|
||
function readFromFile(): void { | ||
const fileContents = readFileSync(filePath); | ||
const message = SendEmailRequest.deserialize( | ||
const requestFileContents = readFileSync(requestFilePath); | ||
const requestMessage = SendEmailRequest.deserialize( | ||
new DataView( | ||
requestFileContents.buffer, | ||
requestFileContents.byteOffset, | ||
requestFileContents.byteLength, | ||
), | ||
); | ||
|
||
if (requestMessage instanceof Error) { | ||
throw requestMessage; | ||
} | ||
|
||
const responseFileContents = readFileSync(responseFilePath); | ||
const responseMessage = SendEmailResponse.deserialize( | ||
new DataView( | ||
fileContents.buffer, | ||
fileContents.byteOffset, | ||
fileContents.byteLength, | ||
responseFileContents.buffer, | ||
responseFileContents.byteOffset, | ||
responseFileContents.byteLength, | ||
), | ||
); | ||
|
||
if (message instanceof Error) { | ||
throw message; | ||
if (responseMessage instanceof Error) { | ||
throw responseMessage; | ||
} | ||
|
||
/* eslint-disable no-console -- Allow logging for this example. */ | ||
|
||
console.log('to:', message.to); | ||
console.log('subject:', message.subject); | ||
console.log('body:', message.body); | ||
console.log('to:', requestMessage.to); | ||
console.log('subject:', requestMessage.subject); | ||
console.log('body:', requestMessage.body); | ||
|
||
switch (responseMessage.$field) { | ||
case 'success': | ||
console.log('The email was sent!'); | ||
break; | ||
case 'error': | ||
console.log('An error occurred:', responseMessage.error); | ||
break; | ||
default: | ||
return unreachable(responseMessage); | ||
} | ||
|
||
/* eslint-enable no-console -- Re-enable this rule. */ | ||
|
||
return undefined; // To satisfy ESLint's `consistent-return` rule | ||
} | ||
|
||
writeToFile(); | ||
readFromFile(); | ||
unlinkSync(filePath); | ||
unlinkSync(requestFilePath); | ||
unlinkSync(responseFilePath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters