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

Include 'mode' on native too #54

Merged
merged 4 commits into from
Feb 17, 2024
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
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ env:
# This is required to enable the web_sys clipboard API which egui_web uses
# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html
# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html
RUSTFLAGS: --cfg=web_sys_unstable_apis
RUSTFLAGS: --cfg=web_sys_unstable_apis --deny warnings
RUSTDOCFLAGS: --deny warnings

jobs:
check_native:
Expand Down Expand Up @@ -85,7 +86,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --all-features -- -D warnings -W clippy::all
args: --all-targets --all-features -- --deny warnings -W clippy::all

doc:
name: cargo doc
Expand Down
28 changes: 28 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# This scripts runs various CI-like checks in a convenient way.

set -eu
script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$script_path"
set -x

export RUSTFLAGS="--deny warnings"

# https://github.com/ericseppanen/cargo-cranky/issues/8
export RUSTDOCFLAGS="--deny warnings --deny rustdoc::missing_crate_level_docs"

typos # cargo install typos-cli

cargo fmt --all -- --check
cargo cranky --quiet --all-targets --all-features -- --deny warnings
cargo test --quiet --all-targets --all-features
cargo test --quiet --doc --all-features # checks all doc-tests

cargo cranky --quiet --lib --target wasm32-unknown-unknown --all-features

cargo doc --quiet --no-deps --all-features
cargo doc --quiet --document-private-items --no-deps --all-features

cargo deny --all-features --log-level error check

echo "All checks passed!"
22 changes: 8 additions & 14 deletions ehttp/src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@
use mime::Mime;
use rand::Rng;

use std::fs::File;
use std::io::{self, Read, Write};
use std::path::Path;

const BOUNDARY_LEN: usize = 29;

fn opt_filename(path: &Path) -> Option<&str> {
path.file_name().and_then(|filename| filename.to_str())
}

fn random_alphanumeric(len: usize) -> String {
rand::thread_rng()
.sample_iter(&rand::distributions::Uniform::from(0..=9))
Expand All @@ -45,12 +39,6 @@ fn random_alphanumeric(len: usize) -> String {
.collect()
}

fn mime_filename(path: &Path) -> (Mime, Option<&str>) {
let content_type = mime_guess::from_path(path);
let filename = opt_filename(path);
(content_type.first_or_octet_stream(), filename)
}

#[derive(Debug)]
/// The Builder for the multipart
pub struct MultipartBuilder {
Expand Down Expand Up @@ -91,10 +79,16 @@ impl MultipartBuilder {
/// * name file field name
/// * path the sending file path
#[cfg(not(target_arch = "wasm32"))]
pub fn add_file<P: AsRef<Path>>(self, name: &str, path: P) -> io::Result<Self> {
pub fn add_file<P: AsRef<std::path::Path>>(self, name: &str, path: P) -> io::Result<Self> {
fn mime_filename(path: &std::path::Path) -> (Mime, Option<&str>) {
let content_type = mime_guess::from_path(path);
let filename = path.file_name().and_then(|filename| filename.to_str());
(content_type.first_or_octet_stream(), filename)
}

let path = path.as_ref();
let (content_type, filename) = mime_filename(path);
let mut file = File::open(path)?;
let mut file = std::fs::File::open(path)?;
self.add_stream(&mut file, name, filename, Some(content_type))
}

Expand Down
12 changes: 4 additions & 8 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl<'h> IntoIterator for &'h Headers {
// ----------------------------------------------------------------------------

/// Determine if cross-origin requests lead to valid responses.
///
/// Based on <https://developer.mozilla.org/en-US/docs/Web/API/Request/mode>
#[cfg(target_arch = "wasm32")]
#[derive(Default, Clone, Copy, Debug)]
pub enum Mode {
/// If a request is made to another origin with this mode set, the result is an error.
Expand Down Expand Up @@ -141,8 +141,9 @@ pub struct Request {
/// ("Accept", "*/*"), …
pub headers: Headers,

/// Request mode used on fetch. Only available on wasm builds
#[cfg(target_arch = "wasm32")]
/// Request mode used on fetch.
///
/// Used on Web to control CORS.
pub mode: Mode,
}

Expand All @@ -155,7 +156,6 @@ impl Request {
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}
Expand All @@ -168,7 +168,6 @@ impl Request {
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}
Expand All @@ -184,7 +183,6 @@ impl Request {
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}
Expand Down Expand Up @@ -219,7 +217,6 @@ impl Request {
url: url.to_string(),
body: data,
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}
Expand All @@ -236,7 +233,6 @@ impl Request {
url: url.to_string(),
body: serde_json::to_string(body)?.into_bytes(),
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
})
}
Expand Down
2 changes: 2 additions & 0 deletions example_eframe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Example application using [`eframe`].

mod app;

pub use app::DemoApp;
Expand Down
2 changes: 1 addition & 1 deletion example_eframe/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl WebHandle {
.start(
canvas_id,
eframe::WebOptions::default(),
Box::new(|_cc| Box::new(DemoApp::default())),
Box::new(|_cc| Box::<DemoApp>::default()),
)
.await
}
Expand Down
Loading