Comments (5)
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.
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.
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.
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.
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)
- Auto add ToSchema bound to generic type
- 如何集成casbin访问控制到中间件里去啊? HOT 1
- UNSPECIFIED Ipv4 and Ipv6 use same port crash in centos HOT 1
- #[handler] and #[endpoint] supports generic function
- Update quinn to 0.11 and remove old rustls
- 1.0 release planning
- Improve documentation and official website HOT 1
- use of undeclared crate or module `serde_json` HOT 4
- cors not working
- Salvo does not take into account the schema name for generics HOT 11
- JWT模块中对数据结构有必须定义`expr`字段要,文档没有说明且易引起用户误用 HOT 1
- example-otlp don't work normally
- Why write status code twice? HOT 7
- There is a probabilistic hang bug in the "graceful shutdown" phase HOT 2
- opanapi 接口组和接口的排序问题 HOT 1
- stack overflow when using recursive struct with openapi HOT 2
- features添加quinn后,无法在windows上交叉编译为linux二进制 HOT 4
- 2 features request. HOT 6
- `required` attribute for salvo_oapi endpoint macro HOT 2
- `hoop`的调用顺序是不是决定了中间件的执行顺序 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from salvo.