[INTERN] cleaned up "dev mode" stuff (#71)

removed some specific logic i had for testing & also cleaned up this `trying.rs` file a bit
This commit is contained in:
hendrik 2026-02-04 22:32:14 +00:00
parent f49942e4c8
commit 8a854d8f90
6 changed files with 112 additions and 264 deletions

View File

@ -1,3 +1,8 @@
[env]
RUST_LOG = "debug"
RUST_BACKTRACE="1"
RUST_LOG = "info"
RUST_BACKTRACE = "1"
# Schema config
SCHEMA_DEV = "true"
SCHEMA_BASE_URL = "http://192.168.101.13:8000"
SCHEMA_EXTENSION = "yml"

View File

@ -2,3 +2,6 @@ pub mod cache;
pub mod fetcher;
pub mod icons;
pub mod read;
#[cfg(debug_assertions)]
pub mod test_data;

View File

@ -1,55 +1,26 @@
use crate::error::add_err::AnyErr;
use strum_macros::{Display, EnumIs};
const BASE_URL: &str = "http://192.168.101.13:8000";
const PROD_BASE_URL: &str = "https://businessexpress.cloud/schemas/be-portal/latest";
const SCHEMA_EXTENSIONS: [&str; 3] = ["json", "yaml", "yml"];
struct Conf {
/// Schema config loaded from compile-time environment variables
/// Set in .cargo/config.toml [env] section
struct SchemaConfig {
dev: bool,
base_url: &'static str,
use_prefix: bool,
extensions: &'static str,
extension: &'static str,
}
static LOCAL_CONFIG: Conf = Conf {
base_url: BASE_URL,
use_prefix: true,
extensions: "yml",
};
static PROD_CONFIG: Conf = Conf {
base_url: PROD_BASE_URL,
use_prefix: false,
extensions: "json",
};
fn load_schema_config() -> SchemaConfig {
let dev = option_env!("SCHEMA_DEV")
.map(|v| v == "1" || v == "true")
.unwrap_or(false);
let base_url = option_env!("SCHEMA_BASE_URL").unwrap_or("http://localhost:8000");
let extension = option_env!("SCHEMA_EXTENSION").unwrap_or("yml");
static CONF: &Conf = &LOCAL_CONFIG;
pub const SCHEMA_NAMES: [(&str, &str); 23] = [
("beas", "beas_mgmt_server.schema"),
("page", "contributes.schema"),
("page", "page.schema"),
("other", "attribute_table.schema"),
("other", "table_entity.schema"),
("workflow", "be_workflow_http_task_handler.schema"),
("workflow", "be_workflow_form.schema"),
("package", "objects/styles.schema"),
("package", "objects/dashboard_widgets.schema"),
("package", "objects/commands.schema"),
("package", "objects/customControls.schema"),
("package", "objects/beas_services.schema"),
("package", "objects/views.schema"),
("package", "objects/tables.schema"),
("package", "objects/menus.schema"),
("package", "objects/roles.schema"),
("package", "objects/entities.schema"),
("package", "objects/objects.schema"),
("package", "be_package.schema"),
("definitions", "definitions.schema"),
("definitions", "validation.schema"),
("package", "objects/patches.schema"),
("package", "objects/translations.schema"),
];
SchemaConfig {
dev,
base_url,
extension,
}
}
pub struct Reader {}
impl Reader {
@ -65,124 +36,39 @@ impl Reader {
}
}
/// Extracts stem name from input file name and maps it to one of SCHEMA_NAMES
/// Maps input to a schema URL based on config.
///
/// Handles:
/// - *.yaml
/// - *.yml
/// - *.json
/// - full URLs
/// - local paths
pub fn map_urls(name: &str) -> Result<String, AnyErr> {
// Remove extension based on SCHEMA_EXTENSIONS
let cleaned = SCHEMA_EXTENSIONS.iter().fold(name.to_string(), |acc, ext| {
acc.trim_end_matches(ext).to_string()
});
/// - If input is a full URL (http/https):
/// - If dev mode: replace PROD base with DEV base, change extension to yml
/// - If not dev: return as-is
/// - If input is a path (e.g. "page.schema" or "objects/menus.schema"):
/// - Prepend configured base_url, ensure correct extension
///
/// Handles both "/path" and "path" correctly.
pub fn map_urls(input: &str) -> Result<String, AnyErr> {
let config = load_schema_config();
let is_url = input.starts_with("http://") || input.starts_with("https://");
// Remove trailing dots
let file_path_without_ext = cleaned.trim_end_matches('.');
let file_stem = file_path_without_ext
.split('/')
.next_back()
.ok_or(AnyErr::SchemaNotFound)?;
// Find matching schema stem
let matched = SCHEMA_NAMES
.iter()
.find(|(_pref, stem)| stem.ends_with(file_stem));
if let Some((pref, stem)) = matched {
let mut url = CONF.base_url.to_string();
url.push('/');
if CONF.use_prefix {
url.push_str(pref);
url.push('/');
if is_url {
if config.dev {
// Get last path part, strip extension, rebuild with configured base/extension
let last = input.rsplit_once('/').map(|(_, p)| p).unwrap_or(input);
let stem = last
.trim_end_matches(".json")
.trim_end_matches(".yaml")
.trim_end_matches(".yml");
Ok(format!("{}/{}.{}", config.base_url, stem, config.extension))
} else {
Ok(input.to_string())
}
url.push_str(stem);
url.push('.');
url.push_str(CONF.extensions);
Ok(url)
} else {
log::error!("No schema found for {}", name);
Err(AnyErr::SchemaNotFound)
// Input is a path like "page.schema" or "objects/menus.schema"
let path = input.trim_start_matches('/');
// Remove existing extension if any
let stem = path
.trim_end_matches(".json")
.trim_end_matches(".yaml")
.trim_end_matches(".yml");
Ok(format!("{}/{}.{}", config.base_url, stem, config.extension))
}
}
pub fn get_by_type(schema: Type) -> Vec<&'static str> {
if schema.is_page() {
TEST.to_vec()
} else {
TEST_E
.iter()
.filter(|x| x.contains(&format!("{schema}").to_lowercase()))
.copied()
.collect()
}
}
#[derive(Debug, Display, EnumIs)]
pub enum Type {
Page,
Commands,
DemoControls,
CustomControls,
Entities,
Widgets,
}
impl From<&str> for Type {
fn from(s: &str) -> Self {
match s
.split('/')
.next_back()
.unwrap()
.to_lowercase()
.trim_end_matches("json")
.trim_end_matches("yaml")
.trim_end_matches("yml")
.trim_end_matches('.')
.trim_end_matches("schema")
.trim_end_matches('.')
{
"page" => Type::Page,
"commands" => Type::Commands,
"democontrols" => Type::DemoControls,
"customcontrols" => Type::CustomControls,
"entities" => Type::Entities,
"widgets" => Type::Widgets,
_ => panic!("No schema found for {s}"),
}
}
}
pub const TEST: [&str; 14] = [
"test-pages/.pages/demo/variable_events.page.yml",
"test-pages/.pages/demo/administration.page.yml",
"test-pages/.pages/demo/controls_preview.page.yml",
"test-pages/.pages/demo/entity_container.page.yml",
"test-pages/.pages/demo/kundenstamm.page.yml",
"test-pages/.pages/demo/movies.page.yml",
"test-pages/.pages/demo/responsiveness.page.yml",
"test-pages/.pages/demo/commands.page.yml",
"test-pages/.pages/demo/controls.page.yml",
"test-pages/.pages/demo/eingangsrechnung.page.yml",
"test-pages/.pages/demo/kundendetails.page.yml",
"test-pages/.pages/demo/main_entities.page.yml",
"test-pages/.pages/demo/mqtt.page.yml",
"test-pages/.pages/demo/restricted_movies.page.yml",
];
pub const TEST2: [&str; 3] = [
"test-pages/test/test0.page.yml",
"test-pages/test/test1.page.yml",
"test-pages/test/test2.page.yml",
];
pub const TEST_E: [&str; 5] = [
"test-pages/.objects/demo.commands.yaml",
"test-pages/.objects/demoConrols.entities.yaml",
"test-pages/.objects/demo.customControls.yml",
"test-pages/.objects/movies.entities.yaml",
"test-pages/.objects/widgets.widgets.yml",
];

16
src/data/test_data.rs Normal file
View File

@ -0,0 +1,16 @@
// For develoopment and testing purposes onlyi
pub fn read_from_file<I: AsRef<str>>(path: I) -> String {
super::read::Reader::from_file(path.as_ref()).unwrap()
}
pub fn read_tst_file(idx: usize) -> String {
read_from_file(SOME_SLICE_WITH_TEST_DATA[idx])
}
const SOME_SLICE_WITH_TEST_DATA: [&str; 4] = [
"test-pages/test/test0.page.yml",
"test-pages/test/test1.page.yml",
"test-pages/test/test2.page.yml",
"test-pages/test/test3.page.yml",
];

View File

@ -7,6 +7,7 @@ pub mod orchestrator;
pub mod parser;
pub mod schema;
#[cfg(debug_assertions)]
pub mod trying;
#[cfg(target_arch = "wasm32")]
pub mod wasm;

View File

@ -1,14 +1,12 @@
#![allow(dead_code, unused_variables)]
use std::ops::Range;
use std::time::Instant;
use yaml_rust2::{Yaml, YamlLoader};
use crate::data::fetcher;
use crate::data::read::{get_by_type, TEST_E};
use crate::data::test_data::read_tst_file;
use crate::{
data::read::{map_urls, Reader, TEST2},
data::read::{map_urls, Reader},
input::yaml_wrapper::YamlWrapper,
misc::index_to_col_row,
orchestrator::{get_info_for_line_and_col_for_yaml, get_validation_result},
schema::ctx::Context,
};
@ -34,10 +32,10 @@ use crate::{
// .collect::<Vec<_>>()
//}
fn validate_file(content: &str) {
fn validate_file(content: &str, schema: &str) {
let storage = Context::new();
let rs = get_validation_result(content, "entities.schema", |x| x);
println!("{:#?}", rs);
let rs = get_validation_result(content, schema, |x| x);
println!("{rs:#?}");
match rs {
Ok(error) => {
for (st, en, err) in error {
@ -57,119 +55,64 @@ fn validate_file(content: &str) {
}
}
fn validate_all_test_files(schema: &str) {
let now = Instant::now();
for (i, f) in get_by_type(schema.into()).iter().enumerate() {
println!("Validating: {f:?}");
let content = Reader::from_file(f).unwrap();
match get_validation_result(&content, schema, |x| x) {
Ok(error) => {
for (st, en, err) in error {
println!(
"Pos: ({:?}..{:?}) / ({:?}) = ({:?}: Error: {:#?}",
st,
en,
index_to_col_row(&content, st),
&content[st..en],
err
);
}
}
Err(e) => {
println!("Error: {e:?}");
}
}
eprintln!("{i:02} - Time: {:?}", now.elapsed());
}
eprintln!("Time: {:?}", now.elapsed());
}
fn full_info(content: &str) {
fn full_info(content: &str, schema: &str) {
let storage = Context::new();
let wrapper = YamlWrapper::try_from(content).unwrap();
println!("{wrapper:#?}");
let full_info = wrapper.generate_full_info("page.schema", &storage).unwrap();
let full_info = wrapper.generate_full_info(schema, &storage).unwrap();
println!("{full_info:#?}");
}
fn read_both(path: &str) -> (String, Vec<u8>) {
eprintln!("Reading file: {}", path);
(
Reader::from_file(path).unwrap(),
Reader::from_file_u8(path).unwrap(),
)
}
fn read_test2_file(i: usize) -> String {
Reader::from_file(TEST2[i]).unwrap()
}
fn read_test2_file_u8(i: usize) -> Vec<u8> {
Reader::from_file_u8(TEST2[i]).unwrap()
}
fn get_info_for_path_line_col(content: &str, line: usize, col: usize) {
fn get_info_for_path_line_col(content: &str, schema: &str, line: usize, col: usize) {
let now = Instant::now();
let test_yaml = YamlWrapper::try_from(content).unwrap();
eprintln!("Time: {:?} - parsed input as yaml", now.elapsed());
let schema = map_urls("page.schema.yml").unwrap();
let schema = map_urls(schema).unwrap();
let context = Context::new();
let l = get_info_for_line_and_col_for_yaml(&test_yaml, &schema, line, col, &context).unwrap();
println!("{l:#?}");
eprintln!("Time: {:?} - generated info", now.elapsed());
}
fn get_full_info_timed(content: &str) {
let now = Instant::now();
full_info(content);
eprintln!("Time: {:?} - generated full info", now.elapsed());
fn calc_path_for_ranges(raw: &str, left: Range<usize>, right: Range<usize>) {
let test_yaml = YamlWrapper::try_from(raw).unwrap();
let r = left
.flat_map(|i| right.clone().map(move |j| (i, j)))
.map(|(i, j)| (i + 1, j, test_yaml.get_path_for_line_col(i, j)))
.collect::<Vec<_>>();
println!(
"paths: {:#?}",
r.iter()
.map(|(i, j, e)| format!("{i}, {j}: {:?}", e))
.collect::<Vec<_>>()
);
}
pub fn validate_test_file(test_file: &str) {
validate_file(test_file);
fn calculate_paths(raw: &str) {
let test_yaml = YamlWrapper::try_from(raw).unwrap();
for i in 1..10 {
for j in 1..20 {
println!("doing for line: {i}, col: {j}");
let path = test_yaml.get_path_for_line_col(i, j);
println!("Path for line {i}, col {j}: {path:#?}");
}
}
}
pub fn do_sth() {
let now = Instant::now();
let (raw, raw_u8) = read_both(TEST_E[3]);
let raw = read_tst_file(3);
ext_info_first_paths(&raw);
if false {
let (line, col) = (7, 13);
let (line, col) = (318, 12);
//let sth = &raw_u8[46283..46300];
//println!("{:?}", std::str::from_utf8(sth).unwrap());
//get_full_info_timed(&raw);
//get_info_for_path_line_col(&raw, line, col);
ext_info(&raw, line, col);
let test_yaml = YamlWrapper::try_from(raw.as_str()).unwrap();
for i in 1..10 {
for j in 1..20 {
println!("doing for line: {i}, col: {j}");
let path = test_yaml.get_path_for_line_col(i, j);
println!("Path for line {i}, col {j}: {path:#?}");
}
}
// validate_all_test_files("page.schema");
//validate_file(&raw);
ext_info_first_paths(&raw);
//let wr = YamlWrapper::try_from(raw.as_str()).unwrap();
//wr.compare_with_src();
}
validate_test_file(&raw);
eprintln!("Full time: {:?}", now.elapsed());
}
fn ext_info(content: &str, line: usize, col: usize) {
fn ext_info(content: &str, schema: &str, line: usize, col: usize) {
let now = Instant::now();
let test_yaml = YamlWrapper::try_from(content).unwrap();
eprintln!("Time: {:?} - parsed input as yaml", now.elapsed());
let schema = map_urls("entities.schema.yml").unwrap();
let schema = map_urls(schema).unwrap();
let context = Context::new();
let path = test_yaml.get_path_for_line_col(line, col).unwrap();
@ -177,10 +120,6 @@ fn ext_info(content: &str, line: usize, col: usize) {
let (l, r) = test_yaml
.generate_element_suggestion_info_for_path(&schema, &context, path)
.unwrap();
println!("left {l:#?}");
println!("right {r:#?}");
eprintln!("Time: {:?} - generated info", now.elapsed());
}
fn ext_info_first_paths(content: &str) {
@ -188,7 +127,7 @@ fn ext_info_first_paths(content: &str) {
let test_yaml = YamlWrapper::try_from(content).unwrap();
eprintln!("Time: {:?} - parsed input as yaml", now.elapsed());
let schema = map_urls("entities.schema.yml").expect("???");
let schema = map_urls("page.schema.yml").expect("???");
let context = Context::new();
let mut count = 0;
for (i, path) in test_yaml.get_all_path().into_iter().enumerate() {
@ -201,12 +140,10 @@ fn ext_info_first_paths(content: &str) {
.generate_info_for_path(&schema, &context, path.clone())
.unwrap();
count += l.len() + r.len();
println!("{path:#?} - l: {:?}, r: {:?}", l.len(), r.len());
println!("{i:02}: left {l:#?}");
println!("{i:02}: right {r:#?}");
println!("{i:02}: info {info:#?}");
println!("{path:#?} - l: {:?}, r: {:?}", l.len(), r.len(),);
eprintln!("{i:02}: Time: {:?} - generated info", now.elapsed());
}
eprintln!("count: {:?}", count);
eprintln!("count: {count:?} ");
eprintln!("Time: {:?} - generated info", now.elapsed());
}