Struct xtask_wasm::DevServer

source ·
#[non_exhaustive]
pub struct DevServer { pub ip: IpAddr, pub port: u16, pub watch: Watch, pub command: Option<Command>, pub not_found_path: Option<PathBuf>, }
Expand description

A simple HTTP server useful during development.

It can watch the source code for changes and restart a provided command.

Get the files at watch_path and serve them at a given IP address (127.0.0.1:8000 by default). An optional command can be provided to restart the build when changes are detected.

§Usage

use std::process;
use xtask_wasm::{
    anyhow::Result,
    clap,
    default_dist_dir,
};

#[derive(clap::Parser)]
enum Opt {
    Start(xtask_wasm::DevServer),
    Dist,
}

fn main() -> Result<()> {
    let opt: Opt = clap::Parser::parse();

    match opt {
        Opt::Start(mut dev_server) => {
            log::info!("Starting the development server...");
            dev_server.arg("dist").start(default_dist_dir(false))?;
        }
        Opt::Dist => todo!("build project"),
    }

    Ok(())
}

Add a start subcommand that will run cargo xtask dist, watching for changes in the workspace and serve the files in the default dist directory (target/debug/dist for non-release) at a given IP address.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§ip: IpAddr

IP address to bind. Default to 127.0.0.1.

§port: u16

Port number. Default to 8000.

§watch: Watch

Watch object for detecting changes.

§Note

Used only if command is set.

§command: Option<Command>

Command executed when a change is detected.

§not_found_path: Option<PathBuf>

Use another file path when the URL is not found.

Implementations§

source§

impl DevServer

source

pub fn address(self, ip: IpAddr, port: u16) -> Self

Set the dev-server binding address.

source

pub fn command(self, command: Command) -> Self

Set the command that is executed when a change is detected.

source

pub fn arg<S: AsRef<OsStr>>(self, arg: S) -> Self

Adds an argument to pass to the command executed when changes are detected.

This will use the xtask command by default.

source

pub fn args<I, S>(self, args: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Adds multiple arguments to pass to the command executed when changes are detected.

This will use the xtask command by default.

source

pub fn not_found(self, path: impl Into<PathBuf>) -> Self

Use another file path when the URL is not found.

source

pub fn start(self, served_path: impl AsRef<Path>) -> Result<()>

Start the server, serving the files at served_path.

crate::default_dist_dir should be used to get the dist directory that needs to be served.

Trait Implementations§

source§

impl Args for DevServer

source§

fn group_id() -> Option<Id>

Report the [ArgGroup::id][crate::ArgGroup::id] for this set of arguments
source§

fn augment_args<'b>(__clap_app: Command) -> Command

Append to [Command] so it can instantiate Self. Read more
source§

fn augment_args_for_update<'b>(__clap_app: Command) -> Command

Append to [Command] so it can update self. Read more
source§

impl CommandFactory for DevServer

source§

fn command<'b>() -> Command

Build a [Command] that can instantiate Self. Read more
source§

fn command_for_update<'b>() -> Command

Build a [Command] that can update self. Read more
source§

impl Debug for DevServer

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for DevServer

source§

fn default() -> DevServer

Returns the “default value” for a type. Read more
source§

impl FromArgMatches for DevServer

source§

fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from [ArgMatches], parsing the arguments as needed. Read more
source§

fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches ) -> Result<Self, Error>

Instantiate Self from [ArgMatches], parsing the arguments as needed. Read more
source§

fn update_from_arg_matches( &mut self, __clap_arg_matches: &ArgMatches ) -> Result<(), Error>

Assign values from ArgMatches to self.
source§

fn update_from_arg_matches_mut( &mut self, __clap_arg_matches: &mut ArgMatches ) -> Result<(), Error>

Assign values from ArgMatches to self.
source§

impl Parser for DevServer

§

fn parse() -> Self

Parse from std::env::args_os(), [exit][Error::exit] on error.
§

fn try_parse() -> Result<Self, Error>

Parse from std::env::args_os(), return Err on error.
§

fn parse_from<I, T>(itr: I) -> Self
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, [exit][Error::exit] on error.
§

fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, return Err on error.
§

fn update_from<I, T>(&mut self, itr: I)
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, [exit][Error::exit] on error.
§

fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, return Err on error.

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
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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>,

§

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.