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

add ci action and rustfmt + clippy #6

Merged
merged 6 commits into from
Jun 27, 2022
Merged
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
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on:
push:
pull_request:

env:
RUSTFLAGS: -Dwarnings

jobs:
test:
name: Rust ${{matrix.rust}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
rust: [beta, stable]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
components: rust-src rustfmt
- run: |
cargo test --all
cargo fmt --check
env:
RUSTFLAGS: ${{env.RUSTFLAGS}}

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy, rust-src
- run: cargo clippy --tests
43 changes: 18 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod errors;

use std::io;
use std::io::{Read, Write};
use std::panic;
pub use crate::errors::Error;
use serde::Serialize;
use serde_json::{json, Value};
pub use crate::errors::Error;
use std::fmt::Display;
use std::io;
use std::io::{Read, Write};
use std::panic;

/// Writes the given JSON data to stdout, thereby 'sending' a message
/// back to Chrome. *If you are on stable, then you also need to import macros
Expand Down Expand Up @@ -54,12 +54,10 @@ pub fn read_input<R: Read>(mut input: R) -> Result<Value, Error> {
let value = serde_json::from_slice(&buffer)?;
Ok(value)
}
Err(e) => {
match e.kind() {
io::ErrorKind::UnexpectedEof => Err(Error::NoMoreInput),
_ => Err(e.into()),
}
}
Err(e) => match e.kind() {
io::ErrorKind::UnexpectedEof => Err(Error::NoMoreInput),
_ => Err(e.into()),
},
}
}

Expand Down Expand Up @@ -107,7 +105,7 @@ fn handle_panic(info: &std::panic::PanicInfo) {
None => match info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<Any>",
}
},
};
// Ignore error if send fails, we don't want to panic inside the panic handler
let _ = send!({
Expand Down Expand Up @@ -144,31 +142,26 @@ fn handle_panic(info: &std::panic::PanicInfo) {
///
/// ```
pub fn event_loop<T, E, F>(callback: F)
where F: Fn(serde_json::Value) -> Result<T, E>,
T: Serialize,
E: Display
where
F: Fn(serde_json::Value) -> Result<T, E>,
T: Serialize,
E: Display,
{
panic::set_hook(Box::new(handle_panic));

loop {
// wait for input
match read_input(io::stdin()) {
Ok(v) => {
match callback(v) {
Ok(response) => send_message(io::stdout(), &response).unwrap(),
Err(e) => send!({
"error": format!("{}", e)
}).unwrap()
}
}
Ok(v) => match callback(v) {
Ok(response) => send_message(io::stdout(), &response).unwrap(),
Err(e) => send!({ "error": format!("{}", e) }).unwrap(),
},
Err(e) => {
// if the input stream has finished, then we exit the event loop
if let Error::NoMoreInput = e {
break;
}
send!({
"error": format!("{}", e)
}).unwrap();
send!({ "error": format!("{}", e) }).unwrap();
}
}
}
Expand Down
27 changes: 10 additions & 17 deletions tests/test_payload_length.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use chrome_native_messaging::{send_message, Error};
use serde::Serialize;
use serde_json::json;
use chrome_native_messaging::{send_message, Error};
use std::io::sink;

#[derive(Serialize)]
struct MoreInfo {
a: i32,
b: i32
b: i32,
}

#[test]
fn test_payload_length() {
let m = MoreInfo {
a: 0,
b: 5
};
let m = MoreInfo { a: 0, b: 5 };

// this is tiny it will work
let small_res = json!({
Expand All @@ -25,21 +22,17 @@ fn test_payload_length() {
assert!(send_message(sink(), &small_res).is_ok());

// this is almost 1024*1024 bytes long, but it should still work
let list = std::iter::repeat(" ").take(1024*1024-20).collect::<String>();
let large_res = json!({
"big_list": list
});
let list = " ".repeat(1024 * 1024 - 20);
let large_res = json!({ "big_list": list });

assert!(send_message(sink(), &large_res).is_ok());

// this is almost 1024*1024 bytes long, but it should still work
let list = std::iter::repeat(" ").take(1024*1024+20).collect::<String>();
let too_large_res = json!({
"big_list": list
});
let list = " ".repeat(1024 * 1024 + 20);
let too_large_res = json!({ "big_list": list });

match send_message(sink(), &too_large_res).err().expect("expected error") {
Error::MessageTooLarge { size: _ } => {},
_ => panic!("expected `MessageTooLarge` error")
match send_message(sink(), &too_large_res).expect_err("expected error") {
Error::MessageTooLarge { size: _ } => {}
_ => panic!("expected `MessageTooLarge` error"),
}
}