librsigstopup 0.1.0

Super safe library untuk simulasi perhitungan top-up dengan JSON API, verbose logging, dan full trace
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
# librsigstopup

> **Safe, precise, and observable top‑up loan calculator** – with JSON API, structured logging, and full traceability.

<p align="center">
  <a href="https://crates.io/crates/librsigstopup"><img src="https://img.shields.io/crates/v/librsigstopup.svg?style=flat-square&logo=rust" alt="crates.io"></a>
  <a href="https://docs.rs/librsigstopup"><img src="https://img.shields.io/docsrs/librsigstopup?style=flat-square&logo=rust" alt="docs.rs"></a>
  <a href="https://github.com/neuxdotdev/librsigstopup/blob/main/LICENSE"><img src="https://img.shields.io/github/license/neuxdotdev/librsigstopup?style=flat-square" alt="license"></a>
  <a href="https://github.com/neuxdotdev/librsigstopup/actions"><img src="https://img.shields.io/github/actions/workflow/status/neuxdotdev/librsigstopup/ci.yml?branch=main&style=flat-square&logo=github" alt="CI"></a>
  <a href="https://rust-lang.org"><img src="https://img.shields.io/badge/rust-1.95+-blue?style=flat-square&logo=rust" alt="Rust"></a>
  <a href="https://tracing.rs"><img src="https://img.shields.io/badge/tracing-json-green?style=flat-square&logo=rust" alt="tracing"></a>
  <a href="https://docs.rs/rust_decimal"><img src="https://img.shields.io/badge/decimal-precision-orange?style=flat-square" alt="rust_decimal"></a>
  <a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="PRs welcome"></a>
</p>

---

## Table of Contents

- [Installation]#installation
- [Quick Start]#quick-start
- [Library API]#library-api
  - [`TopUpHandler`]#topuphandler
  - [`CalculatorConfig`]#calculatorconfig
  - [`LoanInput` & `LoanResult`]#loaninput--loanresult
  - [`CalculationError` & `ApiError`]#calculationerror--apierror
- [Configuration]#configuration
  - [Builder Pattern]#builder-pattern
  - [Serialization]#serialization
- [Error Handling]#error-handling
- [Logging & Tracing]#logging--tracing
- [JSON API]#json-api
- [Development]#development
  - [Project Structure]#project-structure
  - [Scripts]#scripts
- [Contributing]#contributing
- [License]#license

---

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
librsigstopup = "0.1.0"
tokio = { version = "1", features = ["rt"] }   # if you need async runtime
```

**Rust version requirement:** 1.95 or later.

---

## Quick Start

### Basic synchronous usage (async wrapper)

```rust
use librsigstopup::{TopUpHandler, LoanInput};

#[tokio::main]
async fn main() {
    let handler = TopUpHandler::new();

    let input = LoanInput::builder()
        .pinjaman(10_000_000.0)   // loan amount
        .angsuran(500_000.0)      // monthly installment
        .sisa_tenor(5)            // remaining months
        .diskon(200_000.0)        // discount
        .pajak(150_000.0)         // tax (will be added to admin fee)
        .build()
        .unwrap();

    let result = handler.calculate(input).await.unwrap();
    println!("Total diterima nasabah: Rp {}", result.total_didapat);
    // Output: Total diterima nasabah: Rp 7280000.00
}
```

### Using JSON input

```rust
let json = r#"{
    "pinjaman": 10000000,
    "angsuran": 500000,
    "sisa_tenor": 5,
    "diskon": 200000,
    "pajak": 150000
}"#;

let response = handler.calculate_from_json(json).await.unwrap();
println!("{}", response);
```

### Custom configuration

```rust
use librsigstopup::{CalculatorConfig, TopUpHandler};

let config = CalculatorConfig::builder()
    .insurance_percentage(2.0)   // 2% insurance
    .admin_fee(100_000.0)        // Rp 100k admin fee
    .enable_logging(true)
    .log_level("debug")
    .build();

let handler = TopUpHandler::new().with_config(config);
```

---

## Library API

### `TopUpHandler`

Main entry point for calculations.

```rust
pub struct TopUpHandler { /* ... */ }

impl TopUpHandler {
    pub fn new() -> Self;
    pub fn init_verbose() -> Self;   // enables verbose JSON logging
    pub fn with_config(self, config: CalculatorConfig) -> Self;
    pub async fn calculate(&self, input: LoanInput) -> Result<LoanResult, CalculationError>;
    pub async fn calculate_from_json(&self, json: &str) -> Result<String, CalculationError>;
    pub async fn health_check(&self) -> serde_json::Value;
}
```

### `CalculatorConfig`

Controls insurance percentage, admin fee, and logging behaviour.

| Field            | Type              | Default                    | Description                          |
| ---------------- | ----------------- | -------------------------- | ------------------------------------ |
| `insurance`      | `InsuranceConfig` | `percentage: 0.015` (1.5%) | Insurance rate applied to principal. |
| `tax`            | `TaxConfig`       | `admin_fee: 120_000`       | Fixed administrative fee.            |
| `enable_logging` | `bool`            | `true`                     | Enable structured JSON logs.         |
| `log_level`      | `String`          | `"info"`                   | Log level filter.                    |

Use the **builder** to create a custom config:

```rust
let config = CalculatorConfig::builder()
    .insurance_percentage(2.5)   // 2.5% (converted to 0.025 internally)
    .admin_fee(150_000.0)
    .enable_logging(false)
    .log_level("warn")
    .build();
```

### `LoanInput` & `LoanResult`

**LoanInput** – required fields (all `Decimal` except `sisa_tenor`):

| Field        | Type      | Description                 |
| ------------ | --------- | --------------------------- |
| `pinjaman`   | `Decimal` | Loan principal (must be >0) |
| `angsuran`   | `Decimal` | Monthly installment (≥0)    |
| `sisa_tenor` | `u32`     | Remaining months            |
| `diskon`     | `Decimal` | Discount (default 0)        |
| `pajak`      | `Decimal` | Input tax (default 0)       |

Builder with fallible `build()`:

```rust
let input = LoanInput::builder()
    .pinjaman(10_000_000.0)
    .angsuran(500_000.0)
    .sisa_tenor(5)
    .build()?;   // returns Result<LoanInput, ValidationError>
```

**LoanResult** contains:

- `total_didapat` – final amount the customer receives.
- `calculations` – intermediate values (`sisa_pokok`, `asuransi`, `pajak`, etc.)
- `metadata` – calculation time, formula, warnings.

### `CalculationError` & `ApiError`

All errors implement `std::error::Error` and serialise to JSON.

```rust
pub enum CalculationError {
    ValidationError { field: String, message: String },
    InvalidInput(String),
    CalculationFailed(String),
    JsonParseError(String),
    ConfigError(String),
    InternalError(String),
}
```

Convert to user‑friendly `ApiError`:

```rust
let api_err: ApiError = calculation_error.into();
println!("{}", api_err.error.code); // e.g. "VALIDATION_ERROR"
```

---

## Configuration

### Builder Pattern

All configuration uses a fluent builder with validation.

```rust
let config = CalculatorConfig::builder()
    .insurance_percentage(1.75)   // 1.75%
    .admin_fee(125_000.0)         // Rp 125,000
    .enable_logging(true)
    .log_level("debug")
    .build();
```

Missing fields fall back to defaults. Percentages are divided by 100 internally (2.5 → 0.025).

### Serialization

`CalculatorConfig` implements `Serialize`/`Deserialize` – perfect for loading from file or environment.

```rust
let json = r#"{
    "insurance": { "percentage": "0.02" },
    "tax": { "base_amount": "0", "admin_fee": "150000" },
    "enable_logging": false,
    "log_level": "error"
}"#;
let config: CalculatorConfig = serde_json::from_str(json).unwrap();
```

---

## Error Handling

The library distinguishes between recoverable and unrecoverable errors.

```rust
match handler.calculate(input).await {
    Ok(result) => println!("Success: {}", result.total_didapat),
    Err(CalculationError::ValidationError { field, message }) => {
        eprintln!("Input error in {}: {}", field, message);
    }
    Err(CalculationError::JsonParseError(e)) => {
        eprintln!("Invalid JSON: {}", e);
    }
    Err(e) => eprintln!("Unexpected error: {}", e),
}
```

All errors provide `.to_json()` for structured logging.

---

## Logging & Tracing

The library uses the [`tracing`] crate with **JSON formatting**. Call `init_logging()` once at startup:

```rust
librsigstopup::init_logging();
```

Every calculation emits structured events:

- `calculation_started` – includes `request_id` and input.
- `calculation_completed` – includes total, duration, result.
- `input_validated`, `calculate_insurance`, `calculate_tax`, etc.

Set the log level via `RUST_LOG` environment variable:

```bash
RUST_LOG=debug cargo run
```

Disable logging via `CalculatorConfig::builder().enable_logging(false).build()`.

---

## JSON API

The library exposes a JSON‑friendly interface for easy integration.

**Request** (`CalculationRequest`):

```json
{
  "pinjaman": 10000000,
  "angsuran": 500000,
  "sisa_tenor": 5,
  "diskon": 200000,
  "pajak": 150000
}
```

**Response** (`CalculationResponse`):

```json
{
  "success": true,
  "request_id": "550e8400-e29b-41d4-a716-446655440000",
  "timestamp": "2025-04-20T12:34:56Z",
  "data": {
    "input": {
      "pinjaman": "10000000",
      "angsuran": "500000",
      "sisa_tenor": 5,
      "diskon": "200000",
      "pajak": "150000"
    },
    "calculations": {
      "sisa_pokok": "7500000",
      "asuransi": "150000",
      "pajak": "270000",
      "diskon": "200000"
    },
    "total_didapat": "7280000.00"
  },
  "metadata": {
    "calculation_time_ms": 42,
    "formula_used": "(E4-(E5*E6))-E8-E9+E7",
    "warnings": []
  }
}
```

Health check endpoint:

```rust
let health = handler.health_check().await;
// { "status": "healthy", "timestamp": "...", "version": "0.1.0", "config": {...} }
```

---

## Development

```bash
git clone https://github.com/neuxdotdev/librsigstopup.git
cd librsigstopup
cargo build
cargo test
```

### Project Structure

```
.
├── Cargo.toml
├── src/
│   ├── core/                 # Pure calculation logic
│   │   ├── loan.rs           # LoanInput, builder, validation
│   │   ├── insurance_or_cp.rs
│   │   ├── tax.rs
│   │   ├── remaining_installments.rs
│   │   ├── total.rs
│   │   └── ...
│   ├── handler/              # API handlers, config, errors
│   │   ├── config.rs         # CalculatorConfig & builder
│   │   ├── error.rs          # Error types & conversions
│   │   ├── types.rs          # Request/response DTOs
│   │   └── mod.rs            # TopUpHandler implementation
│   └── lib.rs                # Public exports & logging init
├── tests/                    # Integration tests
├── benches/                  # Benchmarks (Criterion)
└── examples/                 # Usage examples
```

### Features

- **`json-logging`** (default) – JSON log output.
- **`verbose`** – Enable verbose tracing (more spans).
- **`android`** – Support Android logging via `android_logger`.
- **`ffi`** – Build C‑compatible static library (see `crate-type = ["staticlib"]`).

---

## Contributing

1. Fork the repository.
2. Create a feature branch (`git checkout -b feat/your-feature`).
3. Commit changes with clear messages.
4. Run `cargo test` and `cargo clippy` to ensure quality.
5. Push and open a Pull Request.

All contributions are welcome – bug reports, feature requests, documentation improvements.

---

## License

**MIT License** – see [LICENSE](https://github.com/neuxdotdev/librsigstopup/blob/main/LICENSE) for details.

---

## Acknowledgements

- Built on [`rust_decimal`] for precise financial arithmetic.
- Structured logging with [`tracing`] and `tracing-subscriber`.
- Inspired by real‑world top‑up loan workflows in Indonesian fintech.

---

> **Repository**: https://github.com/neuxdotdev/librsigstopup  
> **Crates.io**: https://crates.io/crates/librsigstopup