on master help sign in
src/assets.rs: Extract helper method OpenedInput::path()
commitdc8225f6828e30a7acad09b0a9f9b3f1c30fc533 authorMartin Nordholts <enselic@gmail.com> dateSun 26 Sep 2021, 10:00 (5 years ago) parent9d9b266
2 files changed+17−13
src/assets.rs+2−13 src/input.rs+15
src/assets.rs+2−13
@@ -11,7 +11,7 @@ use path_abs::PathAbs;
1111
1212use crate::bat_warning;
1313use crate::error::*;
14−use crate::input::{InputReader, OpenedInput, OpenedInputKind};
14+use crate::input::{InputReader, OpenedInput};
1515use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
1616
1717use ignored_suffixes::*;
@@ -198,18 +198,7 @@ impl HighlightingAssets {
198198 .ok_or_else(|| Error::UnknownSyntax(language.to_owned()));
199199 }
200200
201− // Get the path of the file:
202− // If this was set by the metadata, that will take priority.
203− // If it wasn't, it will use the real file path (if available).
204− let path = input
205− .metadata
206− .user_provided_name
207− .as_ref()
208− .or_else(|| match input.kind {
209− OpenedInputKind::OrdinaryFile(ref path) => Some(path),
210− _ => None,
211− });
212−
201+ let path = input.path();
213202 let path_syntax = if let Some(path) = path {
214203 // If a path was provided, we try and detect the syntax based on extension mappings.
215204 match mapping.get_syntax_for(
src/input.rs+15
@@ -108,6 +108,21 @@ pub(crate) struct OpenedInput<'a> {
108108 pub(crate) description: InputDescription,
109109}
110110
111+impl OpenedInput<'_> {
112+ /// Get the path of the file:
113+ /// If this was set by the metadata, that will take priority.
114+ /// If it wasn't, it will use the real file path (if available).
115+ pub(crate) fn path(&self) -> Option<&PathBuf> {
116+ self.metadata
117+ .user_provided_name
118+ .as_ref()
119+ .or_else(|| match self.kind {
120+ OpenedInputKind::OrdinaryFile(ref path) => Some(path),
121+ _ => None,
122+ })
123+ }
124+}
125+
111126impl<'a> Input<'a> {
112127 pub fn ordinary_file(path: impl AsRef<Path>) -> Self {
113128 Self::_ordinary_file(path.as_ref())