Skip to content

Commit 5329b84

Browse files
pietroalbinijplatte
authored andcommitted
link: fix error handling code
1 parent 1e92d05 commit 5329b84

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/common/link.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Header for Link {
393393
NAME
394394
}
395395

396-
fn parse_header(raw: &Raw) -> ::Result<Link> {
396+
fn parse_header(raw: &Raw) -> Result<Link, ::Error> {
397397
// If more that one `Link` headers are present in a request's
398398
// headers they are combined in a single `Link` header containing
399399
// all the `link-value`s present in each of those `Link` headers.
@@ -408,10 +408,10 @@ impl Header for Link {
408408

409409
Some(Ok(p))
410410
},
411-
_ => Some(Err(::Error::Header)),
411+
_ => Some(Err(::Error::invalid())),
412412
}
413413
})
414-
.unwrap_or(Err(::Error::Header))
414+
.unwrap_or(Err(::Error::invalid()))
415415
}
416416

417417
fn fmt_header(&self, f: &mut ::Formatter) -> fmt::Result {
@@ -463,7 +463,7 @@ impl fmt::Display for LinkValue {
463463
impl FromStr for Link {
464464
type Err = ::Error;
465465

466-
fn from_str(s: &str) -> ::Result<Link> {
466+
fn from_str(s: &str) -> Result<Link, ::Error> {
467467
// Create a split iterator with delimiters: `;`, `,`
468468
let link_split = SplitAsciiUnquoted::new(s, ";,");
469469

@@ -477,7 +477,7 @@ impl FromStr for Link {
477477
if segment.trim().starts_with('<') {
478478
link_values.push(
479479
match verify_and_trim(segment.trim(), (b'<', b'>')) {
480-
Err(_) => return Err(::Error::Header),
480+
Err(_) => return Err(::Error::invalid()),
481481
Ok(s) => {
482482
LinkValue {
483483
link: s.to_owned().into(),
@@ -498,12 +498,12 @@ impl FromStr for Link {
498498
let mut link_param_split = segment.splitn(2, '=');
499499

500500
let link_param_name = match link_param_split.next() {
501-
None => return Err(::Error::Header),
501+
None => return Err(::Error::invalid()),
502502
Some(p) => p.trim(),
503503
};
504504

505505
let link_header = match link_values.last_mut() {
506-
None => return Err(::Error::Header),
506+
None => return Err(::Error::invalid()),
507507
Some(l) => l,
508508
};
509509

@@ -512,13 +512,13 @@ impl FromStr for Link {
512512
// https://tools.ietf.org/html/rfc5988#section-5.3
513513
if link_header.rel.is_none() {
514514
link_header.rel = match link_param_split.next() {
515-
None | Some("") => return Err(::Error::Header),
515+
None | Some("") => return Err(::Error::invalid()),
516516
Some(s) => {
517517
s.trim_matches(|c: char| c == '"' || c.is_whitespace())
518518
.split(' ')
519519
.map(|t| t.trim().parse())
520520
.collect::<Result<Vec<RelationType>, _>>()
521-
.or_else(|_| Err(::Error::Header))
521+
.or_else(|_| Err(::Error::invalid()))
522522
.ok()
523523
},
524524
};
@@ -527,9 +527,9 @@ impl FromStr for Link {
527527
// Parse the `Context IRI`.
528528
// https://tools.ietf.org/html/rfc5988#section-5.2
529529
link_header.anchor = match link_param_split.next() {
530-
None | Some("") => return Err(::Error::Header),
530+
None | Some("") => return Err(::Error::invalid()),
531531
Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) {
532-
Err(_) => return Err(::Error::Header),
532+
Err(_) => return Err(::Error::invalid()),
533533
Ok(a) => Some(String::from(a)),
534534
},
535535
};
@@ -538,13 +538,13 @@ impl FromStr for Link {
538538
// https://tools.ietf.org/html/rfc5988#section-5.3
539539
if link_header.rev.is_none() {
540540
link_header.rev = match link_param_split.next() {
541-
None | Some("") => return Err(::Error::Header),
541+
None | Some("") => return Err(::Error::invalid()),
542542
Some(s) => {
543543
s.trim_matches(|c: char| c == '"' || c.is_whitespace())
544544
.split(' ')
545545
.map(|t| t.trim().parse())
546546
.collect::<Result<Vec<RelationType>, _>>()
547-
.or_else(|_| Err(::Error::Header))
547+
.or_else(|_| Err(::Error::invalid()))
548548
.ok()
549549
},
550550
}
@@ -556,9 +556,9 @@ impl FromStr for Link {
556556

557557
v.push(
558558
match link_param_split.next() {
559-
None | Some("") => return Err(::Error::Header),
559+
None | Some("") => return Err(::Error::invalid()),
560560
Some(s) => match s.trim().parse() {
561-
Err(_) => return Err(::Error::Header),
561+
Err(_) => return Err(::Error::invalid()),
562562
Ok(t) => t,
563563
},
564564
}
@@ -570,13 +570,13 @@ impl FromStr for Link {
570570
// https://tools.ietf.org/html/rfc5988#section-5.4
571571
if link_header.media_desc.is_none() {
572572
link_header.media_desc = match link_param_split.next() {
573-
None | Some("") => return Err(::Error::Header),
573+
None | Some("") => return Err(::Error::invalid()),
574574
Some(s) => {
575575
s.trim_matches(|c: char| c == '"' || c.is_whitespace())
576576
.split(',')
577577
.map(|t| t.trim().parse())
578578
.collect::<Result<Vec<MediaDesc>, _>>()
579-
.or_else(|_| Err(::Error::Header))
579+
.or_else(|_| Err(::Error::invalid()))
580580
.ok()
581581
},
582582
};
@@ -586,9 +586,9 @@ impl FromStr for Link {
586586
// https://tools.ietf.org/html/rfc5988#section-5.4
587587
if link_header.title.is_none() {
588588
link_header.title = match link_param_split.next() {
589-
None | Some("") => return Err(::Error::Header),
589+
None | Some("") => return Err(::Error::invalid()),
590590
Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) {
591-
Err(_) => return Err(::Error::Header),
591+
Err(_) => return Err(::Error::invalid()),
592592
Ok(t) => Some(String::from(t)),
593593
},
594594
};
@@ -601,7 +601,7 @@ impl FromStr for Link {
601601
// https://tools.ietf.org/html/rfc5987#section-3.2.1
602602
if link_header.title_star.is_none() {
603603
link_header.title_star = match link_param_split.next() {
604-
None | Some("") => return Err(::Error::Header),
604+
None | Some("") => return Err(::Error::invalid()),
605605
Some(s) => Some(String::from(s.trim())),
606606
};
607607
}
@@ -610,19 +610,19 @@ impl FromStr for Link {
610610
// https://tools.ietf.org/html/rfc5988#section-5.4
611611
if link_header.media_type.is_none() {
612612
link_header.media_type = match link_param_split.next() {
613-
None | Some("") => return Err(::Error::Header),
613+
None | Some("") => return Err(::Error::invalid()),
614614
Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) {
615-
Err(_) => return Err(::Error::Header),
615+
Err(_) => return Err(::Error::invalid()),
616616
Ok(t) => match t.parse() {
617-
Err(_) => return Err(::Error::Header),
617+
Err(_) => return Err(::Error::invalid()),
618618
Ok(m) => Some(m),
619619
},
620620
},
621621

622622
};
623623
}
624624
} else {
625-
return Err(::Error::Header);
625+
return Err(::Error::invalid());
626626
}
627627
}
628628
}
@@ -651,7 +651,7 @@ impl fmt::Display for MediaDesc {
651651
impl FromStr for MediaDesc {
652652
type Err = ::Error;
653653

654-
fn from_str(s: &str) -> ::Result<MediaDesc> {
654+
fn from_str(s: &str) -> Result<MediaDesc, ::Error> {
655655
match s {
656656
"screen" => Ok(MediaDesc::Screen),
657657
"tty" => Ok(MediaDesc::Tty),
@@ -718,7 +718,7 @@ impl fmt::Display for RelationType {
718718
impl FromStr for RelationType {
719719
type Err = ::Error;
720720

721-
fn from_str(s: &str) -> ::Result<RelationType> {
721+
fn from_str(s: &str) -> Result<RelationType, ::Error> {
722722
if "alternate".eq_ignore_ascii_case(s) {
723723
Ok(RelationType::Alternate)
724724
} else if "appendix".eq_ignore_ascii_case(s) {
@@ -872,7 +872,7 @@ fn fmt_delimited<T: fmt::Display>(f: &mut fmt::Formatter, p: &[T], d: &str, b: (
872872
Ok(())
873873
}
874874

875-
fn verify_and_trim(s: &str, b: (u8, u8)) -> ::Result<&str> {
875+
fn verify_and_trim(s: &str, b: (u8, u8)) -> Result<&str, ::Error> {
876876
let length = s.len();
877877
let byte_array = s.as_bytes();
878878

@@ -883,7 +883,7 @@ fn verify_and_trim(s: &str, b: (u8, u8)) -> ::Result<&str> {
883883
|c: char| c == b.0 as char || c == b.1 as char || c.is_whitespace())
884884
)
885885
} else {
886-
Err(::Error::Header)
886+
Err(::Error::invalid())
887887
}
888888
}
889889

0 commit comments

Comments
 (0)