#![doc(html_root_url = "https://docs.rs/is-svg/0.1.0/")]
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, missing_docs)]
#![warn(rust_2018_idioms)]
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]
use usvg::{Options, Tree};
const GZIP_MAGIC_NUMBER: [u8; 2] = [0x1f, 0x8b];
pub fn is_svg(data: impl AsRef<[u8]>) -> bool {
let inner = |data: &[u8]| -> bool {
let opt = Options::default();
Tree::from_data(data, &opt).is_ok()
};
inner(data.as_ref())
}
pub fn is_svg_string(data: impl AsRef<[u8]>) -> bool {
let inner = |data: &[u8]| -> bool { is_svg(data) && !data.starts_with(&GZIP_MAGIC_NUMBER) };
inner(data.as_ref())
}
pub fn is_svgz(data: impl AsRef<[u8]>) -> bool {
let inner = |data: &[u8]| -> bool { is_svg(data) && data.starts_with(&GZIP_MAGIC_NUMBER) };
inner(data.as_ref())
}
#[cfg(test)]
mod tests {
#[test]
fn is_svg() {
assert!(super::is_svg(include_str!("../tests/data/Ampel.svg")));
assert!(super::is_svg(include_str!("../tests/data/image-033.svg")));
assert!(super::is_svg(include_str!("../tests/data/image.svg")));
assert!(super::is_svg(include_str!("../tests/data/simple-text.svg")));
assert!(super::is_svg(include_str!(
"../tests/data/SVG_animation_using_CSS.svg"
)));
assert!(super::is_svg(include_str!(
"../tests/data/SVG_animation_using_SMIL.svg"
)));
assert!(super::is_svg(include_bytes!("../tests/data/image.svgz")));
assert!(!super::is_svg(include_str!("../tests/data/green.css")));
assert!(!super::is_svg(include_bytes!(
"../tests/data/image-63x61.png"
)));
assert!(!super::is_svg(include_bytes!("../tests/data/image.gif")));
assert!(!super::is_svg(include_bytes!("../tests/data/image.jpg")));
assert!(!super::is_svg(include_bytes!("../tests/data/image.png")));
}
#[test]
fn is_svg_string() {
assert!(super::is_svg_string(include_str!(
"../tests/data/Ampel.svg"
)));
assert!(super::is_svg_string(include_str!(
"../tests/data/image-033.svg"
)));
assert!(super::is_svg_string(include_str!(
"../tests/data/image.svg"
)));
assert!(super::is_svg_string(include_str!(
"../tests/data/simple-text.svg"
)));
assert!(super::is_svg_string(include_str!(
"../tests/data/SVG_animation_using_CSS.svg"
)));
assert!(super::is_svg_string(include_str!(
"../tests/data/SVG_animation_using_SMIL.svg"
)));
assert!(!super::is_svg_string(include_bytes!(
"../tests/data/image.svgz"
)));
assert!(!super::is_svg_string(include_str!(
"../tests/data/green.css"
)));
assert!(!super::is_svg_string(include_bytes!(
"../tests/data/image-63x61.png"
)));
assert!(!super::is_svg_string(include_bytes!(
"../tests/data/image.gif"
)));
assert!(!super::is_svg_string(include_bytes!(
"../tests/data/image.jpg"
)));
assert!(!super::is_svg_string(include_bytes!(
"../tests/data/image.png"
)));
}
#[test]
fn is_svgz() {
assert!(!super::is_svgz(include_str!("../tests/data/image-033.svg")));
assert!(!super::is_svgz(include_str!("../tests/data/Ampel.svg")));
assert!(!super::is_svgz(include_str!("../tests/data/image.svg")));
assert!(!super::is_svgz(include_str!(
"../tests/data/simple-text.svg"
)));
assert!(!super::is_svgz(include_str!(
"../tests/data/SVG_animation_using_CSS.svg"
)));
assert!(!super::is_svgz(include_str!(
"../tests/data/SVG_animation_using_SMIL.svg"
)));
assert!(super::is_svgz(include_bytes!("../tests/data/image.svgz")));
assert!(!super::is_svgz(include_str!("../tests/data/green.css")));
assert!(!super::is_svgz(include_bytes!(
"../tests/data/image-63x61.png"
)));
assert!(!super::is_svgz(include_bytes!("../tests/data/image.gif")));
assert!(!super::is_svgz(include_bytes!("../tests/data/image.jpg")));
assert!(!super::is_svgz(include_bytes!("../tests/data/image.png")));
}
}