define link type of <link> element as enum and prefer match statement

since match statement checks exhaustiveness
This commit is contained in:
rhysd 2020-01-09 15:34:30 +09:00
parent 929512f4f5
commit 660511b8a0

View file

@ -82,8 +82,6 @@ pub fn walk_and_embed_assets(
match name.local.as_ref() {
"link" => {
let mut link_type: &str = "";
// Remove integrity attributes
let mut i = 0;
while i < attrs_mut.len() {
@ -95,26 +93,37 @@ pub fn walk_and_embed_assets(
}
}
enum LinkType {
Icon,
Stylesheet,
Preload,
DnsPrefetch,
Unknown,
}
let mut link_type = LinkType::Unknown;
for attr in attrs_mut.iter_mut() {
if &attr.name.local == "rel" {
if is_icon(attr.value.as_ref()) {
link_type = "icon";
link_type = LinkType::Icon;
break;
} else if attr.value.as_ref() == "stylesheet" {
link_type = "stylesheet";
link_type = LinkType::Stylesheet;
break;
}
}
}
let link_type = link_type;
if link_type == "icon" {
match link_type {
LinkType::Icon => {
for attr in attrs_mut.iter_mut() {
if &attr.name.local == "href" {
if opt_no_images {
attr.value.clear();
} else {
let href_full_url =
resolve_url(&url, attr.value.as_ref()).unwrap_or_default();
let href_full_url = resolve_url(&url, attr.value.as_ref())
.unwrap_or_default();
let (favicon_dataurl, _) = retrieve_asset(
cache,
client,
@ -129,14 +138,15 @@ pub fn walk_and_embed_assets(
}
}
}
} else if link_type == "stylesheet" {
}
LinkType::Stylesheet => {
for attr in attrs_mut.iter_mut() {
if &attr.name.local == "href" {
if opt_no_css {
attr.value.clear();
} else {
let href_full_url =
resolve_url(&url, &attr.value.as_ref()).unwrap_or_default();
let href_full_url = resolve_url(&url, &attr.value.as_ref())
.unwrap_or_default();
let replacement_text = match retrieve_asset(
cache,
client,
@ -170,7 +180,8 @@ pub fn walk_and_embed_assets(
}
}
}
} else {
}
LinkType::Unknown => {
for attr in attrs_mut.iter_mut() {
if &attr.name.local == "href" {
let href_full_url =
@ -181,6 +192,7 @@ pub fn walk_and_embed_assets(
}
}
}
}
"img" => {
// Find source tags
let mut found_src: Option<Attribute> = None;