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

Code before:
            let scheme = &href[..pos];
            if scheme
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
            {
                return Err(EcoString::from("URLs are not allowed"));
            }
        }

        // Resolve the path to the linked image.
        if self.svg_parent.is_none() {
            return Err(EcoString::from("cannot access file system from here"));
        }
        // Replace the final underscore in svg_parent by the href.
        let href_file = self.svg_parent.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),
                };
                match format {
                    None => Err(EcoString::from("could not determine image format")),
                    Some(ImageFormat::Vector(vector_format)) => match vector_format {
                        VectorFormat::Svg => {
                            Err(EcoString::from("SVG images are not supported yet"))
                        }
                        VectorFormat::Pdf => {
                            Err(EcoString::from("PDF documents are not supported"))
                        }
                    },
                    Some(ImageFormat::Raster(raster_format)) => match raster_format {
                        RasterFormat::Exchange(exchange_format) => {
                            match exchange_format {
                                ExchangeFormat::Gif => Ok(usvg::ImageKind::GIF(arc_data)),
                                ExchangeFormat::Jpg => {
                                    Ok(usvg::ImageKind::JPEG(arc_data))
                                }
                                ExchangeFormat::Png => Ok(usvg::ImageKind::PNG(arc_data)),
                                ExchangeFormat::Webp => {
                                    Ok(usvg::ImageKind::WEBP(arc_data))
                                }
                            }
                        }
                        RasterFormat::Pixel(_) => {
                            Err(EcoString::from("pixel formats are not supported"))
                        }
                    },

Review comment (suggestion):
These `EcoString::from` can just be `.into()` calls. It would also be nice to have the happy path at the top of the match.

Code after:

        // 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),
                };
                match format {
                    Some(ImageFormat::Vector(vector_format)) => match vector_format {
                        VectorFormat::Svg => {
                            Err("SVG images are not supported yet".into())
                        }
                        VectorFormat::Pdf => {
                            Err("PDF documents are not supported".into())
                        }
                    },
                    Some(ImageFormat::Raster(raster_format)) => match raster_format {
                        RasterFormat::Exchange(exchange_format) => {
                            match exchange_format {
                                ExchangeFormat::Gif => Ok(usvg::ImageKind::GIF(arc_data)),
                                ExchangeFormat::Jpg => {
                                    Ok(usvg::ImageKind::JPEG(arc_data))
                                }
                                ExchangeFormat::Png => Ok(usvg::ImageKind::PNG(arc_data)),
                                ExchangeFormat::Webp => {
                                    Ok(usvg::ImageKind::WEBP(arc_data))
                                }
                            }
                        }
                        RasterFormat::Pixel(_) => {
                            Err("pixel formats are not supported".into())
                        }
                    },
                    None => Err("unknown image format".into()),
                }
            }
            // TODO: Somehow unify this with `impl Display for FileError`.
            Err(err) => Err(match err {
                FileError::NotFound(path) => {
                    eco_format!("file not found, searched at {}", path.display())
                }
                FileError::AccessDenied => "access denied".into(),