Auto merge of #12419 - GuillaumeGomez:try_from, r=Ms2ger

Replace AdjacentPosition::parse by TryFrom

Fixes #12387.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12419)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-13 00:57:35 -07:00 committed by GitHub
commit 902e6322e0
2 changed files with 9 additions and 5 deletions

View file

@ -78,6 +78,7 @@ use selectors::parser::{AttrSelector, NamespaceConstraint, parse_author_origin_s
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::{Cell, Ref}; use std::cell::{Cell, Ref};
use std::convert::TryFrom;
use std::default::Default; use std::default::Default;
use std::mem; use std::mem;
use std::sync::Arc; use std::sync::Arc;
@ -126,8 +127,10 @@ pub enum AdjacentPosition {
BeforeEnd, BeforeEnd,
} }
impl AdjacentPosition { impl<'a> TryFrom<&'a str> for AdjacentPosition {
pub fn parse(position: &str) -> Fallible<AdjacentPosition> { type Err = Error;
fn try_from(position: &'a str) -> Result<AdjacentPosition, Self::Err> {
match_ignore_ascii_case! { &*position, match_ignore_ascii_case! { &*position,
"beforebegin" => Ok(AdjacentPosition::BeforeBegin), "beforebegin" => Ok(AdjacentPosition::BeforeBegin),
"afterbegin" => Ok(AdjacentPosition::AfterBegin), "afterbegin" => Ok(AdjacentPosition::AfterBegin),
@ -2028,7 +2031,7 @@ impl ElementMethods for Element {
// https://dom.spec.whatwg.org/#dom-element-insertadjacentelement // https://dom.spec.whatwg.org/#dom-element-insertadjacentelement
fn InsertAdjacentElement(&self, where_: DOMString, element: &Element) fn InsertAdjacentElement(&self, where_: DOMString, element: &Element)
-> Fallible<Option<Root<Element>>> { -> Fallible<Option<Root<Element>>> {
let where_ = try!(AdjacentPosition::parse(&*where_)); let where_ = try!(AdjacentPosition::try_from(&*where_));
let inserted_node = try!(self.insert_adjacent(where_, element.upcast())); let inserted_node = try!(self.insert_adjacent(where_, element.upcast()));
Ok(inserted_node.map(|node| Root::downcast(node).unwrap())) Ok(inserted_node.map(|node| Root::downcast(node).unwrap()))
} }
@ -2040,7 +2043,7 @@ impl ElementMethods for Element {
let text = Text::new(data, &document_from_node(self)); let text = Text::new(data, &document_from_node(self));
// Step 2. // Step 2.
let where_ = try!(AdjacentPosition::parse(&*where_)); let where_ = try!(AdjacentPosition::try_from(&*where_));
self.insert_adjacent(where_, text.upcast()).map(|_| ()) self.insert_adjacent(where_, text.upcast()).map(|_| ())
} }
@ -2048,7 +2051,7 @@ impl ElementMethods for Element {
fn InsertAdjacentHTML(&self, position: DOMString, text: DOMString) fn InsertAdjacentHTML(&self, position: DOMString, text: DOMString)
-> ErrorResult { -> ErrorResult {
// Step 1. // Step 1.
let position = try!(AdjacentPosition::parse(&*position)); let position = try!(AdjacentPosition::try_from(&*position));
let context = match position { let context = match position {
AdjacentPosition::BeforeBegin | AdjacentPosition::AfterEnd => { AdjacentPosition::BeforeBegin | AdjacentPosition::AfterEnd => {

View file

@ -19,6 +19,7 @@
#![feature(slice_patterns)] #![feature(slice_patterns)]
#![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
#![feature(question_mark)] #![feature(question_mark)]
#![feature(try_from)]
#![deny(unsafe_code)] #![deny(unsafe_code)]
#![allow(non_snake_case)] #![allow(non_snake_case)]