waterui-cli 0.3.2

Cross-platform tooling for WaterUI applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! ESP32 backend configuration and initialization.

use std::path::{Path, PathBuf};

use cargo_toml::Manifest as CargoManifest;
use serde::{Deserialize, Serialize};

use crate::{
    backend::Backend,
    build::BuildOptions,
    device::Artifact,
    esp32::{
        chip::Esp32Chip,
        platform::{build_esp32, clean_esp32, is_esp32_platform, package_esp32},
    },
    platform::{PackageOptions, TargetPlatform},
    project::Project,
    templates::{self, Esp32TemplateEntry, TemplateContext},
};

#[cfg(feature = "esp32")]
fn subset_font(path: &Path, ranges: &str, output_dir: &Path) -> eyre::Result<PathBuf> {
    crate::esp32::fonts::subset_into(path, ranges, output_dir)
}

#[cfg(not(feature = "esp32"))]
fn subset_font(_path: &Path, _ranges: &str, _output_dir: &Path) -> eyre::Result<PathBuf> {
    eyre::bail!("[backends.esp32] font_ranges requires the `esp32` feature of waterui-cli")
}

/// Configuration for the ESP32 backend in a `WaterUI` project.
///
/// `[backends.esp32]` in `Water.toml`
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Esp32Backend {
    #[serde(
        default = "default_esp32_project_path",
        skip_serializing_if = "is_default_esp32_project_path"
    )]
    project_path: PathBuf,
    #[serde(
        default = "default_esp32_chip",
        skip_serializing_if = "is_default_esp32_chip"
    )]
    chip: String,
    #[serde(
        default = "default_esp32_panel_width",
        skip_serializing_if = "is_default_esp32_panel_width"
    )]
    panel_width: u32,
    #[serde(
        default = "default_esp32_panel_height",
        skip_serializing_if = "is_default_esp32_panel_height"
    )]
    panel_height: u32,
    #[serde(
        default = "default_esp32_band_height",
        skip_serializing_if = "is_default_esp32_band_height"
    )]
    band_height: u32,
    /// TTF/OTF binaries bundled into flash for dew text shaping, relative to
    /// the project root. Firmware has no font directory to enumerate, so a
    /// text-rendering app must list at least one face here.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    fonts: Vec<PathBuf>,
    /// Unicode ranges to subset every bundled font to before embedding
    /// (e.g. `["U+0020-007E", "U+00A0-00FF"]`). Absent means the whole font
    /// is embedded. Subsetting is explicit because it silently drops glyphs
    /// outside the ranges; when set, a full Latin face shrinks from
    /// hundreds of kilobytes of flash to a few dozen.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    font_ranges: Vec<String>,
}

impl Esp32Backend {
    /// Create a new ESP32 backend configuration with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            project_path: default_esp32_project_path(),
            chip: default_esp32_chip(),
            panel_width: default_esp32_panel_width(),
            panel_height: default_esp32_panel_height(),
            band_height: default_esp32_band_height(),
            fonts: Vec::new(),
            font_ranges: Vec::new(),
        }
    }

    /// Set a custom project path (defaults to "esp32").
    #[must_use]
    pub fn with_project_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.project_path = path.into();
        self
    }

    /// Set the target chip, returning the updated configuration.
    #[must_use]
    pub fn with_chip(mut self, chip: Esp32Chip) -> Self {
        self.chip = chip.id().to_string();
        self
    }

    /// Get the path to the ESP32 harness project within the `WaterUI` project.
    #[must_use]
    pub const fn project_path(&self) -> &PathBuf {
        &self.project_path
    }

    /// Get the configured target chip identifier (e.g. "esp32s3").
    #[must_use]
    pub fn chip(&self) -> &str {
        &self.chip
    }

    /// Parse the configured chip into an [`Esp32Chip`].
    ///
    /// # Errors
    ///
    /// Returns an error when the configured chip string is not a supported
    /// ESP32 chip.
    pub fn resolved_chip(&self) -> eyre::Result<Esp32Chip> {
        self.chip.parse()
    }

    /// Get the harness parameters substituted into generated templates.
    ///
    /// Font paths are resolved against `project_root` so the generated
    /// harness can `include_bytes!` them from wherever it lives. When
    /// `font_ranges` is configured, each font is subset to those ranges
    /// into `harness_fonts_dir` and the subset file is embedded instead.
    ///
    /// # Errors
    ///
    /// Returns an error when the configured chip string is not a supported
    /// ESP32 chip, when a configured font file does not exist, or when
    /// subsetting fails.
    pub fn template_entry(
        &self,
        project_root: &Path,
        harness_fonts_dir: &Path,
    ) -> eyre::Result<Esp32TemplateEntry> {
        let ranges = self.font_ranges.join(",");
        let fonts = self
            .fonts
            .iter()
            .map(|font| {
                let path = if font.is_absolute() {
                    font.clone()
                } else {
                    project_root.join(font)
                };
                if !path.is_file() {
                    eyre::bail!(
                        "[backends.esp32] fonts entry {} does not exist (resolved to {})",
                        font.display(),
                        path.display()
                    );
                }
                let path = if ranges.is_empty() {
                    path
                } else {
                    subset_font(&path, &ranges, harness_fonts_dir)?
                };
                Ok(path.to_string_lossy().into_owned())
            })
            .collect::<eyre::Result<Vec<_>>>()?;
        Ok(Esp32TemplateEntry::new(
            self.resolved_chip()?,
            self.panel_width,
            self.panel_height,
            self.band_height,
        )
        .with_fonts(fonts))
    }

    /// Check whether generated ESP32 harness files should be regenerated.
    ///
    /// This is used by playground mode where backend glue code is fully managed by the CLI.
    ///
    /// # Errors
    ///
    /// Returns an error when the harness `Cargo.toml` exists but cannot be parsed.
    pub fn requires_regeneration(project: &Project) -> eyre::Result<bool> {
        let backend_path = project.backend_path::<Self>();
        let cargo_toml_path = backend_path.join("Cargo.toml");
        if !cargo_toml_path.exists() {
            return Ok(true);
        }

        let manifest =
            CargoManifest::<cargo_toml::Value>::from_path(&cargo_toml_path).map_err(|error| {
                eyre::eyre!("failed to parse {}: {error}", cargo_toml_path.display())
            })?;
        let main_rs = std::fs::read_to_string(backend_path.join("src/main.rs")).unwrap_or_default();
        let config = project
            .esp32_backend()
            .cloned()
            .unwrap_or_default()
            .template_entry(project.root(), &backend_path.join("fonts"))?;
        let main_matches_panel = main_rs.contains(&format!(
            "PanelConfig::new({}, {}, {})",
            config.panel_width, config.panel_height, config.band_height
        ));
        let main_matches_fonts = main_rs.matches("include_bytes!").count() == config.fonts.len()
            && config
                .fonts
                .iter()
                .all(|font| main_rs.contains(font.as_str()));
        let cargo_target_matches = backend_path
            .join(".cargo/config.toml")
            .exists()
            .then(|| std::fs::read_to_string(backend_path.join(".cargo/config.toml")).ok())
            .flatten()
            .is_some_and(|cargo_config| {
                cargo_config.contains(&format!("target = \"{}\"", config.resolved_target_triple()))
            });

        // A manifest rendered before the backend carried the framework patch
        // tables lets `waterui-dew`'s own `waterui-*` requirements resolve
        // beside the project's copies — the recorded framework selection
        // produces the patch set the manifest must already carry.
        // The emitter (`generated_crate_patches`) prefers the checkout
        // whenever `waterui_path` resolves, so the comparison must name the
        // arms in the same order — a manifest carrying both fields emits the
        // checkout's set, and expecting the channel's would regenerate
        // forever.
        let expected_patches = match (
            &project.manifest().waterui_path,
            &project.manifest().framework,
        ) {
            (Some(waterui_path), _) => {
                let path = Path::new(waterui_path);
                let root = if path.is_absolute() {
                    path.to_path_buf()
                } else {
                    project.root().join(path)
                };
                Some(
                    crate::project_model::templates::collect_workspace_patches(&root).map_err(
                        |error| {
                            eyre::eyre!(
                                "failed to read the WaterUI checkout's patch tables at {}: {error}",
                                root.display()
                            )
                        },
                    )?,
                )
            }
            (None, Some(framework)) => Some(framework.patches()),
            (None, None) => None,
        };

        // The generated package name carries the project-root tag — a
        // manifest rendered before it did must be rewritten, or artifact
        // lookups would go looking for the tagged name.
        let package_name_matches = manifest
            .package
            .as_ref()
            .is_some_and(|package| package.name == project.esp32_backend_crate_name().as_str());

        Ok(!package_name_matches
            || !manifest.dependencies.contains_key("waterui-dew")
            || !main_matches_panel
            || !main_matches_fonts
            || !cargo_target_matches
            || expected_patches.is_some_and(|expected| manifest.patch != expected)
            || !backend_path.join("rust-toolchain.toml").exists()
            || !backend_path.join(".cargo/config.toml").exists()
            || !backend_path.join("sdkconfig.defaults").exists()
            || !backend_path.join("partitions.csv").exists()
            || !backend_path.join("build.rs").exists())
    }
}

impl Default for Esp32Backend {
    fn default() -> Self {
        Self::new()
    }
}

impl Backend for Esp32Backend {
    const DEFAULT_PATH: &'static str = "esp32";

    // The ESP32 harness uses Cargo build cache under the project target tree.
    const CACHE_PATHS: &'static [&'static str] = &[];

    fn path(&self) -> &Path {
        &self.project_path
    }

    async fn init(project: &Project) -> Result<Self, crate::backend::FailToInitBackend> {
        let manifest = project.manifest();
        let backend = project.esp32_backend().cloned().unwrap_or_default();

        let app_name = manifest
            .package
            .name
            .chars()
            .filter(|c| c.is_alphanumeric())
            .collect::<String>();
        let template_entry = backend
            .template_entry(
                project.root(),
                &project.backend_path::<Self>().join("fonts"),
            )
            .map_err(crate::backend::FailToInitBackend::Config)?;
        if template_entry.fonts.is_empty() {
            tracing::warn!(
                "[backends.esp32] bundles no fonts; dew fails fast at the first text layout. \
                 Add `fonts = [\"path/to/Font.ttf\"]` (relative to the project root) to render text."
            );
        }
        let ctx = TemplateContext::for_project_manifest(
            manifest,
            project.crate_name().clone(),
            app_name,
            &project
                .resolved_framework()
                .await
                .map_err(crate::backend::FailToInitBackend::Config)?,
        )
        .with_backend_project_path(project.backend_path::<Self>())
        .with_project_root_path(project.root().to_path_buf())
        .with_esp32(template_entry);

        templates::esp32::scaffold(&project.backend_path::<Self>(), &ctx)
            .await
            .map_err(crate::backend::FailToInitBackend::Io)?;

        Ok(backend)
    }

    fn supports(&self, platform: TargetPlatform) -> bool {
        is_esp32_platform(platform)
    }

    async fn build(
        &self,
        project: &Project,
        platform: TargetPlatform,
        options: BuildOptions,
    ) -> eyre::Result<PathBuf> {
        if !is_esp32_platform(platform) {
            eyre::bail!("ESP32 backend only supports the esp32s3, esp32c3, and esp32p4 platforms");
        }
        build_esp32(project, options).await
    }

    async fn package(
        &self,
        project: &Project,
        platform: TargetPlatform,
        options: PackageOptions,
    ) -> eyre::Result<Artifact> {
        if !is_esp32_platform(platform) {
            eyre::bail!("ESP32 backend only supports the esp32s3, esp32c3, and esp32p4 platforms");
        }
        package_esp32(project, options).await
    }

    async fn clean(&self, project: &Project, _platform: TargetPlatform) -> eyre::Result<()> {
        clean_esp32(project).await
    }
}

fn default_esp32_project_path() -> PathBuf {
    PathBuf::from("esp32")
}

fn is_default_esp32_project_path(path: &Path) -> bool {
    path == Path::new("esp32")
}

fn default_esp32_chip() -> String {
    "esp32s3".to_string()
}

fn is_default_esp32_chip(chip: &str) -> bool {
    chip == "esp32s3"
}

const fn default_esp32_panel_width() -> u32 {
    410
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde skip_serializing_if requires a reference predicate"
)]
const fn is_default_esp32_panel_width(width: &u32) -> bool {
    *width == default_esp32_panel_width()
}

const fn default_esp32_panel_height() -> u32 {
    502
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde skip_serializing_if requires a reference predicate"
)]
const fn is_default_esp32_panel_height(height: &u32) -> bool {
    *height == default_esp32_panel_height()
}

const fn default_esp32_band_height() -> u32 {
    16
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde skip_serializing_if requires a reference predicate"
)]
const fn is_default_esp32_band_height(band_height: &u32) -> bool {
    *band_height == default_esp32_band_height()
}