Merge pull request #2457 from Skgland/library
simplify generated libraries map by using `include_str!`
This commit is contained in:
@@ -23,6 +23,7 @@ hostname = ["dep:hostname"]
|
||||
tls = ["dep:native-tls"]
|
||||
http = ["dep:warp", "dep:reqwest"]
|
||||
crypto-full = []
|
||||
"rust-version-1.80" = []
|
||||
|
||||
[build-dependencies]
|
||||
indexmap = "1.0.2"
|
||||
|
||||
@@ -5,23 +5,18 @@ use instructions_template::generate_instructions_rs;
|
||||
use static_string_indexing::index_static_strings;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
fn find_prolog_files(
|
||||
libraries: &mut File,
|
||||
path_prefix: &str,
|
||||
const_prefix: &str,
|
||||
current_dir: &Path,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut constants = vec![];
|
||||
fn find_prolog_files(path_prefix: &str, current_dir: &Path) -> Vec<(String, PathBuf)> {
|
||||
let mut libraries = vec![];
|
||||
|
||||
let entries = match current_dir.read_dir() {
|
||||
Ok(entries) => entries,
|
||||
Err(_) => return constants,
|
||||
Err(_) => return libraries,
|
||||
};
|
||||
|
||||
for entry in entries.filter_map(Result::ok).map(|e| e.path()) {
|
||||
@@ -29,27 +24,21 @@ fn find_prolog_files(
|
||||
if let Some(file_name) = entry.file_name() {
|
||||
let file_name = file_name.to_str().unwrap();
|
||||
let new_path_prefix = format!("{path_prefix}{file_name}/");
|
||||
let new_const_prefix = format!("{const_prefix}_{}", file_name.to_uppercase());
|
||||
let new_consts =
|
||||
find_prolog_files(libraries, &new_path_prefix, &new_const_prefix, &entry);
|
||||
constants.extend(new_consts);
|
||||
let new_libs = find_prolog_files(&new_path_prefix, &entry);
|
||||
libraries.extend(new_libs);
|
||||
}
|
||||
} else if entry.is_file() {
|
||||
let ext = std::ffi::OsStr::new("pl");
|
||||
if entry.extension() == Some(ext) {
|
||||
let contain = String::from_utf8(fs::read(&entry).unwrap()).unwrap();
|
||||
let name = entry.file_stem().unwrap().to_str().unwrap();
|
||||
let lib_name = format!("{path_prefix}{name}");
|
||||
let const_name = format!("{const_prefix}_{}", name.to_uppercase());
|
||||
|
||||
writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap();
|
||||
|
||||
constants.push((lib_name, const_name));
|
||||
libraries.push((lib_name, entry));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constants
|
||||
libraries
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -67,44 +56,40 @@ fn main() {
|
||||
let dest_path = Path::new(&out_dir).join("libraries.rs");
|
||||
|
||||
let mut libraries = File::create(dest_path).unwrap();
|
||||
let lib_path = Path::new("src/lib");
|
||||
let lib_path = Path::new("src").join("lib");
|
||||
|
||||
writeln!(
|
||||
libraries,
|
||||
"\
|
||||
use indexmap::IndexMap;\
|
||||
"
|
||||
)
|
||||
.unwrap();
|
||||
let constants = find_prolog_files("", &lib_path);
|
||||
|
||||
let constants = find_prolog_files(&mut libraries, "", "LIB", lib_path);
|
||||
let out_dir = std::env::var("OUT_DIR").unwrap();
|
||||
let out_dir_path: &Path = out_dir.as_ref();
|
||||
let manifest_dir = &std::env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let manifest_dir_path: &Path = manifest_dir.as_ref();
|
||||
|
||||
writeln!(
|
||||
libraries,
|
||||
"\
|
||||
std::thread_local!{{
|
||||
static LIBRARIES: IndexMap<&'static str, &'static str> = {{
|
||||
let mut m = IndexMap::new();"
|
||||
)
|
||||
.unwrap();
|
||||
let prefix: PathBuf = if let Ok(diff) = out_dir_path.strip_prefix(manifest_dir_path) {
|
||||
let mut path = PathBuf::from(".");
|
||||
for comp in diff.components() {
|
||||
match comp {
|
||||
std::path::Component::Normal(_) => path.push(".."),
|
||||
std::path::Component::CurDir => (),
|
||||
std::path::Component::Prefix(_)
|
||||
| std::path::Component::RootDir
|
||||
| std::path::Component::ParentDir => {
|
||||
path = manifest_dir_path.to_path_buf();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
path
|
||||
} else {
|
||||
manifest_dir_path.to_path_buf()
|
||||
};
|
||||
|
||||
for (name, constant) in constants {
|
||||
writeln!(
|
||||
libraries,
|
||||
"\
|
||||
m.insert(\"{name}\",{constant});"
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(libraries, "{{").unwrap();
|
||||
for (name, lib_path) in constants {
|
||||
let path: PathBuf = prefix.join(lib_path);
|
||||
writeln!(libraries, "m.insert(\"{name}\", include_str!({path:?}));").unwrap();
|
||||
}
|
||||
|
||||
writeln!(
|
||||
libraries,
|
||||
"
|
||||
m
|
||||
}};
|
||||
}}"
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(libraries, "}}").unwrap();
|
||||
|
||||
let instructions_path = Path::new(&out_dir).join("instructions.rs");
|
||||
let mut instructions_file = File::create(&instructions_path).unwrap();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use bytes::{Bytes, buf::Reader};
|
||||
use bytes::{buf::Reader, Bytes};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
|
||||
use warp::http;
|
||||
|
||||
@@ -118,25 +118,50 @@ fn current_dir() -> PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "rust-version-1.80"))]
|
||||
mod libraries {
|
||||
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
fn libraries() -> &'static IndexMap<&'static str, &'static str> {
|
||||
static LIBRARIES: OnceLock<IndexMap<&'static str, &'static str>> = OnceLock::new();
|
||||
LIBRARIES.get_or_init(|| {
|
||||
let mut m = IndexMap::new();
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
|
||||
|
||||
m
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn contains(name: &str) -> bool {
|
||||
LIBRARIES.with(|libs| libs.contains_key(name))
|
||||
libraries().contains_key(name)
|
||||
}
|
||||
|
||||
pub(crate) fn get(name: &str) -> Option<&'static str> {
|
||||
LIBRARIES.with(|libs| libs.get(name).copied())
|
||||
libraries().get(name).copied()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rust-version-1.80")]
|
||||
mod libraries {
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static LIBRARIES: LazyLock<IndexMap<&'static str, &'static str>> = LazyLock::new(|| {
|
||||
let mut m = IndexMap::new();
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
|
||||
|
||||
m
|
||||
});
|
||||
|
||||
pub(crate) fn contains(name: &str) -> bool {
|
||||
LIBRARIES.contains_key(name)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
std::thread_local! {
|
||||
#[allow(dead_code)]
|
||||
static LIBRARIES2 : IndexMap<&'static str, &'static str> = {
|
||||
let mut m = IndexMap::new();
|
||||
m.insert("test", "test2");
|
||||
m
|
||||
};
|
||||
pub(crate) fn get(name: &str) -> Option<&'static str> {
|
||||
LIBRARIES.get(name).copied()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@ use crate::machine::machine_indices::*;
|
||||
use crate::machine::machine_state::*;
|
||||
use crate::types::*;
|
||||
|
||||
use bytes::Buf;
|
||||
pub use scryer_modular_bitfield::prelude::*;
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
use bytes::{buf::Reader as BufReader, Buf, Bytes};
|
||||
use std::cmp::Ordering;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
@@ -22,8 +23,6 @@ use std::fmt::Debug;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::hash::Hash;
|
||||
use std::io;
|
||||
#[cfg(feature = "http")]
|
||||
use bytes::{buf::Reader as BufReader, Bytes};
|
||||
use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
|
||||
use std::net::{Shutdown, TcpStream};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
@@ -1136,13 +1135,20 @@ impl Stream {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stream::HttpRead(stream_layout) => {
|
||||
if stream_layout.stream.get_ref().body_reader.get_ref().has_remaining() {
|
||||
AtEndOfStream::Not
|
||||
} else {
|
||||
AtEndOfStream::Past
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "http")]
|
||||
Stream::HttpRead(stream_layout) => {
|
||||
if stream_layout
|
||||
.stream
|
||||
.get_ref()
|
||||
.body_reader
|
||||
.get_ref()
|
||||
.has_remaining()
|
||||
{
|
||||
AtEndOfStream::Not
|
||||
} else {
|
||||
AtEndOfStream::Past
|
||||
}
|
||||
}
|
||||
_ => AtEndOfStream::Not,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user