Skip to main content

Assets

Struct Assets 

Source
pub struct Assets { /* private fields */ }
Expand description

A surface’s asset directory, with what has been read from it: the Cockpit’s share/cockpit/ under /cockpit/assets/, or another surface’s beside it (ADR 0135).

Implementations§

Source§

impl Assets

Source

pub fn new(share_dir: &Path) -> Self

The assets under share_dir/cockpit, when that directory exists. A distribution without one serves no assets and every page still renders: the stylesheet link and the module scripts simply resolve to 404, and the Cockpit degrades to unstyled server-rendered HTML rather than failing.

Source

pub fn under(share_dir: &Path, dir: &'static str, prefix: &'static str) -> Self

The assets under share_dir/<dir>, served under prefix: one surface’s files, read and digested exactly as the Cockpit’s are. A surface does not get a second file server; it gets this one pointed at its own directory.

use majordomus_cli::cockpit::assets::Assets;
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("ui")).unwrap();
std::fs::write(dir.path().join("ui/ui.css"), "body{}").unwrap();
let ui = Assets::under(dir.path(), "ui", "/ui/assets/");
assert!(ui.url("ui.css").starts_with("/ui/assets/ui.css?v="));
assert!(!Assets::new(dir.path()).present(), "the Cockpit's directory is another one");
Source

pub fn none() -> Self

No asset directory at all: for a router built without a distribution.

Source

pub fn none_under(dir: &'static str, prefix: &'static str) -> Self

No asset directory, for a surface whose files would live under dir and prefix: nothing is servable, and every URL keeps its plain path under that surface’s prefix, so a missing file is a 404 that names where it was looked for.

use majordomus_cli::cockpit::assets::Assets;
let ui = Assets::none_under("ui", "/ui/assets/");
assert!(!ui.present());
assert!(!ui.has("ui.css"));
assert_eq!(ui.url("ui.css"), "/ui/assets/ui.css");
Source

pub fn present(&self) -> bool

Is there an asset directory?

Source

pub fn has(&self, name: &str) -> bool

Is this file there and servable?

Source

pub fn url(&self, name: &str) -> String

The URL a page emits for an asset: the path with the digest of its bytes, so the URL changes exactly when the file does. An asset that is not there keeps its plain path, so the 404 names the file a reader should look for.

Source

pub fn respond(&self, name: &str, req: &Request) -> Response

Answer a request for prefix + name. Immutable only when the request’s v is exactly the version the URL a page emits carries; anything else (no v, an empty one, a prefix, a stale one) revalidates. An If-None-Match naming the current ETag is answered 304 Not Modified with no body: the 200’s validator, caching and length, and nothing else crosses the wire.

Trait Implementations§

Source§

impl Debug for Assets

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more