# misc/buildinfo (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/buildinfo/

Build info. (original misc/buildinfo.js)
- Reads the version written by the release build once when the app starts. Reading order:
  1. The content of <meta name="build-version" content="..."> in the document head. (that text as is if present, Document::get_meta_content of platform/document.rs)
     On the web it is the page's meta, as in the original. On desktop it is the meta of the document held by the engine, which the release build puts in EngineConfiguration.document_meta_list.
     (the meta slot of the web version's index.html, something like reading build.rs's rustc-env with env! and putting it in)
  2. Otherwise version.json. In the form { version, date, time, hash, ... }, and the text is "date time (hash)". version if there is no hash.
     On the web, fetches the page-relative address without the cache (fetch's cache: "no-cache"). So the old version display does not linger after uploading a new version.
     On desktop it reads from the asset folder. (finishes on the spot, and as a file it has no cache that keeps the old version)
     If it cannot be fetched (no file, HTTP error, network error), it quietly moves on, just as the original load swallows the error.
     The handle finishes with empty bytes and does not count as a failure, so it is not in the failed list even when added to an AssetLoader.
  3. If neither, the version put in with set_package_version. (the game crate passes env!("CARGO_PKG_VERSION"), an empty string if not set, as in the original)
- Differences from the original:
  The default version.json address argument of the original constructor is split into new() (default address) and set_version_json_path.
  The original await load() waits until it is fetched. The Rust version's load(engine) also returns a BuildInfoLoadHandle that can be awaited. (wait_for_load().await is the same)
  Without await, check completion with is_loaded(), and scenes hand get_load_handle() over to track_load_handle.

## BuildInfoLoadHandle (struct) (Clone, Debug, IntoFuture)

Build info load handle. (returned by BuildInfo::load. In place of the original load's Promise, it finishes immediately if read from meta)
- Await it as is inside an engine task. It also finishes if reading fails.

Example: Await load in an engine task

```rust
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{BuildInfo, Engine, EngineConfiguration};

let mut engine_configuration = EngineConfiguration::new();
engine_configuration.document_meta_list = vec![(String::from("build-version"), String::from("1.2.3"))];
let engine = Engine::new(engine_configuration);
let version_text = Rc::new(RefCell::new(String::new()));
let version_text_for_task = version_text.clone();
let task_handle = engine.spawn_task(|mut engine_context| async move {
	let mut build_info = BuildInfo::new();
	let load_handle = engine_context.with(|engine| build_info.load(engine));
	load_handle.await;
	*version_text_for_task.borrow_mut() = build_info.read_text();
});
assert!(task_handle.is_finished());
assert_eq!(*version_text.borrow(), "1.2.3");
```

## BuildInfoLoadFuture (struct) (Clone, Debug, Future)

Future that waits for the build info to load. (finished even if it fails)
- Obtained with BuildInfo::wait_for_load or BuildInfoLoadHandle's into_future. If not ready, checks again on the next frame.

Example: Call load and then wait with wait_for_load

```rust
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{BuildInfo, Engine, EngineConfiguration};

let mut engine_configuration = EngineConfiguration::new();
engine_configuration.document_meta_list = vec![(String::from("build-version"), String::from("2026-11-26 def5678"))];
let engine = Engine::new(engine_configuration);
let build_info = Rc::new(RefCell::new(BuildInfo::new()));
build_info.borrow_mut().load(&engine);
let build_info_for_task = build_info.clone();
let task_handle = engine.spawn_task(|_engine_context| async move {
	let load_future = build_info_for_task.borrow().wait_for_load();
	load_future.await;
	assert!(build_info_for_task.borrow().is_loaded());
});
assert!(task_handle.is_finished());
assert_eq!(build_info.borrow_mut().read_text(), "2026-11-26 def5678");
```

## BuildInfo (struct) (Default)

Build info.
- Call load once at startup, and after it finishes read the version text with read_text. (in order: meta, version.json, the version set at build time)

Example: Read the version written in the document's meta

```rust
use vanilla::{BuildInfo, Engine, EngineConfiguration};

let mut engine_configuration = EngineConfiguration::new();
engine_configuration.document_meta_list = vec![(String::from("build-version"), String::from(" 2026-11-26 def5678 "))];
let engine = Engine::new(engine_configuration);

let mut build_info = BuildInfo::new();
build_info.set_package_version(env!("CARGO_PKG_VERSION"));
assert!(!build_info.is_loaded());
build_info.load(&engine);
assert!(build_info.is_loaded());
assert!(build_info.get_load_handle().is_none());
assert_eq!(build_info.read_text(), "2026-11-26 def5678");
```

- `pub fn new() -> BuildInfo` (constructor, since 0.4.0): Constructor. (the version.json address defaults to "version.json")
  - returns BuildInfo: New build info not read yet. (meta name "build-version", the build-time version is an empty string)
- `pub fn set_meta_name(&mut self, meta_name: &str)` (method, since 0.4.0): Sets the meta name. (default "build-version")
  - `meta_name` &str: name of the meta that holds the version.
- `pub fn set_version_json_path(&mut self, version_json_path: &str)` (method, since 0.4.0): Sets the build version file path. (default "version.json", an empty string means the file is not fetched)
  - `version_json_path` &str: version.json path. (page-relative address on the web, relative to the asset folder on desktop)
- `pub fn load(&mut self, engine: &Engine) -> BuildInfoLoadHandle` (method, since 0.4.0): Requests reading. (reads the document's meta immediately, and if absent, the web requests version.json without the cache. Desktop reads version.json from the asset folder. Returns a handle that can be awaited)
  - Calling again clears what was read before and reads from the beginning.
  - `engine` &Engine: Engine to load the asset with.
  - returns BuildInfoLoadHandle: Handle that waits for reading. An already finished handle if read from meta or the file path is an empty string.

  Example: The build-time version if there is neither meta nor version.json

  ```rust
  use vanilla::{BuildInfo, Engine, EngineConfiguration};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut build_info = BuildInfo::new();
  build_info.set_package_version("0.4.0");
  build_info.set_version_json_path("missing-version.json");
  build_info.load(&engine);
  assert!(build_info.is_loaded());
  assert!(build_info.read_version_data().is_none());
  assert_eq!(build_info.read_text(), "0.4.0");
  ```

- `pub fn is_loaded(&self) -> bool` (method, since 0.4.0): Whether reading has finished. (original isLoaded, false before load, finished even if it fails)
  - returns bool: true if load was called and it was read from meta or fetching version.json has finished.
- `pub fn wait_for_load(&self) -> BuildInfoLoadFuture` (method, since 0.4.0): Future that waits for reading to finish. (in place of the await in the original await load(), await it inside an engine task after calling load. Finished even if it fails)
  - Finishes immediately if read from meta. Shares the handle instead of borrowing.
  - returns BuildInfoLoadFuture: Future waiting for the current read. Finishes immediately if called before load.
- `pub fn get_load_handle(&self) -> Option<&AssetLoadHandle>` (method, since 0.4.0): version.json load handle. (scenes hand it over with track_load_handle. None if read from meta)
  - Finishes with empty bytes, not a failure, even if it could not be fetched. (like the try/catch of the original load, it is not in the AssetLoader failed list)
  - returns Option<&AssetLoadHandle>: version.json fetch handle. None before load, if read from meta, or if the file path is an empty string.
- `pub fn read_version_data(&mut self) -> Option<&Value>` (method, since 0.4.0): Value read from version.json. (None before fetching, if read from meta, or if unparsable)
  - Parsed only once after the first fetch, and the same value is returned.
  - returns Option<&Value>: JSON object value. None if not an object, not fetched, or not ready yet.
- `pub fn read_text(&mut self) -> String` (method, since 0.4.0): Version text. (meta > version.json > build-time version)
  - returns String: The meta text (trimmed), otherwise the version.json text built with compose_text, and if that is empty too, the build-time version. (an empty string if not set)
- `pub fn read_meta_text(&self) -> String` (method, since 0.4.0): Version written in the document's meta. (original readMetaText, an empty string if none. On desktop, the meta of the document held by the engine)
  - returns String: The trimmed content of the meta with the same meta name. An empty string if none.
- `pub fn set_package_version(&mut self, package_version: &str)` (method, since 0.4.0): Sets the build-time version. (the text when there is neither meta nor version.json, usually env!("CARGO_PKG_VERSION"))
  - `package_version` &str: The version text.
- `pub fn get_package_version(&self) -> &str` (method, since 0.4.0): Build-time version.
  - returns &str: Text put in with set_package_version. (initially an empty string)
- `pub fn compose_text(version_data: &Value) -> String` (static, since 0.4.0): Builds the version text from a version.json value. (Static, "date time (hash)", version if there is no hash, empty text if that is also missing)
  - `version_data` &Value: JSON value of version.json. (reads only string fields and trims them)
  - returns String: Version text. If the date or time is missing, joins without that piece.

  Example: With and without a hash

  ```rust
  use vanilla::BuildInfo;
  
  let version_data = serde_json::json!({ "version": "1.2.3", "date": "2026-11-25", "time": "04:00", "hash": "abc1234" });
  assert_eq!(BuildInfo::compose_text(&version_data), "2026-11-25 04:00 (abc1234)");
  assert_eq!(BuildInfo::compose_text(&serde_json::json!({ "version": " 2.0.0 " })), "2.0.0");
  assert_eq!(BuildInfo::compose_text(&serde_json::json!({ "hash": "def5678" })), "(def5678)");
  ```

