0.8.0-beta cargo doc crate_name bug fix

This commit is contained in:
Tim-Paik 2021-08-23 12:48:35 +08:00
parent 2624d5d6fe
commit 1ae50ebca8
3 changed files with 56 additions and 19 deletions

12
Cargo.lock generated
View File

@ -2142,6 +2142,15 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.26" version = "0.1.26"
@ -2463,7 +2472,7 @@ checksum = "acdb075a845574a1fa5f09fd77e43f7747599301ea3417a9fbffdeedfc1f4a29"
[[package]] [[package]]
name = "web" name = "web"
version = "0.7.2-beta" version = "0.8.0-beta"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-http", "actix-http",
@ -2480,6 +2489,7 @@ dependencies = [
"serde", "serde",
"sha2", "sha2",
"tera", "tera",
"toml",
] ]
[[package]] [[package]]

View File

@ -3,7 +3,7 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
description = "simple http server written in rust" description = "simple http server written in rust"
edition = "2018" edition = "2018"
name = "web" name = "web"
version = "0.7.2-beta" version = "0.8.0-beta"
[dependencies] [dependencies]
actix-files = "0.5" actix-files = "0.5"
@ -21,6 +21,7 @@ regex = "1.5"
serde = "1" serde = "1"
sha2 = "0.9" sha2 = "0.9"
tera = "1" tera = "1"
toml = "0.5"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -14,7 +14,7 @@ use sha2::Digest;
use std::{ use std::{
env::{set_var, var}, env::{set_var, var},
fs::read_dir, fs::read_dir,
io::{Error, ErrorKind, Write}, io::{Error, ErrorKind, Read, Write},
net::IpAddr, net::IpAddr,
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
@ -151,6 +151,16 @@ fn get_file_type(from: &Path) -> String {
.to_string() .to_string()
} }
#[derive(serde::Deserialize)]
struct Package {
name: String,
}
#[derive(serde::Deserialize)]
struct CargoToml {
package: Package,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, serde::Serialize)] #[derive(Eq, Ord, PartialEq, PartialOrd, serde::Serialize)]
struct Dir { struct Dir {
name: String, name: String,
@ -473,11 +483,7 @@ async fn main() -> std::io::Result<()> {
} else { } else {
"http://".to_string() "http://".to_string()
}, },
if ip == "0.0.0.0" { if ip == "0.0.0.0" { "127.0.0.1" } else { &ip },
"127.0.0.1"
} else {
ip.as_str()
},
matches.value_of("port").unwrap_or("8000").to_string() matches.value_of("port").unwrap_or("8000").to_string()
); );
@ -528,9 +534,9 @@ async fn main() -> std::io::Result<()> {
let process_time: Vec<&str> = data[3].splitn(2, ".").collect(); let process_time: Vec<&str> = data[3].splitn(2, ".").collect();
let process_time = process_time[0].to_string() + "ms"; let process_time = process_time[0].to_string() + "ms";
let process_time = blue.value(if process_time.len() == 3 { let process_time = blue.value(if process_time.len() == 3 {
" ".to_string() + process_time.as_str() " ".to_string() + &process_time
} else if process_time.len() == 4 { } else if process_time.len() == 4 {
" ".to_string() + process_time.as_str() " ".to_string() + &process_time
} else { } else {
process_time process_time
}); });
@ -559,7 +565,7 @@ async fn main() -> std::io::Result<()> {
.map_or("", |m| m.as_str()); .map_or("", |m| m.as_str());
let data = format!( let data = format!(
"[INFO] Serving {} on {}", "[INFO] Serving {} on {}",
var("ROOT").unwrap_or(".".to_string()).as_str(), var("ROOT").unwrap_or(".".to_string()),
addr addr
); );
return writeln!(buf, "\r{}", green.value(data)); return writeln!(buf, "\r{}", green.value(data));
@ -576,6 +582,29 @@ async fn main() -> std::io::Result<()> {
let addr = if let Some(matches) = matches.subcommand_matches("doc") { let addr = if let Some(matches) = matches.subcommand_matches("doc") {
info!("[INFO] Generating document (may take a while)"); info!("[INFO] Generating document (may take a while)");
let mut cargo_toml = match std::fs::File::open("./Cargo.toml") {
Ok(file) => file,
Err(e) => {
error!("[ERROR] {}", e.to_string());
return Ok(());
}
};
let mut contents = String::new();
match cargo_toml.read_to_string(&mut contents) {
Ok(_) => {}
Err(e) => {
error!("[ERROR] {}", e.to_string());
return Ok(());
}
}
let contents: CargoToml = match toml::from_str(&contents) {
Ok(t) => t,
Err(e) => {
error!("[ERROR] {}", e.to_string());
return Ok(());
}
};
let crate_name = contents.package.name;
match std::process::Command::new("cargo").arg("doc").output() { match std::process::Command::new("cargo").arg("doc").output() {
Ok(output) => { Ok(output) => {
let output = std::str::from_utf8(&output.stderr).unwrap_or(""); let output = std::str::from_utf8(&output.stderr).unwrap_or("");
@ -587,6 +616,7 @@ async fn main() -> std::io::Result<()> {
"[ERROR] {}", "[ERROR] {}",
output.strip_prefix("error: ").unwrap_or(output) output.strip_prefix("error: ").unwrap_or(output)
); );
return Ok(());
} }
} }
Err(e) => { Err(e) => {
@ -596,7 +626,7 @@ async fn main() -> std::io::Result<()> {
} }
let path = Path::new("./target/doc/"); let path = Path::new("./target/doc/");
let mut index_path = path.to_path_buf(); let mut index_path = path.to_path_buf();
index_path.push(crate_name!().to_string() + "/index.html"); index_path.push(crate_name.to_string() + "/index.html");
if !index_path.exists() || !index_path.is_file() { if !index_path.exists() || !index_path.is_file() {
error!("[ERROR] Cargo Error: doc path not found"); error!("[ERROR] Cargo Error: doc path not found");
return Ok(()); return Ok(());
@ -613,13 +643,9 @@ async fn main() -> std::io::Result<()> {
); );
let url = format!( let url = format!(
"http://{}:{}/{}/index.html", "http://{}:{}/{}/index.html",
if ip == "0.0.0.0" { if ip == "0.0.0.0" { "127.0.0.1" } else { &ip },
"127.0.0.1"
} else {
ip.as_str()
},
matches.value_of("port").unwrap_or("8000").to_string(), matches.value_of("port").unwrap_or("8000").to_string(),
crate_name!(), crate_name,
); );
if !matches.is_present("noopen") { if !matches.is_present("noopen") {
open_in_browser(&url); open_in_browser(&url);
@ -667,7 +693,7 @@ async fn main() -> std::io::Result<()> {
} }
if var("ENABLE_CORS").unwrap_or("false".to_string()) == "true" { if var("ENABLE_CORS").unwrap_or("false".to_string()) == "true" {
let cors = var("CORS").unwrap_or("*".to_string()); let cors = var("CORS").unwrap_or("*".to_string());
let cors = http::HeaderValue::from_str(cors.as_str()) let cors = http::HeaderValue::from_str(&cors)
.unwrap_or(http::HeaderValue::from_static("*")); .unwrap_or(http::HeaderValue::from_static("*"));
head.headers_mut() head.headers_mut()
.insert(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, cors); .insert(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, cors);