aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Pontes <github@thiago.me>2015-09-02 20:16:17 -0300
committerThiago Pontes <github@thiago.me>2015-09-03 14:42:53 -0300
commita2a9c0489d692b0970a887dccb61cbc1e7fd232a (patch)
tree545922f75b726c407942f3646867a4b605c0cbf5
parent3f9b6f8586b60929ccbfe1cf51b84887ef711b77 (diff)
downloadservo-a2a9c0489d692b0970a887dccb61cbc1e7fd232a.tar.gz
servo-a2a9c0489d692b0970a887dccb61cbc1e7fd232a.zip
Make use of FromStr and Default traits in lib canvas
fixup! Make use of FromStr and Default traits in lib canvas
-rw-r--r--components/canvas_traits/lib.rs182
-rw-r--r--components/script/dom/canvasrenderingcontext2d.rs9
2 files changed, 109 insertions, 82 deletions
diff --git a/components/canvas_traits/lib.rs b/components/canvas_traits/lib.rs
index 6f2924d2171..00cde267e83 100644
--- a/components/canvas_traits/lib.rs
+++ b/components/canvas_traits/lib.rs
@@ -37,6 +37,8 @@ use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
use layers::platform::surface::NativeSurface;
use offscreen_gl_context::GLContextAttributes;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
+use std::default::Default;
+use std::str::FromStr;
use std::sync::mpsc::Sender;
use util::mem::HeapSizeOf;
@@ -345,6 +347,19 @@ pub enum LineCapStyle {
Square = 2,
}
+impl FromStr for LineCapStyle {
+ type Err = ();
+
+ fn from_str(string: &str) -> Result<LineCapStyle, ()> {
+ match string {
+ "butt" => Ok(LineCapStyle::Butt),
+ "round" => Ok(LineCapStyle::Round),
+ "square" => Ok(LineCapStyle::Square),
+ _ => Err(()),
+ }
+ }
+}
+
impl LineCapStyle {
pub fn to_azure_style(&self) -> CapStyle {
match *self {
@@ -353,15 +368,6 @@ impl LineCapStyle {
LineCapStyle::Square => CapStyle::Square,
}
}
-
- pub fn from_str(string: &str) -> Option<LineCapStyle> {
- match string {
- "butt" => Some(LineCapStyle::Butt),
- "round" => Some(LineCapStyle::Round),
- "square" => Some(LineCapStyle::Square),
- _ => None
- }
- }
}
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize, HeapSizeOf)]
@@ -371,6 +377,19 @@ pub enum LineJoinStyle {
Miter = 2,
}
+impl FromStr for LineJoinStyle {
+ type Err = ();
+
+ fn from_str(string: &str) -> Result<LineJoinStyle, ()> {
+ match string {
+ "round" => Ok(LineJoinStyle::Round),
+ "bevel" => Ok(LineJoinStyle::Bevel),
+ "miter" => Ok(LineJoinStyle::Miter),
+ _ => Err(()),
+ }
+ }
+}
+
impl LineJoinStyle {
pub fn to_azure_style(&self) -> JoinStyle {
match *self {
@@ -379,15 +398,6 @@ impl LineJoinStyle {
LineJoinStyle::Miter => JoinStyle::Miter,
}
}
-
- pub fn from_str(string: &str) -> Option<LineJoinStyle> {
- match string {
- "round" => Some(LineJoinStyle::Round),
- "bevel" => Some(LineJoinStyle::Bevel),
- "miter" => Some(LineJoinStyle::Miter),
- _ => None
- }
- }
}
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize)]
@@ -398,14 +408,16 @@ pub enum RepetitionStyle {
NoRepeat,
}
-impl RepetitionStyle {
- pub fn from_str(string: &str) -> Option<RepetitionStyle> {
+impl FromStr for RepetitionStyle {
+ type Err = ();
+
+ fn from_str(string: &str) -> Result<RepetitionStyle, ()> {
match string {
- "repeat" => Some(RepetitionStyle::Repeat),
- "repeat-x" => Some(RepetitionStyle::RepeatX),
- "repeat-y" => Some(RepetitionStyle::RepeatY),
- "no-repeat" => Some(RepetitionStyle::NoRepeat),
- _ => None
+ "repeat" => Ok(RepetitionStyle::Repeat),
+ "repeat-x" => Ok(RepetitionStyle::RepeatX),
+ "repeat-y" => Ok(RepetitionStyle::RepeatY),
+ "no-repeat" => Ok(RepetitionStyle::NoRepeat),
+ _ => Err(()),
}
}
}
@@ -425,6 +437,27 @@ pub enum CompositionStyle {
Xor,
}
+impl FromStr for CompositionStyle {
+ type Err = ();
+
+ fn from_str(string: &str) -> Result<CompositionStyle, ()> {
+ match string {
+ "source-in" => Ok(CompositionStyle::SrcIn),
+ "source-out" => Ok(CompositionStyle::SrcOut),
+ "source-over" => Ok(CompositionStyle::SrcOver),
+ "source-atop" => Ok(CompositionStyle::SrcAtop),
+ "destination-in" => Ok(CompositionStyle::DestIn),
+ "destination-out" => Ok(CompositionStyle::DestOut),
+ "destination-over" => Ok(CompositionStyle::DestOver),
+ "destination-atop" => Ok(CompositionStyle::DestAtop),
+ "copy" => Ok(CompositionStyle::Copy),
+ "lighter" => Ok(CompositionStyle::Lighter),
+ "xor" => Ok(CompositionStyle::Xor),
+ _ => Err(())
+ }
+ }
+}
+
impl CompositionStyle {
pub fn to_azure_style(&self) -> CompositionOp {
match *self {
@@ -442,23 +475,6 @@ impl CompositionStyle {
}
}
- pub fn from_str(string: &str) -> Option<CompositionStyle> {
- match string {
- "source-in" => Some(CompositionStyle::SrcIn),
- "source-out" => Some(CompositionStyle::SrcOut),
- "source-over" => Some(CompositionStyle::SrcOver),
- "source-atop" => Some(CompositionStyle::SrcAtop),
- "destination-in" => Some(CompositionStyle::DestIn),
- "destination-out" => Some(CompositionStyle::DestOut),
- "destination-over" => Some(CompositionStyle::DestOver),
- "destination-atop" => Some(CompositionStyle::DestAtop),
- "copy" => Some(CompositionStyle::Copy),
- "lighter" => Some(CompositionStyle::Lighter),
- "xor" => Some(CompositionStyle::Xor),
- _ => None
- }
- }
-
pub fn to_str(&self) -> &str {
match *self {
CompositionStyle::SrcIn => "source-in",
@@ -495,6 +511,31 @@ pub enum BlendingStyle {
Luminosity,
}
+impl FromStr for BlendingStyle {
+ type Err = ();
+
+ fn from_str(string: &str) -> Result<BlendingStyle, ()> {
+ match string {
+ "multiply" => Ok(BlendingStyle::Multiply),
+ "screen" => Ok(BlendingStyle::Screen),
+ "overlay" => Ok(BlendingStyle::Overlay),
+ "darken" => Ok(BlendingStyle::Darken),
+ "lighten" => Ok(BlendingStyle::Lighten),
+ "color-dodge" => Ok(BlendingStyle::ColorDodge),
+ "color-burn" => Ok(BlendingStyle::ColorBurn),
+ "hard-light" => Ok(BlendingStyle::HardLight),
+ "soft-light" => Ok(BlendingStyle::SoftLight),
+ "difference" => Ok(BlendingStyle::Difference),
+ "exclusion" => Ok(BlendingStyle::Exclusion),
+ "hue" => Ok(BlendingStyle::Hue),
+ "saturation" => Ok(BlendingStyle::Saturation),
+ "color" => Ok(BlendingStyle::Color),
+ "luminosity" => Ok(BlendingStyle::Luminosity),
+ _ => Err(())
+ }
+ }
+}
+
impl BlendingStyle {
pub fn to_azure_style(&self) -> CompositionOp {
match *self {
@@ -516,27 +557,6 @@ impl BlendingStyle {
}
}
- pub fn from_str(string: &str) -> Option<BlendingStyle> {
- match string {
- "multiply" => Some(BlendingStyle::Multiply),
- "screen" => Some(BlendingStyle::Screen),
- "overlay" => Some(BlendingStyle::Overlay),
- "darken" => Some(BlendingStyle::Darken),
- "lighten" => Some(BlendingStyle::Lighten),
- "color-dodge" => Some(BlendingStyle::ColorDodge),
- "color-burn" => Some(BlendingStyle::ColorBurn),
- "hard-light" => Some(BlendingStyle::HardLight),
- "soft-light" => Some(BlendingStyle::SoftLight),
- "difference" => Some(BlendingStyle::Difference),
- "exclusion" => Some(BlendingStyle::Exclusion),
- "hue" => Some(BlendingStyle::Hue),
- "saturation" => Some(BlendingStyle::Saturation),
- "color" => Some(BlendingStyle::Color),
- "luminosity" => Some(BlendingStyle::Luminosity),
- _ => None
- }
- }
-
pub fn to_str(&self) -> &str {
match *self {
BlendingStyle::Multiply => "multiply",
@@ -564,28 +584,34 @@ pub enum CompositionOrBlending {
Blending(BlendingStyle),
}
-impl CompositionOrBlending {
- pub fn to_azure_style(&self) -> CompositionOp {
- match *self {
- CompositionOrBlending::Composition(op) => op.to_azure_style(),
- CompositionOrBlending::Blending(op) => op.to_azure_style(),
- }
- }
-
- pub fn default() -> CompositionOrBlending {
+impl Default for CompositionOrBlending {
+ fn default() -> CompositionOrBlending {
CompositionOrBlending::Composition(CompositionStyle::SrcOver)
}
+}
+
+impl FromStr for CompositionOrBlending {
+ type Err = ();
- pub fn from_str(string: &str) -> Option<CompositionOrBlending> {
- if let Some(op) = CompositionStyle::from_str(string) {
- return Some(CompositionOrBlending::Composition(op));
+ fn from_str(string: &str) -> Result<CompositionOrBlending, ()> {
+ if let Ok(op) = CompositionStyle::from_str(string) {
+ return Ok(CompositionOrBlending::Composition(op));
}
- if let Some(op) = BlendingStyle::from_str(string) {
- return Some(CompositionOrBlending::Blending(op));
+ if let Ok(op) = BlendingStyle::from_str(string) {
+ return Ok(CompositionOrBlending::Blending(op));
}
- None
+ Err(())
+ }
+}
+
+impl CompositionOrBlending {
+ pub fn to_azure_style(&self) -> CompositionOp {
+ match *self {
+ CompositionOrBlending::Composition(op) => op.to_azure_style(),
+ CompositionOrBlending::Blending(op) => op.to_azure_style(),
+ }
}
}
diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs
index 893ad97b526..57feaf5e9d7 100644
--- a/components/script/dom/canvasrenderingcontext2d.rs
+++ b/components/script/dom/canvasrenderingcontext2d.rs
@@ -45,6 +45,7 @@ use std::borrow::ToOwned;
use std::cell::RefCell;
use std::cmp;
use std::fmt;
+use std::str::FromStr;
use std::sync::mpsc::channel;
use url::Url;
@@ -568,7 +569,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
- if let Some(op) = CompositionOrBlending::from_str(&op_str) {
+ if let Ok(op) = CompositionOrBlending::from_str(&op_str) {
self.state.borrow_mut().global_composition = op;
self.ipc_renderer
.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetGlobalComposition(op)))
@@ -1006,7 +1007,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
},
};
- if let Some(rep) = RepetitionStyle::from_str(&repetition) {
+ if let Ok(rep) = RepetitionStyle::from_str(&repetition) {
return Ok(CanvasPattern::new(self.global.root().r(),
image_data,
Size2D::new(image_size.width as i32, image_size.height as i32),
@@ -1045,7 +1046,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn SetLineCap(&self, cap_str: DOMString) {
- if let Some(cap) = LineCapStyle::from_str(&cap_str) {
+ if let Ok(cap) = LineCapStyle::from_str(&cap_str) {
self.state.borrow_mut().line_cap = cap;
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineCap(cap))).unwrap()
}
@@ -1063,7 +1064,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn SetLineJoin(&self, join_str: DOMString) {
- if let Some(join) = LineJoinStyle::from_str(&join_str) {
+ if let Ok(join) = LineJoinStyle::from_str(&join_str) {
self.state.borrow_mut().line_join = join;
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineJoin(join))).unwrap()
}