Code Monkey home page Code Monkey logo

Comments (5)

chrislearn avatar chrislearn commented on June 18, 2024 1

I hope you have carefully looked at the code. Most of the unwrap in it is only in the documentation and unit tests.
In addition, there are a few places where unwrap is needed.

from salvo.

rxdiscovery avatar rxdiscovery commented on June 18, 2024

Hello,

of course not all of them are in the main code, the existence of a single unwrap is enough to break the developer's privacy and create the potential risk of an uncontrolled crash.

here are some examples of code containing unwraps :

impl Default for Compression {
    #[inline]
    fn default() -> Self {
        #[allow(unused_mut)]
        let mut algos = IndexMap::new();
        #[cfg(feature = "zstd")]
        algos.insert(CompressionAlgo::Zstd, CompressionLevel::Default);
        #[cfg(feature = "gzip")]
        algos.insert(CompressionAlgo::Gzip, CompressionLevel::Default);
        #[cfg(feature = "deflate")]
        algos.insert(CompressionAlgo::Deflate, CompressionLevel::Default);
        #[cfg(feature = "brotli")]
        algos.insert(CompressionAlgo::Brotli, CompressionLevel::Default);
        Self {
            algos,
            content_types: vec![
                "text/*".parse().unwrap(),
                "application/javascript".parse().unwrap(),
                "application/json".parse().unwrap(),
                "application/xml".parse().unwrap(),
                "application/rss+xml".parse().unwrap(),
                "application/wasm".parse().unwrap(),
                "image/svg+xml".parse().unwrap(),
            ],
            min_length: 0,
            force_priority: false,
        }
    }
}
#[inline]
fn status_error_json(code: StatusCode, name: &str, brief: &str, cause: Option<&str>) -> String {
    #[derive(Serialize)]
    struct Data<'a> {
        error: Error<'a>,
    }
    #[derive(Serialize)]
    struct Error<'a> {
        code: u16,
        name: &'a str,
        brief: &'a str,
        cause: &'a str,
    }
    let data = Data {
        error: Error {
            code: code.as_u16(),
            name,
            brief,
            cause: cause.unwrap_or(EMPTY_CAUSE_MSG),
        },
    };
    serde_json::to_string(&data).unwrap() // <--------------------------------------------- /!\
}

I've noticed that you've sent patches correcting some unwrap with unwrap_or_default, thank you very much 👍 .

if you want, we can share the work, you give me a list of modules to clean them from unwrap, so we don't have to do the job twice.

from salvo.

chrislearn avatar chrislearn commented on June 18, 2024

Mime does not have function like Mime::from_static_str, so if you do not want use unwrap, how to change this code?

Even if all unwrap is removed from salvo, salvo cannot guarantee that unwrap is not used in all dependencies crates.
If you feel there is a problem with unwrap, should you give an issue to rust lang instead of salvo?

from salvo.

rxdiscovery avatar rxdiscovery commented on June 18, 2024

Why not use this :

mimes! {
    STAR_STAR, "*/*", 1;

    TEXT_STAR, "text/*", 4;
    TEXT_PLAIN, "text/plain", 4;
    TEXT_PLAIN_UTF_8, "text/plain; charset=utf-8", 4, None, 10;
    TEXT_HTML, "text/html", 4;
    TEXT_HTML_UTF_8, "text/html; charset=utf-8", 4, None, 9;
    TEXT_CSS, "text/css", 4;
    TEXT_CSS_UTF_8, "text/css; charset=utf-8", 4, None, 8;
    TEXT_JAVASCRIPT, "text/javascript", 4;
    TEXT_XML, "text/xml", 4;
    TEXT_EVENT_STREAM, "text/event-stream", 4;
    TEXT_CSV, "text/csv", 4;
    TEXT_CSV_UTF_8, "text/csv; charset=utf-8", 4, None, 8;
    TEXT_TAB_SEPARATED_VALUES, "text/tab-separated-values", 4;
    TEXT_TAB_SEPARATED_VALUES_UTF_8, "text/tab-separated-values; charset=utf-8", 4, None, 25;
    TEXT_VCARD, "text/vcard", 4;

    IMAGE_STAR, "image/*", 5;
    IMAGE_JPEG, "image/jpeg", 5;
    IMAGE_GIF, "image/gif", 5;
    IMAGE_PNG, "image/png", 5;
    IMAGE_BMP, "image/bmp", 5;
    IMAGE_SVG, "image/svg+xml", 5, Some(9);

    FONT_WOFF, "font/woff", 4;
    FONT_WOFF2, "font/woff2", 4;

    APPLICATION_JSON, "application/json", 11;
    APPLICATION_JAVASCRIPT, "application/javascript", 11;
    APPLICATION_JAVASCRIPT_UTF_8, "application/javascript; charset=utf-8", 11, None, 22;
    APPLICATION_WWW_FORM_URLENCODED, "application/x-www-form-urlencoded", 11;
    APPLICATION_OCTET_STREAM, "application/octet-stream", 11;
    APPLICATION_MSGPACK, "application/msgpack", 11;
    APPLICATION_PDF, "application/pdf", 11;

    MULTIPART_FORM_DATA, "multipart/form-data", 9;
}

here's a proposal; more efficient and with no risk of exception

        Self {
            algos,
            content_types: vec![
                salvo_core::http::mime::TEXT_STAR,
                salvo_core::http::mime::APPLICATION_JAVASCRIPT,
                salvo_core::http::mime::APPLICATION_JSON,
                salvo_core::http::mime::TEXT_XML,
                "application/xml".parse().unwrap_or(salvo_core::http::mime::TEXT_XML),
                "application/rss+xml"
                    .parse()
                    .unwrap_or(salvo_core::http::mime::STAR_STAR),
                "application/wasm".parse().unwrap_or(salvo_core::http::mime::STAR_STAR),
                salvo_core::http::mime::IMAGE_SVG,
            ],
            min_length: 0,
            force_priority: false,
        }

I've added TEXT_XML ( application/xml <-> text/xml )

I used STAR_STAR temporarily since their equivalent doesn't exist on the MIME crate.

from salvo.

rxdiscovery avatar rxdiscovery commented on June 18, 2024

You also have this directive to prohibit compilation if unwraps exist in the code.

#![deny(clippy::unwrap_used)]

ref: https://rust-lang.github.io/rust-clippy/master/#/unwrap_used

from salvo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.