fn generate_node()

in GaiaXStretch/scripts/gentest/src/main.rs [247:466]


fn generate_node(ident: &str, node: &json::JsonValue) -> TokenStream {
    let style = &node["style"];

    let display = match style["display"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "none" => quote!(display: stretch::style::Display::None,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let position_type = match style["position_type"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "absolute" => quote!(position_type: stretch::style::PositionType::Absolute,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let direction = match style["direction"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "rtl" => quote!(direction: stretch::style::Direction::RTL,),
            "ltr" => quote!(direction: stretch::style::Direction::LTR,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let flex_direction = match style["flexDirection"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "row-reverse" => quote!(flex_direction: stretch::style::FlexDirection::RowReverse,),
            "column" => quote!(flex_direction: stretch::style::FlexDirection::Column,),
            "column-reverse" => quote!(flex_direction: stretch::style::FlexDirection::ColumnReverse,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let flex_wrap = match style["flexWrap"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "wrap" => quote!(flex_wrap: stretch::style::FlexWrap::Wrap,),
            "wrap-reverse" => quote!(flex_wrap: stretch::style::FlexWrap::WrapReverse,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let overflow = match style["overflow"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "hidden" => quote!(overflow: stretch::style::Overflow::Hidden,),
            "scroll" => quote!(overflow: stretch::style::Overflow::Scroll,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let align_items = match style["alignItems"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "flex-start" => quote!(align_items: stretch::style::AlignItems::FlexStart,),
            "flex-end" => quote!(align_items: stretch::style::AlignItems::FlexEnd,),
            "center" => quote!(align_items: stretch::style::AlignItems::Center,),
            "baseline" => quote!(align_items: stretch::style::AlignItems::Baseline,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let align_self = match style["alignSelf"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "flex-start" => quote!(align_self: stretch::style::AlignSelf::FlexStart,),
            "flex-end" => quote!(align_self: stretch::style::AlignSelf::FlexEnd,),
            "center" => quote!(align_self: stretch::style::AlignSelf::Center,),
            "baseline" => quote!(align_self: stretch::style::AlignSelf::Baseline,),
            "stretch" => quote!(align_self: stretch::style::AlignSelf::Stretch,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let align_content = match style["alignContent"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "flex-start" => quote!(align_content: stretch::style::AlignContent::FlexStart,),
            "flex-end" => quote!(align_content: stretch::style::AlignContent::FlexEnd,),
            "center" => quote!(align_content: stretch::style::AlignContent::Center,),
            "space-between" => quote!(align_content: stretch::style::AlignContent::SpaceBetween,),
            "space-around" => quote!(align_content: stretch::style::AlignContent::SpaceAround,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let justify_content = match style["justifyContent"] {
        json::JsonValue::Short(ref value) => match value.as_ref() {
            "flex-end" => quote!(justify_content: stretch::style::JustifyContent::FlexEnd,),
            "center" => quote!(justify_content: stretch::style::JustifyContent::Center,),
            "space-between" => quote!(justify_content: stretch::style::JustifyContent::SpaceBetween,),
            "space-around" => quote!(justify_content: stretch::style::JustifyContent::SpaceAround,),
            "space-evenly" => quote!(justify_content: stretch::style::JustifyContent::SpaceEvenly,),
            _ => quote!(),
        },
        _ => quote!(),
    };

    let flex_grow = match style["flexGrow"] {
        json::JsonValue::Number(value) => {
            let value: f32 = value.into();
            quote!(flex_grow: #value,)
        }
        _ => quote!(),
    };

    let flex_shrink = match style["flexShrink"] {
        json::JsonValue::Number(value) => {
            let value: f32 = value.into();
            quote!(flex_shrink: #value,)
        }
        _ => quote!(),
    };

    let flex_basis = match style["flexBasis"] {
        json::JsonValue::Object(ref value) => {
            let value = generate_dimension(value);
            quote!(flex_basis: #value,)
        }
        _ => quote!(),
    };

    let size = match style["size"] {
        json::JsonValue::Object(ref value) => {
            let size = generate_size(value);
            quote!(size: #size,)
        }
        _ => quote!(),
    };

    let min_size = match style["min_size"] {
        json::JsonValue::Object(ref value) => {
            let min_size = generate_size(value);
            quote!(min_size: #min_size,)
        }
        _ => quote!(),
    };

    let max_size = match style["max_size"] {
        json::JsonValue::Object(ref value) => {
            let max_size = generate_size(value);
            quote!(max_size: #max_size,)
        }
        _ => quote!(),
    };

    macro_rules! edges_quoted {
        ($style:ident, $val:ident) => {
            let $val = match $style[stringify!($val)] {
                json::JsonValue::Object(ref value) => {
                    let edges = generate_edges(value);
                    quote!($val: #edges,)
                },
                _ => quote!(),
            };
        };
    }

    edges_quoted!(style, margin);
    edges_quoted!(style, padding);
    edges_quoted!(style, position);
    edges_quoted!(style, border);

    let (children_body, children) = match node["children"] {
        json::JsonValue::Array(ref value) => {
            if value.len() > 0 {
                let body = value
                    .iter()
                    .enumerate()
                    .map(|(i, child)| generate_node(&format!("{}{}", ident, i), child))
                    .collect();
                let idents = value
                    .iter()
                    .enumerate()
                    .map(|(i, _)| Ident::new(&format!("{}{}", ident, i), Span::call_site()))
                    .collect::<Vec<_>>();
                (body, quote!(vec![#(#idents),*]))
            } else {
                (quote!(), quote!(vec![]))
            }
        }
        _ => (quote!(), quote!()),
    };

    let ident = Ident::new(&format!("{}", ident), Span::call_site());

    quote!(
        #children_body
        let #ident = stretch.new_node(
        stretch::style::Style {
            #display
            #direction
            #position_type
            #flex_direction
            #flex_wrap
            #overflow
            #align_items
            #align_self
            #align_content
            #justify_content
            #flex_grow
            #flex_shrink
            #flex_basis
            #size
            #min_size
            #max_size
            #margin
            #padding
            #position
            #border
            ..Default::default()
        },
        #children
    ).unwrap();)
}