soap-service 0.2.0

Rust macro that transforms modules into SOAP webservices.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# SOAP Service Macro Implementation Plan

## Project Overview

Create a Rust procedural macro crate `soap-service` that transforms modules
containing async functions into SOAP web services with automatic Axum router
generation and WSDL endpoint creation.

## Project Structure

```
soap-service/
├── Cargo.toml
├── src/
│   ├── lib.rs              # Main macro entry point and public API
│   ├── parser/
│   │   ├── mod.rs          # Module parsing orchestration
│   │   ├── attributes.rs   # Parse #[service(...)] attributes
│   │   ├── functions.rs    # Parse and validate async functions
│   │   └── types.rs        # Analyze request/response struct types
│   ├── codegen/
│   │   ├── mod.rs          # Code generation orchestration
│   │   ├── router.rs       # Generate Axum router code
│   │   ├── handlers.rs     # Generate SOAP request handlers
│   │   └── wsdl.rs         # Generate WSDL document and endpoint
│   ├── soap/
│   │   ├── mod.rs          # SOAP processing utilities
│   │   ├── envelope.rs     # SOAP envelope parsing/generation
│   │   ├── serialization.rs # XML serialization helpers
│   │   └── faults.rs       # SOAP fault generation
│   └── schema/
│       ├── mod.rs          # XML Schema generation
│       ├── types.rs        # Rust type to XSD mapping
│       └── templates.rs    # WSDL template generation
├── examples/
│   ├── calculator/
│   │   ├── Cargo.toml
│   │   └── src/main.rs     # Complete working example
│   └── multi-service/
│       ├── Cargo.toml
│       └── src/main.rs     # Multiple services example
└── tests/
    ├── integration/
    │   ├── basic_service.rs
    │   ├── multiple_services.rs
    │   └── error_handling.rs
    └── unit/
        ├── parser_tests.rs
        ├── codegen_tests.rs
        └── soap_tests.rs
```

## Phase 1: Project Setup and Dependencies

### Cargo.toml Configuration

```toml
[package]
name = "soap-service"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
# Proc macro essentials
proc-macro2 = "1.0"
syn = { version = "2.0", features = ["full", "extra-traits", "visit"] }
quote = "1.0"

# XML processing
serde = { version = "1.0", features = ["derive"] }
serde-xml-rs = "0.6"
quick-xml = { version = "0.31", features = ["serialize"] }

# Optional runtime dependencies (for generated code)
axum = { version = "0.7", optional = true }
tokio = { version = "1.0", features = ["full"], optional = true }

[features]
default = ["runtime"]
runtime = ["axum", "tokio"]

[dev-dependencies]
axum = "0.7"
tokio = { version = "1.0", features = ["full"] }
tower = "0.4"
hyper = "1.0"
```

### Initial File Structure Creation

Create all directories and stub files with basic module declarations and TODO
comments.

## Phase 2: Attribute Parsing (src/parser/attributes.rs)

### Parse Service Attributes

```rust
#[derive(Debug, Clone)]
pub struct ServiceConfig {
    pub namespace: String,
    pub service_name: String,
    pub port_name: String,
    pub bind_path: String,
}

pub fn parse_service_attributes(args: TokenStream) -> Result<ServiceConfig, syn::Error> {
    // Parse: namespace = "...", service_name = "...", etc.
    // Validate required fields are present
    // Return structured config
}
```

### Validation Rules

- `namespace` must be a valid URI
- `service_name` and `port_name` must be valid identifiers
- `bind_path` must start with `/`

## Phase 3: Function Analysis (src/parser/functions.rs)

### Function Signature Validation

```rust
#[derive(Debug, Clone)]
pub struct SoapOperation {
    pub name: String,
    pub function_name: syn::Ident,
    pub request_type: syn::Type,
    pub response_type: syn::Type,
    pub error_type: syn::Type,
}

pub fn extract_soap_operations(module: &syn::ItemMod) -> Result<Vec<SoapOperation>, syn::Error> {
    // Find all `pub async fn` functions
    // Validate signature: fn_name(RequestType) -> Result<ResponseType, ErrorType>
    // Extract type information
    // Generate operation names from function names
}
```

### Function Requirements Check

- Must be `pub async fn`
- Exactly one parameter (request struct)
- Return type must be `Result<T, E>`
- Request/Response types must be in scope

## Phase 4: Type System Analysis (src/parser/types.rs)

### Type Information Extraction

```rust
#[derive(Debug, Clone)]
pub struct TypeInfo {
    pub name: String,
    pub fields: Vec<FieldInfo>,
    pub namespace: Option<String>,
}

#[derive(Debug, Clone)]
pub struct FieldInfo {
    pub name: String,
    pub xml_name: String, // From serde rename attribute
    pub field_type: FieldType,
    pub optional: bool,
}

pub fn analyze_type(ty: &syn::Type) -> Result<TypeInfo, syn::Error> {
    // Parse struct definitions
    // Extract field information
    // Handle serde attributes (#[serde(rename = "...")])
    // Detect optional fields (Option<T>)
}
```

## Phase 5: SOAP Envelope Processing (src/soap/envelope.rs)

### Request Processing

```rust
pub struct SoapRequest {
    pub operation: String,
    pub body: String,
    pub headers: std::collections::HashMap<String, String>,
}

pub fn parse_soap_request(xml: &str) -> Result<SoapRequest, SoapError> {
    // Parse SOAP envelope
    // Extract operation name from SOAPAction header or body
    // Extract body content for deserialization
}

pub fn create_soap_response<T: serde::Serialize>(
    result: &T,
    operation: &str,
    namespace: &str
) -> Result<String, SoapError> {
    // Generate SOAP response envelope
    // Serialize response data to XML
    // Include proper namespaces
}
```

### Error Handling

```rust
pub fn create_soap_fault(error: &dyn std::fmt::Display) -> String {
    // Generate SOAP fault response
    // Include error message in faultstring
    // Use standard SOAP fault structure
}
```

## Phase 6: WSDL Generation (src/codegen/wsdl.rs)

### Schema Generation

```rust
pub fn generate_wsdl(
    config: &ServiceConfig,
    operations: &[SoapOperation],
    types: &[TypeInfo]
) -> String {
    // Generate complete WSDL document
    // Include type definitions (XSD schema)
    // Define messages, port types, bindings, services
    // Use proper SOAP 1.1/1.2 bindings
}

pub fn rust_type_to_xsd_type(rust_type: &str) -> &str {
    match rust_type {
        "i32" => "xsd:int",
        "i64" => "xsd:long",
        "f32" => "xsd:float",
        "f64" => "xsd:double",
        "String" => "xsd:string",
        "bool" => "xsd:boolean",
        _ => "xsd:string", // fallback
    }
}
```

## Phase 7: Code Generation (src/codegen/router.rs)

### Router Generation

```rust
pub fn generate_router_function(
    config: &ServiceConfig,
    operations: &[SoapOperation]
) -> proc_macro2::TokenStream {
    quote! {
        pub fn router() -> axum::Router {
            axum::Router::new()
                .route(#bind_path, axum::routing::post(soap_handler))
                .route(&format!("{}/wsdl", #bind_path), axum::routing::get(wsdl_handler))
        }

        async fn soap_handler(body: String) -> axum::response::Response {
            // Generated SOAP request dispatcher
        }

        async fn wsdl_handler() -> axum::response::Response {
            // Return generated WSDL
        }
    }
}
```

### Operation Dispatcher

```rust
pub fn generate_dispatcher(operations: &[SoapOperation]) -> proc_macro2::TokenStream {
    // Generate match statement routing operations to functions
    // Handle deserialization and serialization
    // Include error handling with SOAP faults
}
```

## Phase 8: Main Macro Implementation (src/lib.rs)

```rust
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn service(args: TokenStream, input: TokenStream) -> TokenStream {
    let config = match parser::attributes::parse_service_attributes(args.into()) {
        Ok(config) => config,
        Err(e) => return e.to_compile_error().into(),
    };

    let module = match syn::parse::<syn::ItemMod>(input.clone()) {
        Ok(module) => module,
        Err(e) => return e.to_compile_error().into(),
    };

    let operations = match parser::functions::extract_soap_operations(&module) {
        Ok(ops) => ops,
        Err(e) => return e.to_compile_error().into(),
    };

    let enhanced_module = codegen::generate_enhanced_module(
        module,
        config,
        operations
    );

    enhanced_module.into()
}
```

## Phase 9: Example Implementation

### Calculator Service Example

```rust
// examples/calculator/src/main.rs
use soap_service::service;

#[derive(Debug)]
pub struct ServiceError(pub String);

impl std::fmt::Display for ServiceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[service(
    namespace = "http://example.com/calculator",
    service_name = "CalculatorService",
    port_name = "CalculatorPort",
    bind_path = "/soap/calculator"
)]
mod calculator {
    use super::ServiceError;
    use serde::{Deserialize, Serialize};

    #[derive(Deserialize, Serialize, Debug)]
    pub struct AddRequest {
        #[serde(rename = "Operand1")]
        pub a: i32,
        #[serde(rename = "Operand2")]
        pub b: i32,
    }

    #[derive(Deserialize, Serialize, Debug)]
    pub struct AddResponse {
        #[serde(rename = "Result")]
        pub sum: i32,
    }

    pub async fn add(req: AddRequest) -> Result<AddResponse, ServiceError> {
        if req.a == 0 && req.b == 0 {
            return Err(ServiceError("Cannot add two zeros".to_string()));
        }
        Ok(AddResponse { sum: req.a + req.b })
    }
}

#[tokio::main]
async fn main() {
    let app = axum::Router::new()
        .merge(calculator::router())
        .route("/health", axum::routing::get(|| async { "OK" }));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();

    println!("Calculator SOAP Service running on http://localhost:3000");
    println!("WSDL available at: http://localhost:3000/soap/calculator/wsdl");

    axum::serve(listener, app).await.unwrap();
}
```

## Phase 10: Testing Strategy

### Unit Tests

- Attribute parsing with various configurations
- Function signature validation
- Type analysis accuracy
- WSDL generation correctness
- SOAP envelope parsing/generation

### Integration Tests

- Complete service generation and compilation
- SOAP request/response roundtrip
- Multiple services in one application
- Error handling and SOAP fault generation
- WSDL validation against standard tools

### Example-based Testing

- Ensure examples compile and run
- Test with real SOAP clients
- Performance benchmarks

## Implementation Order

1. **Phase 1-2**: Setup project structure and attribute parsing
2. **Phase 3**: Function analysis and validation
3. **Phase 4**: Type system analysis with serde integration
4. **Phase 5**: Basic SOAP envelope processing
5. **Phase 6**: WSDL generation for simple types
6. **Phase 7**: Code generation for router and handlers
7. **Phase 8**: Main macro integration and testing
8. **Phase 9**: Complete working example
9. **Phase 10**: Comprehensive testing and documentation

## Success Criteria

- [ ] Macro compiles without errors
- [ ] Generated code compiles without warnings
- [ ] Calculator example runs and handles SOAP requests
- [ ] WSDL validates against standard WSDL validators
- [ ] SOAP clients can successfully call generated services
- [ ] Error handling produces valid SOAP faults
- [ ] Multiple services can coexist in one application
- [ ] Documentation is complete with examples

## Deliverables

1. Complete `soap-service` crate ready for publication
2. Working calculator example demonstrating all features
3. Comprehensive test suite
4. Documentation with usage examples
5. README with quick start guide