Repository: nushell/nushell
File: crates/nu-command/src/filesystem/open.rs (Rust)

Code before:
                        .map(|db| db.into_value(call.head).into_pipeline_data());

                        if res.is_ok() {
                            return res;
                        }
                    }

                    let file = match std::fs::File::open(path) {
                        Ok(file) => file,
                        Err(err) => {
                            return Err(ShellError::GenericError {
                                error: "Permission denied".into(),
                                msg: err.to_string(),
                                span: Some(arg_span),
                                help: None,
                                inner: vec![],
                            });
                        }
                    };

                    // Assigning content type should only happen in raw mode. Otherwise, the content
                    // will potentially be in one of the built-in nushell `from xxx` formats and therefore
                    // cease to be in the original content-type.... or so I'm told. :)
                    // However, nu files only open as text regardless.
                    let is_nu = path.extension().map(|ext| ext == "nu").unwrap_or(false);
                    let content_type = if raw || is_nu {
                        path.extension()
                            .map(|ext| ext.to_string_lossy().to_string())
                            .and_then(|ref s| detect_content_type(s))
                    } else {
                        None
                    };

                    let stream = PipelineData::ByteStream(
                        ByteStream::file(file, call_span, engine_state.signals().clone()),
                        Some(PipelineMetadata {
                            data_source: DataSource::FilePath(path.to_path_buf()),
                            content_type,
                        }),
                    );

                    let exts_opt: Option<Vec<String>> = if raw {
                        None
                    } else {
                        let path_str = path
                            .file_name()
                            .unwrap_or(std::ffi::OsStr::new(path))
                            .to_string_lossy()
                            .to_lowercase();
                        Some(extract_extensions(path_str.as_str()))
                    };

Review comment (suggestion):
maybe the value of `raw` could be passed to `detect_content_type` and added to the match in there so we could add mimetypes for additional filetypes in the future, instead of explicitly checking `is_nu` here? but you could also argue YAGNI, so /shrug

Code after:
                        .map(|db| db.into_value(call.head).into_pipeline_data());

                        if res.is_ok() {
                            return res;
                        }
                    }

                    let file = match std::fs::File::open(path) {
                        Ok(file) => file,
                        Err(err) => {
                            return Err(ShellError::GenericError {
                                error: "Permission denied".into(),
                                msg: err.to_string(),
                                span: Some(arg_span),
                                help: None,
                                inner: vec![],
                            });
                        }
                    };

                    // No content_type by default - Is added later if no converter is found
                    let stream = PipelineData::ByteStream(
                        ByteStream::file(file, call_span, engine_state.signals().clone()),
                        Some(PipelineMetadata {
                            data_source: DataSource::FilePath(path.to_path_buf()),
                            content_type: None,
                        }),
                    );

                    let exts_opt: Option<Vec<String>> = if raw {
                        None
                    } else {
                        let path_str = path
                            .file_name()
                            .unwrap_or(std::ffi::OsStr::new(path))
                            .to_string_lossy()
                            .to_lowercase();
                        Some(extract_extensions(path_str.as_str()))
                    };

                    let converter = exts_opt.and_then(|exts| {
                        exts.iter().find_map(|ext| {
                            engine_state
                                .find_decl(format!("from {}", ext).as_bytes(), &[])
                                .map(|id| (id, ext.to_string()))
                        })
                    });

                    match converter {
                        Some((converter_id, ext)) => {
                            let decl = engine_state.get_decl(converter_id);