pub struct HttpAssetFetch { /* private fields */ }
Expand description
HttpAssetFetch
is a struct that enables fetching assets from an HTTP endpoint.
The root URL represents the base URL to join with paths to form full asset URLs.
Implementations§
Source§impl HttpAssetFetch
impl HttpAssetFetch
Sourcepub fn new(root: &str) -> Result<Self, Box<dyn Error>>
pub fn new(root: &str) -> Result<Self, Box<dyn Error>>
Creates a new HttpAssetFetch
with a provided root URL.
§Arguments
root
: A string representing the root URL for the HTTP request (e.g., “https://example.com”).
§Returns
Ok(HttpAssetFetch)
: If the root URL is valid and successfully parsed.Err(Box<dyn Error>)
: If the URL is invalid or any error occurs while parsing.
Examples found in repository?
examples/hello_http.rs (lines 19-21)
10fn main() -> Result<(), Box<dyn Error>> {
11 /* ANCHOR: main */
12 let mut database = AssetDatabase::default()
13 .with_protocol(TextAssetProtocol)
14 .with_protocol(BytesAssetProtocol)
15 .with_protocol(BundleAssetProtocol::new("json", |bytes: Vec<u8>| {
16 Ok((serde_json::from_slice::<Value>(&bytes)?,).into())
17 }))
18 // HTTP asset fetch with root URL for assets.
19 .with_fetch(DeferredAssetFetch::new(HttpAssetFetch::new(
20 "https://raw.githubusercontent.com/PsichiX/Keket/refs/heads/master/resources/",
21 )?));
22
23 // Ensure assets exists or start getting fetched.
24 let lorem = database.ensure("text://lorem.txt")?;
25 let json = database.ensure("json://person.json")?;
26 let trash = database.ensure("bytes://trash.bin")?;
27
28 // Wait while database is busy.
29 while database.is_busy() {
30 println!("Waiting for database to be free");
31 println!(
32 "Loading:\n- Lorem Ipsum: {}\n- JSON: {}\n- Bytes: {}",
33 lorem.awaits_async_fetch(&database),
34 json.awaits_async_fetch(&database),
35 trash.awaits_async_fetch(&database)
36 );
37 database.maintain()?;
38 }
39
40 println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
41 println!("JSON: {:#}", json.access::<&Value>(&database));
42 println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
43
44 // List all assets from HTTP.
45 for (asset_path, url) in database.storage.query::<true, (&AssetPath, &Url)>() {
46 println!("Asset: `{asset_path}` at url: `{url}`");
47 }
48 /* ANCHOR_END: main */
49
50 Ok(())
51}
Trait Implementations§
Source§impl AssetFetch for HttpAssetFetch
impl AssetFetch for HttpAssetFetch
Auto Trait Implementations§
impl Freeze for HttpAssetFetch
impl RefUnwindSafe for HttpAssetFetch
impl Send for HttpAssetFetch
impl Sync for HttpAssetFetch
impl Unpin for HttpAssetFetch
impl UnwindSafe for HttpAssetFetch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more