Fix compiler errors

This commit is contained in:
Aster committed 2022-02-09 16:11:02 +08:00
1 parent b6c6dd6532
commit ce628e1197
12 files changed
+97 -60

No files matched your search

+2
View File
@@ -31,6 +31,8 @@ tokio = { version = "1.16.1", features = ["full"] }
atty = "0.2.14" atty = "0.2.14"
regex = "1.5.4" regex = "1.5.4"
chrono = "0.4.19" chrono = "0.4.19"
anyhow = "1.0.53"
hyper = "0.14.17"
axum = { version = "0.4.5", features = ["ws", "headers"] } axum = { version = "0.4.5", features = ["ws", "headers"] }
tower-http = { version = "0.2.2", features = ["fs", "trace"] } tower-http = { version = "0.2.2", features = ["fs", "trace"] }
+4 -5
View File
@@ -1,16 +1,15 @@
use super::*; use super::*;
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
#[structopt(name = "build")] #[clap(name = "build")]
pub struct Build { pub struct Build {
#[structopt(flatten)] #[clap(flatten)]
pub build: ConfigOptsBuild, pub build: ConfigOptsBuild,
} }
impl Build { impl Build {
pub fn build(self) -> anyhow::Result<()> { pub fn build(self) -> Result<()> {
let mut crate_config = crate::CrateConfig::new()?; let mut crate_config = crate::CrateConfig::new()?;
// change the release state. // change the release state.
@@ -28,7 +27,7 @@ impl Build {
crate::builder::build_desktop(&crate_config)?; crate::builder::build_desktop(&crate_config)?;
} }
_ => { _ => {
return Err(anyhow::anyhow!("Unsoppurt platform target.")); return custom_error!("Unsoppurt platform target.");
} }
} }
+8 -8
View File
@@ -4,40 +4,40 @@ use super::*;
#[derive(Clone, Debug, Default, Deserialize, Parser)] #[derive(Clone, Debug, Default, Deserialize, Parser)]
pub struct ConfigOptsBuild { pub struct ConfigOptsBuild {
/// The index HTML file to drive the bundling process [default: index.html] /// The index HTML file to drive the bundling process [default: index.html]
#[structopt(parse(from_os_str))] #[clap(parse(from_os_str))]
pub target: Option<PathBuf>, pub target: Option<PathBuf>,
/// Build in release mode [default: false] /// Build in release mode [default: false]
#[structopt(long)] #[clap(long)]
#[serde(default)] #[serde(default)]
pub release: bool, pub release: bool,
/// Build a example [default: ""] /// Build a example [default: ""]
#[structopt(long)] #[clap(long)]
pub example: Option<String>, pub example: Option<String>,
/// Build platform: support Web & Desktop [default: "web"] /// Build platform: support Web & Desktop [default: "web"]
#[structopt(long, default_value = "web")] #[clap(long, default_value = "web")]
pub platform: String, pub platform: String,
} }
#[derive(Clone, Debug, Default, Deserialize, Parser)] #[derive(Clone, Debug, Default, Deserialize, Parser)]
pub struct ConfigOptsServe { pub struct ConfigOptsServe {
/// The index HTML file to drive the bundling process [default: index.html] /// The index HTML file to drive the bundling process [default: index.html]
#[structopt(parse(from_os_str))] #[clap(parse(from_os_str))]
pub target: Option<PathBuf>, pub target: Option<PathBuf>,
/// Build a example [default: ""] /// Build a example [default: ""]
#[structopt(long)] #[clap(long)]
pub example: Option<String>, pub example: Option<String>,
/// Build in release mode [default: false] /// Build in release mode [default: false]
#[structopt(long)] #[clap(long)]
#[serde(default)] #[serde(default)]
pub release: bool, pub release: bool,
/// Build platform: support Web & Desktop [default: "web"] /// Build platform: support Web & Desktop [default: "web"]
#[structopt(long, default_value = "web")] #[clap(long, default_value = "web")]
pub platform: String, pub platform: String,
} }
+2 -2
View File
@@ -2,11 +2,11 @@ use super::*;
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
#[structopt(name = "clean")] #[clap(name = "clean")]
pub struct Clean {} pub struct Clean {}
impl Clean { impl Clean {
pub fn clean(self) -> anyhow::Result<()> { pub fn clean(self) -> Result<()> {
let crate_config = crate::CrateConfig::new()?; let crate_config = crate::CrateConfig::new()?;
let output = Command::new("cargo") let output = Command::new("cargo")
+5 -4
View File
@@ -1,8 +1,9 @@
use super::*; use super::*;
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Deserialize, Parser)] #[derive(Clone, Debug, Deserialize, Subcommand)]
#[structopt(name = "config")] #[clap(name = "config")]
pub enum Config { pub enum Config {
/// Init `Dioxus.toml` for project/folder. /// Init `Dioxus.toml` for project/folder.
Init { Init {
@@ -10,14 +11,14 @@ pub enum Config {
name: String, name: String,
/// Cover old config /// Cover old config
#[structopt(long)] #[clap(long)]
#[serde(default)] #[serde(default)]
force: bool, force: bool,
}, },
} }
impl Config { impl Config {
pub fn config(self) -> anyhow::Result<()> { pub fn config(self) -> Result<()> {
let crate_root = crate::cargo::crate_root()?; let crate_root = crate::cargo::crate_root()?;
match self { match self {
Config::Init { name, force } => { Config::Init { name, force } => {
+8 -12
View File
@@ -1,15 +1,16 @@
use super::*; use super::*;
use crate::custom_error;
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Default, Deserialize, StructOpt)] #[derive(Clone, Debug, Default, Deserialize, Parser)]
#[structopt(name = "create")] #[clap(name = "create")]
pub struct Create { pub struct Create {
/// Init project name /// Init project name
#[structopt(default_value = ".")] #[clap(default_value = ".")]
name: String, name: String,
/// Template path /// Template path
#[structopt(default_value = "gh:dioxuslabs/dioxus-template", long)] #[clap(default_value = "gh:dioxuslabs/dioxus-template", long)]
template: String, template: String,
} }
@@ -23,10 +24,7 @@ impl Create {
let project_path = PathBuf::from(&self.name); let project_path = PathBuf::from(&self.name);
if project_path.join("Dioxus.toml").is_file() || project_path.join("Cargo.toml").is_file() { if project_path.join("Dioxus.toml").is_file() || project_path.join("Cargo.toml").is_file() {
return Err(Error::Other(anyhow::anyhow!( return custom_error!("🧨 Folder '{}' is initialized.", &self.name);
"🧨 Folder '{}' is initialized.",
&self.name
)));
} }
log::info!("🔧 Start to create a new project '{}'.", self.name); log::info!("🔧 Start to create a new project '{}'.", self.name);
@@ -47,9 +45,7 @@ impl Create {
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.output()?; .output()?;
if !install_output.status.success() { if !install_output.status.success() {
return Err(Error::Other(anyhow::anyhow!( return custom_error!("Try to install cargo-generate failed.");
"Try to install cargo-generate failed."
)));
} }
} }
@@ -63,7 +59,7 @@ impl Create {
.output()?; .output()?;
if !generate_output.status.success() { if !generate_output.status.success() {
return Err(Error::Other(anyhow::anyhow!("Generate project failed."))); return custom_error!("Generate project failed.");
} }
let mut dioxus_file = File::open(project_path.join("Dioxus.toml"))?; let mut dioxus_file = File::open(project_path.join("Dioxus.toml"))?;
+17 -16
View File
@@ -6,31 +6,31 @@ pub mod create;
pub mod serve; pub mod serve;
pub mod translate; pub mod translate;
use std::{io::Write, path::PathBuf}; use crate::custom_error;
use crate::{cfg::ConfigOptsBuild, gen_page}; use crate::{cfg::ConfigOptsBuild, gen_page};
use clap::{Parser}; use crate::{cfg::ConfigOptsServe, server, CrateConfig};
use crate::{error::Result, Error};
use clap::Parser;
use html_parser::Dom;
use html_parser::Element;
use html_parser::Node;
use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io::Read;
use std::process::exit;
use std::{ use std::{
fs::remove_dir_all, fs::remove_dir_all,
process::{Command, Stdio}, process::{Command, Stdio},
}; };
use std::{fs::File}; use std::{io::Write, path::PathBuf};
use std::{ use clap::{Subcommand};
io::{Read},
};
use regex::Regex;
use crate::{error::Result, Error};
use crate::{cfg::ConfigOptsServe, server, CrateConfig};
use html_parser::Dom;
use html_parser::Element;
use html_parser::Node;
use std::fmt::{Display, Formatter};
use std::process::exit;
/// Build, bundle, & ship your Dioxus app. /// Build, bundle, & ship your Dioxus app.
/// ///
/// ///
#[derive(Parser, Debug)] #[derive(Parser)]
#[clap(name = "dioxus")] #[clap(name = "dioxus")]
pub struct Cli { pub struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
@@ -42,7 +42,7 @@ pub struct Cli {
// //
// // note: dioxus is still roughly compatible with trunk // // note: dioxus is still roughly compatible with trunk
// /// Path to the Trunk config file [default: Trunk.toml] // /// Path to the Trunk config file [default: Trunk.toml]
// #[structopt(long, parse(from_os_str), env = "TRUNK_CONFIG")] // #[clap(long, parse(from_os_str), env = "TRUNK_CONFIG")]
// pub config: Option<PathBuf>, // pub config: Option<PathBuf>,
} }
@@ -59,5 +59,6 @@ pub enum Commands {
/// Clean output artifacts. /// Clean output artifacts.
Clean(clean::Clean), Clean(clean::Clean),
/// Dioxus config file controls. /// Dioxus config file controls.
#[clap(subcommand)]
Config(config::Config), Config(config::Config),
} }
+5 -5
View File
@@ -2,14 +2,14 @@ use super::*;
/// Run the WASM project on dev-server /// Run the WASM project on dev-server
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
#[structopt(name = "serve")] #[clap(name = "serve")]
pub struct Serve { pub struct Serve {
#[structopt(flatten)] #[clap(flatten)]
pub serve: ConfigOptsServe, pub serve: ConfigOptsServe,
} }
impl Serve { impl Serve {
pub async fn serve(self) -> anyhow::Result<()> { pub async fn serve(self) -> Result<()> {
let mut crate_config = crate::CrateConfig::new()?; let mut crate_config = crate::CrateConfig::new()?;
// change the relase state. // change the relase state.
@@ -49,7 +49,7 @@ impl Serve {
return Ok(()); return Ok(());
} }
_ => { _ => {
return Err(anyhow::anyhow!("Unsoppurt platform target.")); return custom_error!("Unsoppurt platform target.");
} }
} }
@@ -62,7 +62,7 @@ impl Serve {
Ok(()) Ok(())
} }
pub fn regen_dev_page(crate_config: &CrateConfig) -> anyhow::Result<()> { pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> {
let serve_html = gen_page(&crate_config.dioxus_config, true); let serve_html = gen_page(&crate_config.dioxus_config, true);
let mut file = std::fs::File::create( let mut file = std::fs::File::create(
+5 -5
View File
@@ -2,24 +2,24 @@ use super::*;
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
#[structopt(name = "translate")] #[clap(name = "translate")]
pub struct Translate { pub struct Translate {
/// Activate debug mode /// Activate debug mode
// short and long flags (-d, --debug) will be deduced from the field's name // short and long flags (-d, --debug) will be deduced from the field's name
#[structopt(short, long)] #[clap(short, long)]
pub component: bool, pub component: bool,
/// Input file /// Input file
#[structopt(short, long)] #[clap(short, long)]
pub file: Option<String>, pub file: Option<String>,
/// Output file, stdout if not present /// Output file, stdout if not present
#[structopt(parse(from_os_str))] #[clap(parse(from_os_str))]
pub output: Option<PathBuf>, pub output: Option<PathBuf>,
} }
impl Translate { impl Translate {
pub fn translate(self) -> anyhow::Result<()> { pub fn translate(self) -> Result<()> {
let Translate { let Translate {
component, component,
output, output,
+37
View File
@@ -11,6 +11,15 @@ pub enum Error {
#[error("I/O Error: {0}")] #[error("I/O Error: {0}")]
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
#[error("Format Error: {0}")]
FormatError(#[from] std::fmt::Error),
#[error("Format failed: {0}")]
ParseError(String),
#[error("Runtime Error: {0}")]
RuntimeError(String),
#[error("Failed to write error")] #[error("Failed to write error")]
FailedToWrite, FailedToWrite,
@@ -20,6 +29,9 @@ pub enum Error {
#[error("Failed to write error")] #[error("Failed to write error")]
CargoError(String), CargoError(String),
#[error("{0}")]
CustomError(String),
#[error(transparent)] #[error(transparent)]
Other(#[from] anyhow::Error), Other(#[from] anyhow::Error),
} }
@@ -35,3 +47,28 @@ impl From<String> for Error {
Error::Unique(s) Error::Unique(s)
} }
} }
impl From<html_parser::Error> for Error {
fn from(e: html_parser::Error) -> Self {
Self::ParseError(e.to_string())
}
}
impl From<hyper::Error> for Error {
fn from(e: hyper::Error) -> Self {
Self::RuntimeError(e.to_string())
}
}
#[macro_export]
macro_rules! custom_error {
($msg:literal $(,)?) => {
Err(Error::CustomError($msg.to_string()))
};
($err:expr $(,)?) => {
Err(Error::from($err))
};
($fmt:expr, $($arg:tt)*) => {
Err(Error::CustomError(format!($fmt, $($arg)*)))
};
}
+2 -2
View File
@@ -1,9 +1,9 @@
use clap::Parser;
use dioxus_cli::*; use dioxus_cli::*;
use structopt::StructOpt;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let args = Cli::from_args(); let args = Cli::parse();
set_up_logging(); set_up_logging();
match args.action { match args.action {
+2 -1
View File
@@ -10,6 +10,7 @@ use notify::{RecommendedWatcher, Watcher};
use std::{path::PathBuf, sync::Arc}; use std::{path::PathBuf, sync::Arc};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use crate::Result;
use crate::{builder, serve::Serve, CrateConfig}; use crate::{builder, serve::Serve, CrateConfig};
use tokio::sync::broadcast; use tokio::sync::broadcast;
@@ -17,7 +18,7 @@ struct WsRelodState {
update: broadcast::Sender<String>, update: broadcast::Sender<String>,
} }
pub async fn startup(config: CrateConfig) -> anyhow::Result<()> { pub async fn startup(config: CrateConfig) -> Result<()> {
log::info!("🚀 Starting development server..."); log::info!("🚀 Starting development server...");
let dist_path = config.out_dir.clone(); let dist_path = config.out_dir.clone();