Merge pull request #2457 from Skgland/library

simplify generated libraries map by using `include_str!`
This commit is contained in:
Mark Thom
2024-07-26 11:29:12 -06:00
committed by GitHub
5 changed files with 91 additions and 74 deletions

View File

@@ -23,6 +23,7 @@ hostname = ["dep:hostname"]
tls = ["dep:native-tls"] tls = ["dep:native-tls"]
http = ["dep:warp", "dep:reqwest"] http = ["dep:warp", "dep:reqwest"]
crypto-full = [] crypto-full = []
"rust-version-1.80" = []
[build-dependencies] [build-dependencies]
indexmap = "1.0.2" indexmap = "1.0.2"

View File

@@ -5,23 +5,18 @@ use instructions_template::generate_instructions_rs;
use static_string_indexing::index_static_strings; use static_string_indexing::index_static_strings;
use std::env; use std::env;
use std::fs;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
fn find_prolog_files( fn find_prolog_files(path_prefix: &str, current_dir: &Path) -> Vec<(String, PathBuf)> {
libraries: &mut File, let mut libraries = vec![];
path_prefix: &str,
const_prefix: &str,
current_dir: &Path,
) -> Vec<(String, String)> {
let mut constants = vec![];
let entries = match current_dir.read_dir() { let entries = match current_dir.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(_) => return constants, Err(_) => return libraries,
}; };
for entry in entries.filter_map(Result::ok).map(|e| e.path()) { 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() { if let Some(file_name) = entry.file_name() {
let file_name = file_name.to_str().unwrap(); let file_name = file_name.to_str().unwrap();
let new_path_prefix = format!("{path_prefix}{file_name}/"); let new_path_prefix = format!("{path_prefix}{file_name}/");
let new_const_prefix = format!("{const_prefix}_{}", file_name.to_uppercase()); let new_libs = find_prolog_files(&new_path_prefix, &entry);
let new_consts = libraries.extend(new_libs);
find_prolog_files(libraries, &new_path_prefix, &new_const_prefix, &entry);
constants.extend(new_consts);
} }
} else if entry.is_file() { } else if entry.is_file() {
let ext = std::ffi::OsStr::new("pl"); let ext = std::ffi::OsStr::new("pl");
if entry.extension() == Some(ext) { 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 name = entry.file_stem().unwrap().to_str().unwrap();
let lib_name = format!("{path_prefix}{name}"); let lib_name = format!("{path_prefix}{name}");
let const_name = format!("{const_prefix}_{}", name.to_uppercase());
writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap(); libraries.push((lib_name, entry));
constants.push((lib_name, const_name));
} }
} }
} }
constants libraries
} }
fn main() { fn main() {
@@ -67,44 +56,40 @@ fn main() {
let dest_path = Path::new(&out_dir).join("libraries.rs"); let dest_path = Path::new(&out_dir).join("libraries.rs");
let mut libraries = File::create(dest_path).unwrap(); let mut libraries = File::create(dest_path).unwrap();
let lib_path = Path::new("src/lib"); let lib_path = Path::new("src").join("lib");
writeln!( let constants = find_prolog_files("", &lib_path);
libraries,
"\
use indexmap::IndexMap;\
"
)
.unwrap();
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!( let prefix: PathBuf = if let Ok(diff) = out_dir_path.strip_prefix(manifest_dir_path) {
libraries, let mut path = PathBuf::from(".");
"\ for comp in diff.components() {
std::thread_local!{{ match comp {
static LIBRARIES: IndexMap<&'static str, &'static str> = {{ std::path::Component::Normal(_) => path.push(".."),
let mut m = IndexMap::new();" std::path::Component::CurDir => (),
) std::path::Component::Prefix(_)
.unwrap(); | std::path::Component::RootDir
| std::path::Component::ParentDir => {
for (name, constant) in constants { path = manifest_dir_path.to_path_buf();
writeln!( break;
libraries,
"\
m.insert(\"{name}\",{constant});"
)
.unwrap();
} }
}
}
path
} else {
manifest_dir_path.to_path_buf()
};
writeln!( writeln!(libraries, "{{").unwrap();
libraries, for (name, lib_path) in constants {
" let path: PathBuf = prefix.join(lib_path);
m writeln!(libraries, "m.insert(\"{name}\", include_str!({path:?}));").unwrap();
}}; }
}}" writeln!(libraries, "}}").unwrap();
)
.unwrap();
let instructions_path = Path::new(&out_dir).join("instructions.rs"); let instructions_path = Path::new(&out_dir).join("instructions.rs");
let mut instructions_file = File::create(&instructions_path).unwrap(); let mut instructions_file = File::create(&instructions_path).unwrap();

View File

@@ -1,4 +1,4 @@
use bytes::{Bytes, buf::Reader}; use bytes::{buf::Reader, Bytes};
use std::sync::{Arc, Condvar, Mutex}; use std::sync::{Arc, Condvar, Mutex};
use warp::http; use warp::http;

View File

@@ -118,25 +118,50 @@ fn current_dir() -> PathBuf {
} }
} }
#[cfg(not(feature = "rust-version-1.80"))]
mod libraries { mod libraries {
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")); include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
m
})
}
pub(crate) fn contains(name: &str) -> bool { 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> { pub(crate) fn get(name: &str) -> Option<&'static str> {
LIBRARIES.with(|libs| libs.get(name).copied()) libraries().get(name).copied()
}
} }
#[cfg(test)] #[cfg(feature = "rust-version-1.80")]
std::thread_local! { mod libraries {
#[allow(dead_code)] use indexmap::IndexMap;
static LIBRARIES2 : IndexMap<&'static str, &'static str> = { use std::sync::LazyLock;
static LIBRARIES: LazyLock<IndexMap<&'static str, &'static str>> = LazyLock::new(|| {
let mut m = IndexMap::new(); let mut m = IndexMap::new();
m.insert("test", "test2");
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
m m
}; });
pub(crate) fn contains(name: &str) -> bool {
LIBRARIES.contains_key(name)
}
pub(crate) fn get(name: &str) -> Option<&'static str> {
LIBRARIES.get(name).copied()
} }
} }

View File

@@ -12,9 +12,10 @@ use crate::machine::machine_indices::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
use crate::types::*; use crate::types::*;
use bytes::Buf;
pub use scryer_modular_bitfield::prelude::*; pub use scryer_modular_bitfield::prelude::*;
#[cfg(feature = "http")]
use bytes::{buf::Reader as BufReader, Buf, Bytes};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
@@ -22,8 +23,6 @@ use std::fmt::Debug;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::hash::Hash; use std::hash::Hash;
use std::io; use std::io;
#[cfg(feature = "http")]
use bytes::{buf::Reader as BufReader, Bytes};
use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write}; use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
use std::net::{Shutdown, TcpStream}; use std::net::{Shutdown, TcpStream};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@@ -1136,8 +1135,15 @@ impl Stream {
} }
} }
} }
#[cfg(feature = "http")]
Stream::HttpRead(stream_layout) => { Stream::HttpRead(stream_layout) => {
if stream_layout.stream.get_ref().body_reader.get_ref().has_remaining() { if stream_layout
.stream
.get_ref()
.body_reader
.get_ref()
.has_remaining()
{
AtEndOfStream::Not AtEndOfStream::Not
} else { } else {
AtEndOfStream::Past AtEndOfStream::Past