product_farm_yaml_loader/
lib.rs

1//! Product-FARM YAML Loader
2//!
3//! A flexible YAML-based product definition loader that intelligently parses
4//! user-defined schemas into Product-FARM core types.
5//!
6//! # Features
7//!
8//! - **Flexible Parsing**: Detect YAML structure from file names and content tags
9//! - **Intelligent Interpretation**: Infer attribute types from naming conventions
10//! - **Master Schema Derivation**: Extract unified schema from multi-layer definitions
11//! - **Inference Reports**: Generate confidence-based reports of parsing decisions
12//!
13//! # Quick Start
14//!
15//! ```ignore
16//! use product_farm_yaml_loader::{init, State};
17//!
18//! // Initialize from a folder containing YAML files
19//! let mut registry = init("./products/my-product")?;
20//!
21//! // Create state with input values
22//! let mut state = State::new();
23//! state.set("scenario.difficulty", "expert");
24//! state.set("scenario.max_score", 100.0);
25//!
26//! // Evaluate a function
27//! let result = registry.evaluate("my-product-v1", state, "calculate-score")?;
28//!
29//! println!("Score: {:?}", result.outputs.get("scenario.score"));
30//! ```
31
32pub mod error;
33pub mod schema;
34pub mod discovery;
35pub mod parser;
36pub mod interpreter;
37pub mod transformer;
38pub mod registry;
39pub mod evaluator;
40pub mod report;
41pub mod output;
42pub mod llm_prompt;
43
44// Re-export farmscript from the separate crate
45pub use product_farm_farmscript as farmscript;
46
47pub use error::*;
48pub use schema::*;
49pub use registry::*;
50pub use evaluator::{State, EvalResult};
51pub use report::InferenceReport;
52
53use std::path::Path;
54
55/// Initialize a ProductRegistry from a YAML folder.
56///
57/// This function:
58/// 1. Discovers all YAML files in the folder (recursive)
59/// 2. Parses files based on names and content tags
60/// 3. Intelligently interprets field definitions
61/// 4. Transforms into core Product-FARM types
62/// 5. Validates critical requirements only
63/// 6. Pre-compiles rules for efficient evaluation
64///
65/// # Arguments
66///
67/// * `folder_path` - Path to folder containing YAML product definitions
68///
69/// # Returns
70///
71/// A `ProductRegistry` ready for evaluation, or an error if critical
72/// information is missing.
73///
74/// # Example
75///
76/// ```ignore
77/// let registry = init("./products/assessment")?;
78/// ```
79pub fn init<P: AsRef<Path>>(folder_path: P) -> LoaderResult<ProductRegistry> {
80    let path = folder_path.as_ref();
81
82    // 1. Discover YAML files
83    let files = discovery::discover_yaml_files(path)?;
84
85    // 2. Parse all files
86    let documents = parser::parse_all(&files)?;
87
88    // 3. Transform to master schema
89    let transformer = transformer::SchemaTransformer::new();
90    let schema = transformer.transform(documents)?;
91
92    // 4. Validate critical requirements only
93    transformer.validate_critical(&schema)?;
94
95    // 5. Build registry with pre-compiled rules
96    let mut registry = ProductRegistry::new();
97    registry.register(schema)?;
98    registry.compile_rules()?;
99
100    Ok(registry)
101}
102
103/// Initialize and generate an inference report.
104///
105/// Same as `init()` but also returns a detailed inference report
106/// showing what was detected and confidence levels.
107///
108/// # Returns
109///
110/// A tuple of (ProductRegistry, InferenceReport)
111pub fn init_with_report<P: AsRef<Path>>(
112    folder_path: P,
113) -> LoaderResult<(ProductRegistry, InferenceReport)> {
114    let path = folder_path.as_ref();
115
116    // 1. Discover YAML files
117    let files = discovery::discover_yaml_files(path)?;
118
119    // 2. Parse all files
120    let documents = parser::parse_all(&files)?;
121
122    // 3. Transform to master schema (with report generation)
123    let transformer = transformer::SchemaTransformer::new();
124    let (schema, report) = transformer.transform_with_report(documents)?;
125
126    // 4. Validate critical requirements only
127    transformer.validate_critical(&schema)?;
128
129    // 5. Build registry
130    let mut registry = ProductRegistry::new();
131    registry.register(schema)?;
132    registry.compile_rules()?;
133
134    Ok((registry, report))
135}
136
137/// Load a single product definition without building a registry.
138///
139/// Useful for validation, inspection, or custom processing.
140pub fn load<P: AsRef<Path>>(path: P) -> LoaderResult<MasterSchema> {
141    let path = path.as_ref();
142
143    let files = discovery::discover_yaml_files(path)?;
144    let documents = parser::parse_all(&files)?;
145
146    let transformer = transformer::SchemaTransformer::new();
147    transformer.transform(documents)
148}