Skip to main content

sdsforge_core/
lib.rs

1//! LLM-based bidirectional conversion between Safety Data Sheet (SDS) documents and
2//! the Japanese MHLW SDS Data Exchange Format v1.0 (JIS Z 7253 / GHS).
3//!
4//! # Quick start
5//!
6//! ```no_run
7//! use sdsforge_core::{
8//!     AnthropicBackend, LlmConfig,
9//!     convert_to_json, convert_to_json_with_report, ConvertConfig, Language,
10//! };
11//!
12//! #[tokio::main]
13//! async fn main() -> anyhow::Result<()> {
14//!     let backend = AnthropicBackend::new(
15//!         std::env::var("ANTHROPIC_API_KEY")?,
16//!         LlmConfig::default(),
17//!     );
18//!     let config = ConvertConfig {
19//!         source_language: Some(Language::Japanese),
20//!         output_language: Language::Japanese,
21//!         ..Default::default()
22//!     };
23//!     // `convert_to_json_with_report` returns structured metadata (language, sections, notes).
24//!     let (sds, report) =
25//!         convert_to_json_with_report(std::path::Path::new("input.pdf"), &backend, &config).await?;
26//!     for w in &report.warnings { eprintln!("WARN: {w}"); }
27//!     eprintln!("Populated sections: {:?}", report.populated_sections);
28//!     eprintln!("Standardization notes: {:?}", report.standardization_notes);
29//!     std::fs::write("output.json", serde_json::to_string_pretty(&sds)?)?;
30//!     std::fs::write("output_report.json", serde_json::to_string_pretty(&report)?)?;
31//!     Ok(())
32//! }
33//! ```
34//!
35//! # Features
36//!
37//! - **SDS → JSON**: PDF/DOCX/XLSX/TXT → MHLW standard JSON via LLM (parallel extraction,
38//!   automatic retry, JSON repair).
39//! - **JSON → DOCX**: Generates a JIS Z 7253-compliant Word document with localized headings.
40//! - **Multilingual**: `ja` / `en` / `zh-CN` / `zh-TW` source documents and output headings.
41//! - **Pluggable LLM**: Ships with [`AnthropicBackend`] and [`OpenAiCompatBackend`].
42//!   Implement [`converter::LlmBackend`] to bring your own.
43
44pub mod assist;
45pub mod converter;
46pub mod country;
47pub mod enrichment;
48pub mod error;
49pub mod generation;
50pub mod ghs_codes;
51pub mod language;
52pub mod normalize;
53pub mod schema;
54
55pub use assist::{
56    build_proposals, excerpt_verifies, is_allowed_path, parse_candidates_json, run_section4_assist,
57    sha256_hex, validate_candidate, AssistCandidate, AssistProposal, AssistRun, ASSIST_CONFIDENCE,
58    ASSIST_SCHEMA_VERSION, EXTRACTION_METHOD_LLM, SECTION4_ALLOWED_PATHS,
59};
60pub use converter::{
61    AnthropicBackend, AnyBackend, ConvertConfig, ConversionReport, LlmBackend, LlmConfig,
62    OpenAiCompatBackend, build_any_backend,
63    convert_bytes_to_json, convert_bytes_to_json_with_report,
64    convert_from_json, convert_from_template,
65    convert_pdf_to_json_vision, convert_to_json, convert_to_json_with_report,
66    convert_url_to_json,
67    extract_sds_from_pdf_vision, fill_template, openai_compat_url,
68    prune_empty_fields,
69};
70pub use converter::extractor::{
71    detect_format_str, detect_language_from_file, detect_language_from_url,
72    extract_text, extract_text_from_url, extract_text_limited,
73};
74pub use converter::validator::{validate, validate_typed, Finding};
75pub use enrichment::{
76    enrich_composition, lookup_cas, lookup_cas_detailed, CasInfo, CasResolution, CasWarning,
77    ChemicalIdentityCandidate,
78};
79pub use error::SdsError;
80pub use generation::{
81    build_generation_artifacts, build_generation_report, build_product_level_unresolved,
82    compute_evidence_summary, compute_release_status, draft_sections_from_resolved_input,
83    evaluate_release_gate, generate_from_detailed_lookups, generate_from_normalized_input,
84    generate_from_resolved_input, generate_section_1_and_3, generate_with_detailed_enrichment,
85    generate_with_enrichment,
86    render_review_report, serialize_generation_report, serialize_official_sds,
87    validate_product_input, ComponentInput,
88    ConcentrationRange, ConfidenceLevel, EvidenceApplicability, EvidenceLevel, EvidenceSource,
89    EvidenceSummary, ExplosiveLimitsEvidence, FieldPolicy, FieldProvenance, FieldStatus,
90    GenerationArtifactError, GenerationArtifacts, GenerationReport, GenerationResult,
91    MeasuredPropertiesInput, MeasuredValueEvidence, MeasurementConditions,
92    NotApplicableReason, ProductInput, RegulatoryImpact, ReleaseGateResult, ReleaseStatus,
93    REPORT_SCHEMA_VERSION, RequiredInput, SafetyImpact, SectionDraftResult, SupplierInput,
94    TestResultEvidence, UnresolvedField, UnresolvedReason,
95};
96pub use ghs_codes::{h_code_description, is_valid_h_code, is_valid_p_code, p_code_description};
97pub use country::SourceCountry;
98pub use language::{detect_language, Language};
99pub use normalize::{
100    CalculatedIdentityProperties, ChemicalNormalizationResult, ChemicalNormalizer,
101    NormalizationIssue, NormalizationStatus, UnavailableNormalizer,
102};
103#[cfg(feature = "chematic-normalization")]
104pub use normalize::ChematicNormalizer;
105pub use schema::SdsRoot;