Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

rust-css add colors & test cases #22

Merged
merged 6 commits into from
Aug 20, 2013
Merged

Conversation

recrack
Copy link
Contributor

@recrack recrack commented Jul 26, 2013

No description provided.

@recrack
Copy link
Contributor Author

recrack commented Jul 27, 2013

r? @metajack

@metajack
Copy link
Contributor

The idea is good, but I think this needs macro-izing.

something like:

define_color!(aliceblue, 240, 248, 255);

which would expand to:

static ALICEBLUE: Color = Color { red: 240, green: 248, blue: 255, alpha: 1.0 };

This also solves the problem of them being functions. I think this was due to an old compiler bug.

I'd delete all the color tests, as they are of dubious value. They would just check that the constants you created have the values you specify, but we already specify them once so it seems silly to have those numbers twice.

For the big match statement, you'll probably want to macro that too, with something like:

parse_static_color!(color, aliceblue, antiquewhite, ...);

which would expand to:

match color {
  "aliceblue" => ALICEBLUE,
  "antiquewhite" => ANTIQUEWHITE,
  ...
  _ => fail_unrecognized(color)
}

and used like:

let col = parse_static_color!(color, ...);

Or have the macro generate a function wrapping the match, etc.

@recrack
Copy link
Contributor Author

recrack commented Jul 30, 2013

#[macro_escape];
macro_rules! define_color(
    ($name:ident, $r:expr, $g:expr, $b:expr) => (
        {
            static $name: Color = Color { red: $r as u8, green: $g as u8, blue: $b as u8, alpha: 1.0 };
        }
    );
)

pub struct Color {
    red: u8,
    green: u8,
    blue: u8,
    alpha: f64,
}

fn main() {
    define_color!(black, 0, 255, 0);
}

@metajack Do have any idea? How to make indent string 'lowercase to uppercase' ?

 rustcn --pretty expanded macro.rs
#[macro_escape];
priv use std::prelude::*;
priv extern mod std;

mod __std_macros {
    #[macro_escape];
    #[doc(hidden)];
    priv use std::prelude::*;
    #[cfg(not(debug))]
    #[macro_escape]
    mod debug_macro {
        priv use std::prelude::*;
    }
}
pub struct Color {
    red: u8,
    green: u8,
    blue: u8,
    alpha: f64,
}
fn main() {
    {
        static black: Color =
            Color{red: 0 as u8, green: 255 as u8, blue: 0 as u8, alpha: 1.0,};
    };
}

@metajack
Copy link
Contributor

metajack commented Aug 7, 2013

@recrack I suggested a workaround in rust#8210. What did you think?

@recrack
Copy link
Contributor Author

recrack commented Aug 8, 2013

@metajack After i try your suggestion, and i will tell you. I think, your suggestion is good to me. i will try it.

@recrack
Copy link
Contributor Author

recrack commented Aug 12, 2013

@metajack i was try to make color macro-izing. but this code is not pretty.
May i have you good example code?
I want to follow you opinion.

and i have one question.

Variable is lowercase. Is it ok?

define_color!(aliceblue, 240, 248, 255);

which would expand to:

static aliceblue: Color = Color { red: 240, green: 248, blue: 255, alpha: 1.0 };

@metajack
Copy link
Contributor

Here's a working set of the two macros. Perhpas all the method calls in parse_static_color! can be avoided, but I didn't look to see what hte inveraints were on the input that's passed in.

use std::ascii::AsciiStr;

struct Color { red: u8, green: u8, blue: u8, alpha: f64 }

macro_rules! define_color(
    ($color:ident, $r:expr, $g:expr, $b:expr) => {
        static $color: Color = Color { red: $r as u8, green: $g as u8, blue: $b as u8, alpha: 1.0 };
    }
)

macro_rules! parse_static_color(
    ($name:expr, $($color:ident),+) => {
        {
            let name = $name.trim().to_owned().into_ascii().to_upper().into_str();
            let mut color = None;
            $(
                if (stringify!($color) == name) {
                    color = Some($color);
                }
            )+
            color
        }
    }
)

define_color!(ALICEBLUE, 240, 248, 255)

fn parse_color(name: ~str) -> Option<Color> {
    parse_static_color!(name, ALICEBLUE)
}

fn main() {
    let name = ~"aliceblue";
    let color = parse_color(name);
    printfln!("%?", color);
}

@recrack
Copy link
Contributor Author

recrack commented Aug 19, 2013

Awesome!! @metajack.
Thank you for your help :)
r?

@metajack
Copy link
Contributor

You almost have it.

parse_static_color! can take multiple arguments. Maybe I shouldn't have been lazy in my example. The intent is that you call it like:

parse_static_color!(color, ALICEBLUE, ANTIQUEWHITE, ...) with each color listed. Then you can remove that huge block of code with the single macro invocation.

metajack added a commit that referenced this pull request Aug 20, 2013
rust-css add colors & test cases
@metajack metajack merged commit cadbf33 into servo:master Aug 20, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants