Skip to content

Instantly share code, notes, and snippets.

@MartinKavik
Created June 24, 2020 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinKavik/7bafc2d37378685b7c9dcace9b92d8c8 to your computer and use it in GitHub Desktop.
Save MartinKavik/7bafc2d37378685b7c9dcace9b92d8c8 to your computer and use it in GitHub Desktop.
breakpoints!
// ------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);
}
@MartinKavik
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment