From a6242e9166f573dbffd48814750e6184363f2499 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Wed, 6 Nov 2024 10:50:54 +0100 Subject: [PATCH] Remove mime_guess dependency --- CHANGELOG.md | 5 +++++ Cargo.lock | 11 ---------- Cargo.toml | 1 + pulldown-cmark-mdcat/Cargo.toml | 2 +- pulldown-cmark-mdcat/src/resources/file.rs | 25 +++++++++++++++++++++- supply-chain/config.toml | 4 ---- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f7999a5..d2658ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ Use `cargo release` to create a new release. ## [Unreleased] +### Removed +- Remove a few dependencies: `mime_guess` (see [GH-297]). + +[GH-297]: https://github.com/swsnr/mdcat/pull/297 + ## [2.5.0] – 2024-09-26 ### Changed diff --git a/Cargo.lock b/Cargo.lock index b5560f33..3d1e2777 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1254,16 +1254,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "mime_guess" -version = "2.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1682,7 +1672,6 @@ dependencies = [ "insta", "mdcat-http-reqwest", "mime", - "mime_guess", "pulldown-cmark", "regex", "resvg", diff --git a/Cargo.toml b/Cargo.toml index fddee80f..3dd4db69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ pulldown-cmark-mdcat = { workspace = true, default-features = true } reqwest = { workspace = true } shell-words = { version = "1.1.0", default-features = false, features = ["std"] } syntect = { workspace = true, features = ["regex-fancy", "default-syntaxes"] } +# TODO: Drop system_proxy system_proxy = { version = "0.3.2", default-features = false } tracing = { workspace = true } tracing-subscriber = { version = "0.3.18", default-features = false, features = ["env-filter", "std", "fmt", "ansi"] } diff --git a/pulldown-cmark-mdcat/Cargo.toml b/pulldown-cmark-mdcat/Cargo.toml index a64422e1..8481bb87 100644 --- a/pulldown-cmark-mdcat/Cargo.toml +++ b/pulldown-cmark-mdcat/Cargo.toml @@ -24,10 +24,10 @@ onig = ["syntect/regex-onig"] base64 = { version = "0.22.1", default-features = false, features = ["std"] } anstyle = { version = "1.0.7", default-features = false } mime = { workspace = true } -mime_guess = { version = "2.0.5", default-features = false } pulldown-cmark = { workspace = true } syntect = { workspace = true, features = ["parsing"] } textwrap = { version = "0.16.1", default-features = false, features = ["unicode-linebreak", "unicode-width"] } +# TODO: Drop this-error? thiserror = { version = "1.0.61", default-features = false } tracing = { workspace = true } url = "2.5.2" diff --git a/pulldown-cmark-mdcat/src/resources/file.rs b/pulldown-cmark-mdcat/src/resources/file.rs index c3f0ce5b..34927c79 100644 --- a/pulldown-cmark-mdcat/src/resources/file.rs +++ b/pulldown-cmark-mdcat/src/resources/file.rs @@ -9,7 +9,9 @@ use std::fs::File; use std::io::prelude::*; use std::io::{Error, ErrorKind, Result}; +use std::path::Path; +use mime::Mime; use tracing::{event, instrument, Level}; use url::Url; @@ -30,6 +32,27 @@ impl FileResourceHandler { } } +/// Guess a mimetype, in so far as mdcat makes use of the mime type. +/// +/// This function recognizes +/// +/// - SVG images because mdcat needs to render SVG images explicitly, and +/// - PNG images because kitty can pass through PNG images in some cases. +/// +/// It checks mime types exclusively by looking at the lowercase extension. +/// +/// It ignores all other extensions and mime types and returns `None` in these cases. +fn guess_mimetype>(path: P) -> Option { + path.as_ref() + .extension() + .map(|s| s.to_ascii_lowercase()) + .and_then(|s| match s.to_str() { + Some("png") => Some(mime::IMAGE_PNG), + Some("svg") => Some(mime::IMAGE_SVG), + _ => None, + }) +} + impl ResourceUrlHandler for FileResourceHandler { #[instrument(level = "debug", skip(self))] fn read_resource(&self, url: &Url) -> Result { @@ -54,7 +77,7 @@ impl ResourceUrlHandler for FileResourceHandler { format!("Contents of {url} exceeded {} bytes", self.read_limit), )) } else { - let mime_type = mime_guess::from_path(&path).first(); + let mime_type = guess_mimetype(&path); if mime_type.is_none() { event!( Level::DEBUG, diff --git a/supply-chain/config.toml b/supply-chain/config.toml index 39d8a07d..0eecd328 100644 --- a/supply-chain/config.toml +++ b/supply-chain/config.toml @@ -292,10 +292,6 @@ criteria = "safe-to-run" version = "0.9.5" criteria = "safe-to-run" -[[exemptions.mime_guess]] -version = "2.0.5" -criteria = "safe-to-run" - [[exemptions.minimal-lexical]] version = "0.2.1" criteria = "safe-to-run"