Created
June 24, 2020 19:09
-
-
Save MartinKavik/7bafc2d37378685b7c9dcace9b92d8c8 to your computer and use it in GitHub Desktop.
breakpoints!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------Seed code ------ | |
#[derive(Debug)] | |
struct BreakpointInterval<B> { | |
id: B, | |
start: u32, | |
end: Option<u32>, | |
} | |
#[derive(Debug)] | |
struct Theme<B> { | |
id: String, | |
breakpoints: Vec<BreakpointInterval<B>>, | |
breakpoint_scale: Vec<u32>, | |
} | |
impl<B> Theme<B> { | |
fn new(id: impl ToString) -> Self { | |
Self { | |
id: id.to_string(), | |
breakpoints: Vec::new(), | |
breakpoint_scale: Vec::new(), | |
} | |
} | |
fn set_breakpoints(mut self, breakpoints: Vec<BreakpointInterval<B>>) -> Self { | |
self.breakpoint_scale = breakpoints.iter().skip(1).map(|interval| interval.start).collect(); | |
self.breakpoints = breakpoints; | |
self | |
} | |
} | |
macro_rules! breakpoints { | |
{ $($num:expr, $id:expr $(,)?)* } => { | |
{ | |
let mut nums = Vec::new(); | |
let mut ids = Vec::new(); | |
$( | |
nums.push($num); | |
ids.push($id); | |
)* | |
breakpoint_intervals(nums, ids) | |
} | |
} | |
} | |
fn breakpoint_intervals<B>(nums: Vec<u32>, ids: Vec<B>) -> Vec<BreakpointInterval<B>> { | |
ids.into_iter().enumerate().map(|(index, id)| { | |
BreakpointInterval { | |
id, | |
start: nums[index], | |
end: nums.get(index+1).copied(), | |
} | |
}).collect() | |
} | |
// ------App code ------ | |
#[derive(Debug)] | |
enum Breakpoint { | |
ExtraSmall, | |
Small, | |
Medium, | |
Large, | |
ExtraLarge, | |
} | |
fn main() { | |
use Breakpoint::*; | |
let theme = Theme::new("my_theme").set_breakpoints(breakpoints![ | |
0, ExtraSmall, 600, Small, 960, Medium, 1280, Large, 1920, ExtraLarge | |
]); | |
println!("{:#?}", theme); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runnable in https://play.rust-lang.org/