# resource/textasset (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/resource/textasset/

Text asset. (the original resource/textasset.js, resource/jsonasset.js)
- The original TextAsset keeps the text received with fetch in text, and JsonAsset keeps the JSON.parse result in data.
- The Rust version decodes it as UTF-8 text on first access once the bytes arrive, and JsonAsset decodes it into serde_json::Value.
- Like the original, JsonAsset also holds TextAsset's text (get_text), and JSON that cannot be parsed counts as not loaded (is_loaded false, get_data None).
  When received by AssetLoader it goes into the failure list as in the original. (the parsed data is shared with the AssetLoader list entry so it is parsed only once)
- set_no_cache: if true, adds cache: "no-cache" to web fetch so old data is not used after a deploy. (same as the original, desktop always reads the file anew, so the result is the same as on)
- load returns a load handle that can be awaited as in the original. (like the original JsonAsset.load, if receiving or JSON parsing fails, the await ends in failure and get_data is None)

## TextAsset (struct) (Default, Asset)

Text asset.
- Once the bytes arrive, decodes them as UTF-8 text on the first get_text.

Example: Read from a data: URL and from bytes in hand

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

let engine = Engine::new(EngineConfiguration::new());
let mut text_asset = TextAsset::new();
text_asset.load(&engine, "data:,hello");
assert_eq!(text_asset.get_text(), Some("hello"));

let mut note_asset = TextAsset::new();
assert_eq!(note_asset.get_text(), None);
note_asset.load_from_bytes("note.txt", b"line one".to_vec());
assert_eq!(note_asset.get_text(), Some("line one"));
```

- `pub fn new() -> TextAsset` (constructor, since 0.3.2): Constructor.
  - returns TextAsset: Empty text asset before a request. (cache busting off)
- `pub fn set_no_cache(&mut self, is_no_cache: bool)` (method, since 0.4.0): Sets whether to load with cache busting. (if true, adds cache: "no-cache" to web fetch, for data table JSON, etc.)
  - `is_no_cache` bool: If true, does not use the browser cache on web. (set before load, desktop always reads the file anew, which is the same as on)
- `pub fn is_no_cache(&self) -> bool` (method, since 0.4.0): Returns whether to load with cache invalidation.
  - returns bool: true if it does not use the browser cache on web. (default false)
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle` (method, since 0.3.2): Requests a read. (the original load, returns a load handle that can be awaited)
  - If already requested, does not request again and returns the first handle.
  - `engine` &Engine: Engine to ask for the bytes.
  - `asset_path` &str: Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)
  - returns AssetLoadHandle: Load handle. (on desktop it is read on the spot and already finished. Finishes in failure if it cannot be read)
- `pub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>)` (method, since 0.3.2): Reads from bytes already in hand.
  - Discards the previously decoded text. Replaces it with a new handle even if a requested handle exists.
  - `asset_path` &str: Path to record.
  - `bytes` Vec<u8>: Text bytes. (UTF-8)
- `pub fn unload(&mut self)` (method, since 0.3.2): Unloads.
- `pub fn get_text(&mut self) -> Option<&str>` (method, since 0.3.2): Text. (None before it is fully read. Bytes that are not UTF-8 are replaced with replacement characters)
  - returns Option<&str>: Text read. None before a request, while reading, or if it failed.

## JsonAsset (struct) (Default, Asset)

JSON asset.
- Once the bytes arrive, parses them once into serde_json::Value on first look. JSON that cannot be parsed counts as not loaded (is_loaded false, get_data None) and the text remains.

Example: Read atlas JSON from the repository and check its values

```rust
use vanilla::{Asset, Engine, EngineConfiguration, JsonAsset};

let engine = Engine::new(EngineConfiguration::new());
let mut json_asset = JsonAsset::new();
json_asset.set_no_cache(true);
json_asset.load(&engine, "examples/assets/atlas/icons.json");
assert!(json_asset.is_loaded());
let data = json_asset.get_data().expect("json data");
assert_eq!(data["meta"]["imageFileName"], "icons.png");
assert_eq!(data["images"]["coin.png"]["width"], 30);
```

Example: JSON that cannot be parsed counts as not loaded and the text remains

```rust
use vanilla::{Asset, JsonAsset};

let mut json_asset = JsonAsset::new();
json_asset.load_from_bytes("broken.json", b"{\"count\": ".to_vec());
assert!(!json_asset.is_loaded());
assert!(json_asset.get_data().is_none());
assert_eq!(json_asset.get_text(), Some("{\"count\": "));
```

- `pub fn new() -> JsonAsset` (constructor, since 0.3.2): Constructor.
  - returns JsonAsset: Empty JSON asset before a request. (cache busting off)
- `pub fn set_no_cache(&mut self, is_no_cache: bool)` (method, since 0.4.0): Sets whether to load with cache busting. (if true, adds cache: "no-cache" to web fetch, for data table JSON, etc.)
  - `is_no_cache` bool: If true, does not use the browser cache on web. (set before load, desktop always reads the file anew, which is the same as on)
- `pub fn is_no_cache(&self) -> bool` (method, since 0.4.0): Returns whether to load with cache invalidation.
  - returns bool: true if it does not use the browser cache on web. (default false)
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle` (method, since 0.3.2): Requests a load. (the original load, returns a load handle that can be awaited. As in the original, if receiving or JSON parsing fails, the await ends in failure and get_data is None)
  - `engine` &Engine: Engine to ask for the bytes.
  - `asset_path` &str: Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)
  - returns AssetLoadHandle: Load handle. (on desktop it is read on the spot and already finished. Finishes in failure if it cannot be read)
- `pub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>)` (method, since 0.3.2): Reads from bytes already in hand.
  - Discards the previously parsed data and text and parses anew. Replaces it with a new handle even if a requested handle exists.
  - `asset_path` &str: Path to record. (used in notice text when it cannot be parsed)
  - `bytes` Vec<u8>: JSON text bytes. (UTF-8)
- `pub fn unload(&mut self)` (method, since 0.3.2): Unloads.
- `pub fn get_data(&self) -> Option<&serde_json::Value>` (method, since 0.3.2): Data. (the original data. None before it is fully read or if parsing fails)
  - returns Option<&serde_json::Value>: Parsed JSON value. None before a request, while reading, or if reading or parsing failed.
- `pub fn get_text(&mut self) -> Option<&str>` (method, since 0.4.0): Text. (text of the original TextAsset. None before it is fully read, JSON that cannot be parsed still has text. Bytes that are not UTF-8 are replaced with replacement characters)
  - returns Option<&str>: JSON text read. None before a request, while reading, or if reading failed.
