Skip to content

Commit

Permalink
Remove mime_guess dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Nov 6, 2024
1 parent f8aea12 commit a6242e9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion pulldown-cmark-mdcat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 24 additions & 1 deletion pulldown-cmark-mdcat/src/resources/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<P: AsRef<Path>>(path: P) -> Option<Mime> {
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<MimeData> {
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a6242e9

Please sign in to comment.