commit 99c8aa8e70cab6b94c37bbca0767a7e8ee2e2d75 Author: vato007 Date: Thu Apr 17 07:44:47 2025 +0930 Initial commit diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml new file mode 100644 index 0000000..e891410 --- /dev/null +++ b/.gitea/workflows/test.yaml @@ -0,0 +1,14 @@ +name: test + +on: push + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + lfs: true + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + - run: cargo test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf3e918 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/test_output \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c7c0c45 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,395 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + +[[package]] +name = "clap" +version = "4.5.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2df961d8c8a0d08aa9945718ccf584145eee3f3aa06cddbeac12933781102e04" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "132dbda40fb6753878316a489d5a1242a8ef2f0d9e47ba01c951ea8aa7d013a5" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "jiff" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ad87c89110f55e4cd4dc2893a9790820206729eaf221555f742d540b0724a0" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d076d5b64a7e2fe6f0743f02c43ca4a6725c0f904203bfe276a5b3e793103605" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quick-xml" +version = "0.37.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ce8c88de324ff838700f36fb6ab86c96df0e3c4ab6ef3a9b2044465cce1369" +dependencies = [ + "memchr", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "xsd-to-dts" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "env_logger", + "log", + "quick-xml", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7374923 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "xsd-to-dts" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.98" +clap = { version = "4.5.36", features = ["derive"] } +env_logger = "0.11.8" +log = "0.4.27" +quick-xml = "0.37.4" diff --git a/README.md b/README.md new file mode 100644 index 0000000..53ffea0 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +XSD to DTS + +Ingests and XML schema (.xsd) and outputs a typescript definition file (.d.ts), with types in the java format generated by the xjc tool. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4ebcb01 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,303 @@ +use std::{ + fs::File, + io::{BufWriter, Write}, + path::{Path, PathBuf}, +}; + +use anyhow::Result; +use log::{debug, info}; +use quick_xml::{Reader, events::Event}; + +pub fn xsd_to_dts(xsd: &PathBuf, output: &PathBuf) -> Result<()> { + let mut reader = Reader::from_file(xsd)?; + let mut buf = Vec::new(); + + if let Some(parent_dir) = output.parent() { + std::fs::create_dir_all(parent_dir)?; + } + let output_file = File::create(output)?; + let mut writer = BufWriter::new(output_file); + + let mut in_class = false; + let mut in_nested_class = false; + let mut other_classes = vec![]; + let mut in_sequence = false; + loop { + match reader.read_event_into(&mut buf).unwrap() { + Event::Eof => break, + Event::Start(start) => { + match start.name().as_ref() { + b"xs:sequence" => { + in_sequence = true; + } + b"xs:element" => { + if let Some(type_attr) = start.try_get_attribute("type")? { + let mut ts_type = std::str::from_utf8(&type_attr.value)?; + if ts_type.starts_with("xs:") { + ts_type = &ts_type[3..]; + } + if ts_type.eq_ignore_ascii_case("datetime") { + ts_type = "XMLGregorianCalendar"; + } else if ts_type.eq_ignore_ascii_case("int") + || ts_type.eq_ignore_ascii_case("double") + { + ts_type = "number"; + } + let mut ts_type_final = ts_type.to_owned(); + if !ts_type.eq("string") { + ts_type_final = capitalize(ts_type); + } + if let Some(occurs_attr) = start.try_get_attribute("maxOccurs")? { + if std::str::from_utf8(&occurs_attr.value)? == "unbounded" { + ts_type_final = format!("List<{}>", ts_type_final); + } + } + if let Some(name_attr) = start.try_get_attribute("name")? { + if in_class && !in_nested_class { + writer.write( + format!( + "\tget{}(): {};\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + ts_type_final + ) + .as_bytes(), + )?; + writer.write( + format!( + "\tset{}({}: {}): void;\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + std::str::from_utf8(&name_attr.value)?, + ts_type_final + ) + .as_bytes(), + )?; + } else if in_nested_class { + let class_index = other_classes.len() - 1; + other_classes[class_index] = format!( + "{}\n\n\tget{}(): {};\n\n", + other_classes[other_classes.len() - 1], + capitalize(std::str::from_utf8(&name_attr.value)?), + ts_type_final + ); + other_classes[class_index] = format!( + "{}\tset{}({}: {}): void;\n\n", + other_classes[other_classes.len() - 1], + capitalize(std::str::from_utf8(&name_attr.value)?), + std::str::from_utf8(&name_attr.value)?, + ts_type_final + ); + } + } + } else { + // Got a new type, if we're not in a class we should start + // writing the class, otherwise leave it until the end + if let Some(name_attr) = start.try_get_attribute("name")? { + if !in_class { + in_class = true; + writer.write( + format!( + "declare class {} {{\n", + capitalize(std::str::from_utf8(&name_attr.value)?) + ) + .as_bytes(), + )?; + } else { + in_nested_class = true; + other_classes.push(format!( + "declare class {} {{\n", + capitalize(std::str::from_utf8(&name_attr.value)?) + )); + } + } + } + } + b"xs:complexType" => { + if let Some(name_attr) = start.try_get_attribute("name")? { + in_class = true; + writer.write( + format!( + "declare class {} {{\n", + capitalize(std::str::from_utf8(&name_attr.value)?) + ) + .as_bytes(), + )?; + } + } + b"xs:attribute" => { + if let Some(type_attr) = start.try_get_attribute("type")? { + let mut ts_type = std::str::from_utf8(&type_attr.value)?; + if ts_type.starts_with("xs:") { + ts_type = &ts_type[3..]; + } + if ts_type.eq_ignore_ascii_case("datetime") { + ts_type = "XMLGregorianCalendar"; + } else if ts_type.eq_ignore_ascii_case("int") + || ts_type.eq_ignore_ascii_case("double") + { + ts_type = "number"; + } + + if in_class { + if let Some(name_attr) = start.try_get_attribute("name")? { + writer.write( + format!( + "\tget{}(): {};\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + ts_type + ) + .as_bytes(), + )?; + writer.write( + format!( + "\tset{}({}: {}): void;\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + std::str::from_utf8(&name_attr.value)?, + ts_type + ) + .as_bytes(), + )?; + } + } + } + } + name => { + info!( + "Found unknown element type: {}", + String::from_utf8_lossy(name) + ); + } + } + debug!("{:?}", start); + } + Event::End(end) => { + match end.name().as_ref() { + b"xs:sequence" => { + in_sequence = false; + } + b"xs:element" => { + if in_sequence { + continue; + } + if in_nested_class { + in_nested_class = false; + let class_index = other_classes.len() - 1; + other_classes[class_index] = + format!("{}\n}}", other_classes[other_classes.len() - 1]); + } else if in_class && !in_nested_class { + in_class = false; + writer.write("}\n".as_bytes())?; + } + } + b"xs:complexType" => { + if in_class && !in_nested_class { + in_class = false; + writer.write("}\n".as_bytes())?; + } + } + name => { + info!( + "Found unknown element type: {}", + String::from_utf8_lossy(name) + ); + } + } + debug!("{:?}", end); + } + Event::Empty(empty) => { + if let Some(type_attr) = empty.try_get_attribute("type")? { + let mut ts_type = std::str::from_utf8(&type_attr.value)?; + if ts_type.starts_with("xs:") { + ts_type = &ts_type[3..]; + } + if ts_type.eq_ignore_ascii_case("datetime") { + ts_type = "XMLGregorianCalendar"; + } else if ts_type.eq_ignore_ascii_case("int") + || ts_type.eq_ignore_ascii_case("double") + { + ts_type = "number"; + } + let mut ts_type_final = ts_type.to_owned(); + if !ts_type.eq("string") { + ts_type_final = capitalize(ts_type); + } + if let Some(occurs_attr) = empty.try_get_attribute("maxOccurs")? { + if std::str::from_utf8(&occurs_attr.value)? == "unbounded" { + ts_type_final = format!("List<{}>", ts_type_final); + } + } + if let Some(name_attr) = empty.try_get_attribute("name")? { + if in_class && !in_nested_class { + writer.write( + format!( + "\tget{}(): {};\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + ts_type_final + ) + .as_bytes(), + )?; + writer.write( + format!( + "\tset{}({}: {}): void;\n\n", + capitalize(std::str::from_utf8(&name_attr.value)?), + std::str::from_utf8(&name_attr.value)?, + ts_type_final + ) + .as_bytes(), + )?; + } else if in_nested_class { + let class_index = other_classes.len() - 1; + other_classes[class_index] = format!( + "{}\tget{}(): {};\n\n", + other_classes[other_classes.len() - 1], + capitalize(std::str::from_utf8(&name_attr.value)?), + ts_type_final + ); + other_classes[class_index] = format!( + "{}\tset{}({}: {}): void;\n\n", + other_classes[other_classes.len() - 1], + capitalize(std::str::from_utf8(&name_attr.value)?), + std::str::from_utf8(&name_attr.value)?, + ts_type_final + ); + } + } + } + } + name => { + debug!("Got Something: {:?}", name); + } + } + } + + for other_class in other_classes { + writer.write(format!("\n\n{}", other_class).as_bytes())?; + } + + Ok(()) +} + +/// Capitalizes the first character in s. +pub fn capitalize(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + None => String::new(), + Some(f) => f.to_uppercase().collect::() + c.as_str(), + } +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use anyhow::Result; + + use crate::xsd_to_dts; + + #[test] + fn ppm2xsd() -> Result<()> { + xsd_to_dts( + &PathBuf::from("test_resources/test.xsd"), + &PathBuf::from("test_output/test.d.ts"), + ) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..57745e3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,25 @@ +use std::path::PathBuf; + +use anyhow::Result; +use clap::Parser; +use xsd_to_dts::xsd_to_dts; + +#[derive(Parser)] +#[command(name = "xsd-to-dts")] +#[command(author = "Pivato M. ")] +#[command(version = "0.0.1")] +#[command(about = "Convert an XML Schema (.xsd) file to a .d.ts file using java naming conventions", long_about = None)] +struct Cli { + #[arg(short, long, value_name = "FILE")] + input_xsd: PathBuf, + + #[arg(short, long, value_name = "FILE")] + output_dts: PathBuf, +} + +fn main() -> Result<()> { + env_logger::init(); + + let cli = Cli::parse(); + xsd_to_dts(&cli.input_xsd, &cli.output_dts) +} diff --git a/test_resources/test.xsd b/test_resources/test.xsd new file mode 100644 index 0000000..4973d4d --- /dev/null +++ b/test_resources/test.xsd @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +