serge-widget-codegen 0.1.3

Compile Serge's static mobile widget markup for WidgetKit and Jetpack Glance
Documentation
//! An `Icon` names an SF Symbol on iOS and a drawable resource on Android
//! (`docs/01-dx-and-dsl.md` ยง1). A name Android cannot have is a compile
//! error at the literal, not broken Kotlin.
#![allow(missing_docs)]

use serge_widget_codegen::{Platform, Widget};

fn declaration(platform: Platform, icon: &str) -> String {
    let body = match platform {
        Platform::LiveActivity => format!(
            "live_activity! {{\n        compact_leading: ui! {{ Icon {{ {icon:?} }} }},\n        compact_trailing: ui! {{ Text {{ \"{{state.name}}\" }} }},\n        expanded: ui! {{ Text {{ \"{{state.name}}\" }} }}\n    }}"
        ),
        _ => format!(
            "ui! {{\n        Row {{ Icon {{ {icon:?} }} Text {{ \"{{state.name}}\" }} }}\n    }}"
        ),
    };
    format!("fn Status(state: S) -> W {{\n    {body}\n}}")
}

fn parse(platform: Platform, source: &str) -> syn::Result<Widget> {
    Widget::parse(platform, &syn::parse_str(source).unwrap())
}

#[test]
fn an_android_icon_outside_the_resource_name_rules_fails_at_the_literal() {
    for icon in [
        "cube.fill",
        "Cube",
        "1cube",
        "_cube",
        "cube-fill",
        "",
        "new",
        "class",
        "x)\nval y = (z",
    ] {
        let source = declaration(Platform::Glance, icon);
        let error = parse(Platform::Glance, &source)
            .err()
            .unwrap_or_else(|| panic!("{icon:?} was accepted as a drawable name"));
        assert!(
            error.to_string().contains("drawable resource name"),
            "{icon:?}: {error}"
        );
        let start = error.span().start();
        let literal = format!("{icon:?}");
        let offset = source.find(&literal).unwrap();
        let line = source[..offset].matches('\n').count() + 1;
        let column = offset - source[..offset].rfind('\n').map_or(0, |i| i + 1);
        assert_eq!((start.line, start.column), (line, column), "{icon:?}");
    }
}

#[test]
fn an_android_icon_names_its_drawable_and_kotlin_keywords_are_escaped() {
    for (icon, reference) in [
        ("cube_fill", "R.drawable.`cube_fill`"),
        ("ic_export2", "R.drawable.`ic_export2`"),
        ("in", "R.drawable.`in`"),
        ("object", "R.drawable.`object`"),
    ] {
        let widget = parse(Platform::Glance, &declaration(Platform::Glance, icon))
            .unwrap_or_else(|e| panic!("{icon:?}: {e}"));
        let kotlin = widget.source("org.example.status").unwrap();
        assert!(
            kotlin.contains(&format!(
                "Image(ImageProvider({reference}), contentDescription = \"{icon}\")"
            )),
            "{kotlin}"
        );
    }
}

#[test]
fn an_ios_icon_is_an_sf_symbol_name_passed_through() {
    for platform in [Platform::WidgetKit, Platform::LiveActivity] {
        for icon in ["cube.fill", "cube", "0.circle"] {
            let widget = parse(platform, &declaration(platform, icon))
                .unwrap_or_else(|e| panic!("{icon:?}: {e}"));
            let swift = widget.source("").unwrap();
            assert!(
                swift.contains(&format!("Image(systemName: \"{icon}\")")),
                "{swift}"
            );
        }
    }
}