diff options
author | rtkay123 <dev@kanjala.com> | 2025-08-08 08:01:13 +0200 |
---|---|---|
committer | rtkay123 <dev@kanjala.com> | 2025-08-08 08:01:13 +0200 |
commit | fd81550cd9b49c138f384dd7c097729d66f1e0b0 (patch) | |
tree | 8566e9647715c9a5af09a3d7a6bace77f0c86f71 | |
parent | 68a4edcdf7af5e8c5eef04b9b65683229644f29a (diff) | |
download | warden-fd81550cd9b49c138f384dd7c097729d66f1e0b0.tar.bz2 warden-fd81550cd9b49c138f384dd7c097729d66f1e0b0.zip |
build(proto): compile time iso20022 types
-rw-r--r-- | crates/warden/Cargo.toml | 2 | ||||
-rw-r--r-- | lib/warden-core/Cargo.toml | 3 | ||||
-rw-r--r-- | lib/warden-core/build.rs | 70 | ||||
-rw-r--r-- | lib/warden-core/src/google.rs | 8 | ||||
-rw-r--r-- | lib/warden-core/src/iso20022.rs | 32 | ||||
-rw-r--r-- | lib/warden-core/src/lib.rs | 27 | ||||
-rw-r--r-- | proto/iso20022/pacs_002_001_12.proto (renamed from proto/iso20022/pacs.002.001.12.proto) | 0 | ||||
-rw-r--r-- | proto/iso20022/pacs_008_001_12.proto (renamed from proto/iso20022/pacs.008.001.12.proto) | 0 |
8 files changed, 123 insertions, 19 deletions
diff --git a/crates/warden/Cargo.toml b/crates/warden/Cargo.toml index b6441d5..475e593 100644 --- a/crates/warden/Cargo.toml +++ b/crates/warden/Cargo.toml @@ -8,4 +8,4 @@ documentation.workspace = true description.workspace = true [dependencies] -warden-core.workspace = true +warden-core = { workspace = true, features = ["iso20022"] } diff --git a/lib/warden-core/Cargo.toml b/lib/warden-core/Cargo.toml index efb61a3..dbe61fd 100644 --- a/lib/warden-core/Cargo.toml +++ b/lib/warden-core/Cargo.toml @@ -17,7 +17,8 @@ tonic = { workspace = true, optional = true } tonic-types = { version = "0.14.0", optional = true } [features] -default = ["dep:prost", "dep:tonic", "dep:tonic-types"] +default = [] +iso20022 = ["dep:prost", "dep:tonic", "dep:tonic-types"] [build-dependencies] tonic-prost-build = { version = "0.14.0", features = ["cleanup-markdown"] } diff --git a/lib/warden-core/build.rs b/lib/warden-core/build.rs index 024e9bb..be4f8f2 100644 --- a/lib/warden-core/build.rs +++ b/lib/warden-core/build.rs @@ -1,11 +1,71 @@ +#[cfg(feature = "iso20022")] +enum Entity { + #[cfg(feature = "iso20022")] + ISO2022, +} + +#[cfg(feature = "iso20022")] +impl Entity { + fn protos(&self) -> Vec<&'static str> { + let mut res: Vec<&'static str> = vec![]; + + #[cfg(feature = "iso20022")] + fn iso20022_protos() -> Vec<&'static str> { + vec![ + "proto/iso20022/pacs_008_001_12.proto", + "proto/iso20022/pacs_002_001_12.proto", + ] + } + + match self { + #[cfg(feature = "iso20022")] + Entity::ISO2022 => { + res.extend(iso20022_protos()); + } + } + res + } +} + fn main() -> Result<(), Box<dyn std::error::Error>> { - tonic_prost_build::configure() - .build_server(false) - //.out_dir("src/google") // you can change the generated code's location + println!("cargo:rerun-if-changed=../../proto"); + + #[cfg(feature = "iso20022")] + build_proto("iso20022", Entity::ISO2022)?; + + Ok(()) +} + +#[cfg(feature = "iso20022")] +fn build_proto(package: &str, entity: Entity) -> Result<(), Box<dyn std::error::Error>> { + let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); + + let config = tonic_prost_build::configure() + .server_mod_attribute( + package, + format!("#[cfg(feature = \"rpc-server-{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"rpc-server-{package}\")))]"), + ) + .client_mod_attribute( + package, + format!("#[cfg(feature = \"rpc-client-{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"rpc-client-{package}\")))]"), + ); + + config + .file_descriptor_set_path(out_dir.join(format!("{package}_descriptor.bin"))) + .server_mod_attribute( + package, + format!("#[cfg(feature = \"rpc-server-{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"rpc-server-{package}\")))]"), + ) + .client_mod_attribute( + package, + format!("#[cfg(feature = \"rpc-client-{package}\")] #[cfg_attr(docsrs, doc(cfg(feature = \"rpc-client-{package}\")))]"), + ) .protoc_arg("-I=../..") + .compile_well_known_types(true) .compile_protos( - &["proto/googleapis/google/pubsub/v1/pubsub.proto"], - &["../../proto/googleapis"], // specify the root location to search proto dependencies + &entity.protos(), + &["../../proto/googleapis", "../../proto"], // specify the root location to search proto dependencies )?; + Ok(()) } diff --git a/lib/warden-core/src/google.rs b/lib/warden-core/src/google.rs new file mode 100644 index 0000000..365766c --- /dev/null +++ b/lib/warden-core/src/google.rs @@ -0,0 +1,8 @@ +/// Well known types +pub mod protobuf { + include!(concat!(env!("OUT_DIR"), "/google.protobuf.rs")); +} + +pub mod r#type { + include!(concat!(env!("OUT_DIR"), "/google.r#type.rs")); +} diff --git a/lib/warden-core/src/iso20022.rs b/lib/warden-core/src/iso20022.rs new file mode 100644 index 0000000..e9e4a24 --- /dev/null +++ b/lib/warden-core/src/iso20022.rs @@ -0,0 +1,32 @@ +#[derive(Debug)] +pub enum TransactionType { + PACS008, + PACS002, +} + +impl std::fmt::Display for TransactionType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + TransactionType::PACS002 => "pacs.002.001.12", + TransactionType::PACS008 => "pacs.008.001.12", + } + ) + } +} + +/// Pacs008 file descriptor +pub const ISO20022_FILE_DESCRIPTOR_SET: &[u8] = + tonic::include_file_descriptor_set!("iso20022_descriptor"); + +/// pacs.008.001.12 +pub mod pacs008 { + tonic::include_proto!("iso20022.pacs008"); +} + +/// pacs.002.001.12 +pub mod pacs002 { + tonic::include_proto!("iso20022.pacs002"); +} diff --git a/lib/warden-core/src/lib.rs b/lib/warden-core/src/lib.rs index b93cf3f..111ce5d 100644 --- a/lib/warden-core/src/lib.rs +++ b/lib/warden-core/src/lib.rs @@ -1,14 +1,17 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} +//! warden-core +#![cfg_attr(docsrs, feature(doc_cfg))] +#![warn( + missing_docs, + rustdoc::broken_intra_doc_links, + missing_debug_implementations +)] -#[cfg(test)] -mod tests { - use super::*; +/// Google well known types +#[allow(missing_docs)] +#[cfg(feature = "iso20022")] +pub mod google; - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +/// ISO20022 messages +#[allow(missing_docs)] +#[cfg(feature = "iso20022")] +pub mod iso20022; diff --git a/proto/iso20022/pacs.002.001.12.proto b/proto/iso20022/pacs_002_001_12.proto index 855d317..855d317 100644 --- a/proto/iso20022/pacs.002.001.12.proto +++ b/proto/iso20022/pacs_002_001_12.proto diff --git a/proto/iso20022/pacs.008.001.12.proto b/proto/iso20022/pacs_008_001_12.proto index f8c7804..f8c7804 100644 --- a/proto/iso20022/pacs.008.001.12.proto +++ b/proto/iso20022/pacs_008_001_12.proto |