Repository: typst/typst
File: crates/typst-library/src/visualize/image/svg.rs (Rust)

Code before:
                    .extension()
                    .and_then(OsStr::to_str)
                    .unwrap_or_default()
                    .to_lowercase();
                match ext.as_str() {
                    "jpg" | "jpeg" => Some(usvg::ImageKind::JPEG(arc_data)),
                    "png" => Some(usvg::ImageKind::PNG(arc_data)),
                    "gif" => Some(usvg::ImageKind::GIF(arc_data)),
                    "webp" => Some(usvg::ImageKind::WEBP(arc_data)),
                    // The function load_sub_svg is not public in the usvg crate.
                    // "svg" | "svgz" => usvg::load_sub_svg(arc_data, opts),
                    _ => {
                        if self.error.is_empty() {
                            self.error = eco_format!(
                                "Linked image extension not supported: {}",
                                href
                            );
                        };
                        None
                    }
                }
            }
            Err(e) => {
                if self.error.is_empty() {
                    self.error =
                        eco_format!("Cannot use linked image: {}; Cause: {:?}", href, e);
                };
                None
            }
        }
    }
}


Review comment (bug):
the error messages should follow the standard style (lowercase and how the cause is integrated). if you search across the codebase for "failed to", you'll find plenty of examples.

Code after:
        }
        match self.load_or_error(href) {
            Ok(image) => Some(image),
            Err(err) => {
                self.error = Some(LoadError::new(
                    ReportPos::None,
                    eco_format!("failed to load linked image {} in SVG", href),
                    err,
                ));
                None
            }
        }
    }

    /// Load a linked image or return an error message string.
    fn load_or_error(&mut self, href: &str) -> Result<usvg::ImageKind, EcoString> {
        // If the href starts with "file://", strip this prefix to construct an ordinary path.
        let href = href.strip_prefix("file://").unwrap_or(href);

        // Do not accept absolute hrefs. They would be parsed in typst in a way
        // that is not compatible with their interpretation in the SVG standard.
        if href.starts_with("/") {
            return Err("absolute paths are not allowed".into());
        }

        // Exit early if the href is an URL.
        if let Some(pos) = href.find("://") {
            let scheme = &href[..pos];
            if scheme
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
            {
                return Err("URLs are not allowed".into());
            }
        }

        // Resolve the path to the linked image.
        if self.svg_file.is_none() {
            return Err("cannot access file system from here".into());
        }
        // Replace the file name in svg_file by href.
        let href_file = self.svg_file.unwrap().join(href);

        // Load image if file can be accessed.
        match self.world.file(href_file) {
            Ok(bytes) => {
                let arc_data = Arc::new(bytes.to_vec());
                let format = match determine_format_from_path(href) {
                    Some(format) => Some(format),
                    None => ImageFormat::detect(&arc_data),
                };