bumped MSRV to 1.48.0, minor improvements

- this is the rust version that debian stable (buster) currently has, so that's nice
- now we can use `const fn`s in more places, hooray
- smartstring is no longer outdated, hooray
- bitflags is no longer outdated, hooray
- mime_type in files.rs is now a bit smarter about not reading more than it has to, hooray
- removed some redundant clippy lints, hooray
- added a teensy smidgen more documentation, hooray
This commit is contained in:
Lynne Megido 2021-10-05 02:12:16 +10:00
parent c30aba35fd
commit 47c55ae61e
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
10 changed files with 49 additions and 51 deletions

View file

@ -81,15 +81,8 @@ clippy:
cleanup-stable: cleanup-stable:
extends: .cargo-cleanup extends: .cargo-cleanup
# commented out because cargo-sweep no longer works on our MSRV # there's no point running cargo sweep on MSRV, since it's always at the same version (and therefore cargo sweep will
#cleanup-msrv: # never delete anything)
# extends: cleanup-stable
# image: "rust:1.43.0"
# cache:
# key: msrv
# paths:
# - target
# - .cargo
cleanup-nightly: cleanup-nightly:
extends: cleanup-stable extends: cleanup-stable
@ -107,8 +100,7 @@ build-base-stable:
build-base-msrv: build-base-msrv:
extends: build-base-stable extends: build-base-stable
# needs: ["cleanup-msrv"] image: "rust:1.48.0"
image: "rust:1.43.0"
cache: cache:
key: msrv key: msrv
paths: paths:
@ -134,7 +126,7 @@ build-stable:
build-msrv: build-msrv:
extends: build-stable extends: build-stable
needs: ["build-base-msrv"] needs: ["build-base-msrv"]
image: "rust:1.43.0" image: "rust:1.48.0"
cache: cache:
key: msrv key: msrv
paths: paths:
@ -159,7 +151,7 @@ test-stable:
test-msrv: test-msrv:
extends: test-stable extends: test-stable
image: "rust:1.43.0" image: "rust:1.48.0"
needs: ["build-msrv"] needs: ["build-msrv"]
cache: cache:
key: msrv key: msrv

View file

@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- For a more thorough breakdown of how these flags work, see [the corresponding wiki - For a more thorough breakdown of how these flags work, see [the corresponding wiki
page](https://gitlab.com/Lynnesbian/fif/-/wikis/Fix). page](https://gitlab.com/Lynnesbian/fif/-/wikis/Fix).
### Changed ### Changed
- The Minimum Supported Rust Version (MSRV) is now **1.48.0**.
- Capped help output (`-h`/`--help`) width at 120 characters max - Capped help output (`-h`/`--help`) width at 120 characters max
- Output is now sorted by filename - specifically, errors will appear first, followed by files that fif is unable to - Output is now sorted by filename - specifically, errors will appear first, followed by files that fif is unable to
recommend an extension for, in order of filename, followed by files that fif knows how to rename, again in order recommend an extension for, in order of filename, followed by files that fif knows how to rename, again in order

5
Cargo.lock generated
View file

@ -175,7 +175,6 @@ checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193"
name = "fif" name = "fif"
version = "0.3.7" version = "0.3.7"
dependencies = [ dependencies = [
"bitflags",
"cfg-if", "cfg-if",
"clap", "clap",
"clap_derive", "clap_derive",
@ -622,9 +621,9 @@ checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]] [[package]]
name = "smartstring" name = "smartstring"
version = "0.2.7" version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29620fe111ceaba7a50fd806b5f44c1ef44a697a739f6677a4464c7ea8685997" checksum = "31aa6a31c0c2b21327ce875f7e8952322acfcfd0c27569a6e18a647281352c9b"
dependencies = [ dependencies = [
"static_assertions", "static_assertions",
] ]

View file

@ -5,7 +5,7 @@ version = "0.3.7"
authors = ["Lynnesbian <lynne@bune.city>"] authors = ["Lynnesbian <lynne@bune.city>"]
edition = "2018" edition = "2018"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
rust-version = "1.43.0" # this can actually go as low as 1.41.0 after removing cached, but i'll leave it 1.43.0 rust-version = "1.48.0" # this can actually go as low as 1.41.0 after removing cached, but i'll leave it 1.43.0
repository = "https://gitlab.com/Lynnesbian/fif" repository = "https://gitlab.com/Lynnesbian/fif"
readme = "README.md" readme = "README.md"
keywords = ["mime", "mimetype", "utilities", "tools"] keywords = ["mime", "mimetype", "utilities", "tools"]
@ -35,7 +35,6 @@ cfg-if = "1.0.0"
itertools = "0.10.0" itertools = "0.10.0"
serde = { version = "1.0", features = ["derive"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true }
bitflags = "~1.2.1" # 1.3+ requires Rust >= 1.46
num_cpus = { version = "1.13.0", optional = true } num_cpus = { version = "1.13.0", optional = true }
maplit = "1.0.2" maplit = "1.0.2"
@ -54,11 +53,8 @@ infer = { version = "0.5.0", optional = true }
# needs to be too. otherwise, fif won't work on platforms like powerpc, even though this dependency is the only # needs to be too. otherwise, fif won't work on platforms like powerpc, even though this dependency is the only
# blocker -- fif runs just fine on powerpc without smartstring. or at least, just fine under qemu user-mode powerpc ~u0 # blocker -- fif runs just fine on powerpc without smartstring. or at least, just fine under qemu user-mode powerpc ~u0
# additionally, smartstring 0.2.8 requires rust >=1.46 (due to https://github.com/rust-lang/rust/issues/49146), and # additionally, versions before 0.2.4 didn't impl Display, so we need at least that version for displaying Strings.
# 0.2.3 doesn't impl Display on its SmartString type. smartstring = ">= 0.2.4"
# so, we need at least 0.2.4 so we can println! strings, and at most 0.2.7 until we bump the MSRV to at least 1.46.
# see https://github.com/bodil/smartstring/blob/master/CHANGELOG.md
smartstring = "<= 0.2.7, >= 0.2.4"
[dependencies.clap] [dependencies.clap]
# beta.4 requires rust >= 1.54.0 (and beta.3 was yanked) # beta.4 requires rust >= 1.54.0 (and beta.3 was yanked)
@ -90,4 +86,4 @@ opt-level = 3
opt-level = 3 opt-level = 3
[package.metadata] [package.metadata]
msrv = "1.43.0" msrv = "1.48.0"

View file

@ -7,7 +7,7 @@
[![Version](https://img.shields.io/crates/v/fif.svg?logo=rust&style=flat-square) [![Version](https://img.shields.io/crates/v/fif.svg?logo=rust&style=flat-square)
](https://crates.io/crates/fif) ](https://crates.io/crates/fif)
[![Minimum Supported Rust Version](https://img.shields.io/badge/msrv-1.43.0-orange?logo=rust&style=flat-square) [![Minimum Supported Rust Version](https://img.shields.io/badge/msrv-1.48.0-orange?logo=rust&style=flat-square)
](https://gitlab.com/Lynnesbian/fif/-/blob/master/README.md#version-policy) ](https://gitlab.com/Lynnesbian/fif/-/blob/master/README.md#version-policy)
[![License](https://img.shields.io/crates/l/fif.svg?style=flat-square) [![License](https://img.shields.io/crates/l/fif.svg?style=flat-square)
](https://gitlab.com/Lynnesbian/fif/-/blob/master/LICENSE) ](https://gitlab.com/Lynnesbian/fif/-/blob/master/LICENSE)
@ -215,4 +215,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.

View file

@ -23,6 +23,7 @@ for backend in "${_backends[@]}"; do
-W clippy::pedantic \ -W clippy::pedantic \
-W clippy::complexity \ -W clippy::complexity \
-W clippy::cargo \ -W clippy::cargo \
-W clippy::style \
-W clippy::float_cmp_const \ -W clippy::float_cmp_const \
-W clippy::lossy_float_literal \ -W clippy::lossy_float_literal \
-W clippy::multiple_inherent_impl \ -W clippy::multiple_inherent_impl \
@ -32,8 +33,6 @@ for backend in "${_backends[@]}"; do
-A clippy::shadow_unrelated \ -A clippy::shadow_unrelated \
-A clippy::option_if_let_else \ -A clippy::option_if_let_else \
-A clippy::multiple-crate-versions \ -A clippy::multiple-crate-versions \
-A clippy::cast-possible-truncation \
-A clippy::cast-possible-wrap \
-A clippy::must_use_candidate \ -A clippy::must_use_candidate \
-A clippy::missing_panics_doc \ -A clippy::missing_panics_doc \
-A clippy::missing_errors_doc \ -A clippy::missing_errors_doc \
@ -45,9 +44,7 @@ done
# redundant_closure...: the alternative is often much more verbose # redundant_closure...: the alternative is often much more verbose
# shadow_unrelated: sometimes things that seem unrelated are actually related ;) # shadow_unrelated: sometimes things that seem unrelated are actually related ;)
# option_if_let_else: the suggested code is usually harder to read than the original # option_if_let_else: the suggested code is usually harder to read than the original
# multiple_crate_versions: cached uses an old version of hashbrown :c # multiple_crate_versions: this doesn't actually trip right now, but it's not something i would want CI to fail over
# cast_possible_truncation: only ever used where it would be totally fine
# cast_possible_wrap: ditto
# must_use_candidate: useless # must_use_candidate: useless
# missing_panics_doc: the docs are just for me, fif isn't really intended to be used as a library, so this is unneeded # missing_panics_doc: the docs are just for me, fif isn't really intended to be used as a library, so this is unneeded
# missing_errors_doc: ditto # missing_errors_doc: ditto

View file

@ -1,2 +1,3 @@
# avoid-breaking-exported-api = false # only available on nightly for now # avoid-breaking-exported-api = false # only available on nightly for now
cognitive-complexity-threshold = 15 cognitive-complexity-threshold = 15
msrv = "1.48.0"

View file

@ -249,9 +249,29 @@ pub fn mime_type<T: MimeDb>(db: &T, path: &Path) -> io::Result<Option<Mime>> {
let mut file = File::open(path)?; let mut file = File::open(path)?;
// read a small amount to start with // read a small amount to start with
file.read(&mut buffer)?;
let r = db.get_type(&buffer).filter(|mime| let mut read = io::Result::Ok(0);
for _ in 0..3 {
// try to read the file up to 3 times, retrying if interrupted, bailing otherwise
file.seek(SeekFrom::Start(0))?;
read = file.read(&mut buffer);
match read {
Ok(_) => break,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(_) => break,
}
}
let read = read?;
let r = db.get_type(&buffer);
if read < INITIAL_BUF_SIZE {
// the file is smaller than INITIAL_BUF_SIZE - there's no point reading it again
return Ok(r);
}
let r = r.filter(|mime|
// some mime types should be investigated further, reading up to BUF_SIZE even if they've been determined already // some mime types should be investigated further, reading up to BUF_SIZE even if they've been determined already
// one such type is XML - there's many more specific types that can be determined by reading further (such as SVG) // one such type is XML - there's many more specific types that can be determined by reading further (such as SVG)
mime != &mime::TEXT_XML mime != &mime::TEXT_XML
@ -268,24 +288,17 @@ pub fn mime_type<T: MimeDb>(db: &T, path: &Path) -> io::Result<Option<Mime>> {
return Ok(r); return Ok(r);
} }
// attempt to read up to the BUF_SIZE bytes of the file. // attempt to read up to BUF_SIZE bytes of the file.
// we've already read the first 128 bytes into a buffer, but i can't see an obvious way to reuse them in a way that's
// faster than simply moving the seek position back to the start of the file and re-reading the whole BUF_SIZE bytes.
// for example, reading (BUF_SIZE - INITIAL_BUF_SIZE) bytes, then concatenating the original INITIAL_BUF_SIZE buffer
// with this new one would presumably be faster - but it's not. i think it's more expensive to create the iterator,
// collect the contents, etc. i'll have to look into this more. i don't at all doubt that there IS a way to do this
// efficiently, and i can think of a way in principle, but i'm not sure how to express it in a way that is both
// idiomatic/safe and fast.
let mut buffer = [0; BUF_SIZE]; let mut buffer = [0; BUF_SIZE];
file.seek(SeekFrom::Start(0))?; file.seek(SeekFrom::Start(0))?;
file.read(&mut buffer)?; file.read(&mut buffer)?;
Ok(db.get_type(&buffer)) Ok(db.get_type(&buffer))
} }
// Returns a list of known extensions for this mime type, if any. /// Returns a list of known extensions for this mime type, if any.
// This function uses the [Mime]'s "essence" rather than the [Mime] itself - mime_guess::get_mime_extensions ignores /// This function uses the [`Mime`]'s "essence" rather than the [`Mime`] itself - [`mime_guess::get_mime_extensions`]
// the type suffix, treating "image/svg+xml" as "image/svg", and thus fails to find any extensions. Passing the /// ignores the type suffix, treating "image/svg+xml" as "image/svg", and thus fails to find any extensions. Passing the
// essence_str (which includes the suffix) fixes this. /// essence_str (which includes the suffix) fixes this.
pub fn mime_extension_lookup(essence: String) -> Option<Vec<String>> { pub fn mime_extension_lookup(essence: String) -> Option<Vec<String>> {
if let Ok(cache) = MIMEXT.read() { if let Ok(cache) = MIMEXT.read() {
if let Some(exts) = cache.get(&essence) { if let Some(exts) = cache.get(&essence) {

View file

@ -40,7 +40,7 @@ fn main() {
let mut builder = env_logger::Builder::new(); let mut builder = env_logger::Builder::new();
builder builder
.filter_level(args.default_verbosity()) // set default log level .filter_level(args.get_verbosity()) // set default log level
.parse_default_env() // set log level from RUST_LOG .parse_default_env() // set log level from RUST_LOG
.parse_env("FIF_LOG") // set log level from FIF_LOG .parse_env("FIF_LOG") // set log level from FIF_LOG
.format(|buf, r| { .format(|buf, r| {

View file

@ -249,6 +249,7 @@ impl Parameters {
} }
} }
/// Populates a [`ScanOpts`] struct with values from [`self`].
pub const fn get_scan_opts(&self) -> ScanOpts { pub const fn get_scan_opts(&self) -> ScanOpts {
ScanOpts { ScanOpts {
hidden: self.scan_hidden, hidden: self.scan_hidden,
@ -258,10 +259,8 @@ impl Parameters {
} }
} }
pub fn default_verbosity(&self) -> log::LevelFilter { /// Gets the verbosity set by the user via the `-v` and `-q` flags.
#![allow(clippy::missing_const_for_fn)] pub const fn get_verbosity(&self) -> log::LevelFilter {
// match was not permitted inside const functions until 1.46
use log::LevelFilter; use log::LevelFilter;
match self.quiet { match self.quiet {