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

View File

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

View File

@ -14,7 +14,7 @@ use sha2::Digest;
use std::{
env::{set_var, var},
fs::read_dir,
io::{Error, ErrorKind, Write},
io::{Error, ErrorKind, Read, Write},
net::IpAddr,
path::{Path, PathBuf},
str::FromStr,
@ -151,6 +151,16 @@ fn get_file_type(from: &Path) -> String {
.to_string()
}
#[derive(serde::Deserialize)]
struct Package {
name: String,
}
#[derive(serde::Deserialize)]
struct CargoToml {
package: Package,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, serde::Serialize)]
struct Dir {
name: String,
@ -473,11 +483,7 @@ async fn main() -> std::io::Result<()> {
} else {
"http://".to_string()
},
if ip == "0.0.0.0" {
"127.0.0.1"
} else {
ip.as_str()
},
if ip == "0.0.0.0" { "127.0.0.1" } else { &ip },
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 = process_time[0].to_string() + "ms";
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 {
" ".to_string() + process_time.as_str()
" ".to_string() + &process_time
} else {
process_time
});
@ -559,7 +565,7 @@ async fn main() -> std::io::Result<()> {
.map_or("", |m| m.as_str());
let data = format!(
"[INFO] Serving {} on {}",
var("ROOT").unwrap_or(".".to_string()).as_str(),
var("ROOT").unwrap_or(".".to_string()),
addr
);
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") {
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() {
Ok(output) => {
let output = std::str::from_utf8(&output.stderr).unwrap_or("");
@ -587,6 +616,7 @@ async fn main() -> std::io::Result<()> {
"[ERROR] {}",
output.strip_prefix("error: ").unwrap_or(output)
);
return Ok(());
}
}
Err(e) => {
@ -596,7 +626,7 @@ async fn main() -> std::io::Result<()> {
}
let path = Path::new("./target/doc/");
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() {
error!("[ERROR] Cargo Error: doc path not found");
return Ok(());
@ -613,13 +643,9 @@ async fn main() -> std::io::Result<()> {
);
let url = format!(
"http://{}:{}/{}/index.html",
if ip == "0.0.0.0" {
"127.0.0.1"
} else {
ip.as_str()
},
if ip == "0.0.0.0" { "127.0.0.1" } else { &ip },
matches.value_of("port").unwrap_or("8000").to_string(),
crate_name!(),
crate_name,
);
if !matches.is_present("noopen") {
open_in_browser(&url);
@ -667,7 +693,7 @@ async fn main() -> std::io::Result<()> {
}
if var("ENABLE_CORS").unwrap_or("false".to_string()) == "true" {
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("*"));
head.headers_mut()
.insert(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, cors);