forgedb 0.2.0

ForgeDB — an application database generator. Compiles a declarative .forge schema into tailored Rust database code, a TypeScript SDK, and a REST API.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# Contributing to ForgeDB

Thank you for your interest in contributing to ForgeDB! This guide will help you get started.

## Table of Contents

- [Code of Conduct]#code-of-conduct
- [How Can I Contribute?]#how-can-i-contribute
- [Development Setup]#development-setup
- [Testing Requirements]#testing-requirements
- [Pull Request Process]#pull-request-process
- [Code Style]#code-style
- [Documentation Requirements]#documentation-requirements
- [Community]#community

---

## Code of Conduct

### Our Pledge

We are committed to providing a welcoming and inspiring community for all. Please be respectful and constructive in all interactions.

### Our Standards

**Positive behavior includes:**
- Using welcoming and inclusive language
- Being respectful of differing viewpoints
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards others

**Unacceptable behavior includes:**
- Harassment or discriminatory language
- Trolling, insulting comments, or personal attacks
- Public or private harassment
- Publishing others' private information
- Other conduct inappropriate in a professional setting

### Enforcement

Project maintainers have the right to remove, edit, or reject comments, commits, code, issues, and other contributions that do not align with this Code of Conduct.

Report unacceptable behavior by opening a confidential report through
[GitHub's private reporting](https://github.com/hoodiecollin/forgedb/security/advisories) or by
contacting the maintainers via a GitHub issue.

---

## How Can I Contribute?

### Reporting Bugs

Before creating bug reports, please check existing issues to avoid duplicates.

**When filing a bug report, include:**
- Clear, descriptive title
- Exact steps to reproduce
- Expected vs. actual behavior
- ForgeDB version (`forgedb --version`)
- Operating system and version
- Rust version (`rustc --version`)
- Relevant schema files (if applicable)
- Error messages or logs

**Bug Report Template:**
```markdown
### Description
[Clear description of the bug]

### Steps to Reproduce
1. Create schema with...
2. Run command...
3. Observe error...

### Expected Behavior
[What should happen]

### Actual Behavior
[What actually happens]

### Environment
- ForgeDB version: 0.1.0
- OS: Ubuntu 22.04
- Rust: 1.70.0

### Additional Context
[Logs, screenshots, etc.]
```

### Suggesting Enhancements

**Enhancement suggestions should include:**
- Clear, descriptive title
- Detailed description of proposed functionality
- Use cases and benefits
- Examples of how it would work
- Potential drawbacks or alternatives

**Enhancement Template:**
```markdown
### Feature Description
[Clear description]

### Motivation
[Why is this needed?]

### Proposed Solution
[How should it work?]

### Examples
```forgedb
// Example schema or code
```

### Alternatives Considered
[Other approaches]

### Additional Context
[Links, references, etc.]
```

**Design notes & proposals live as issues, not committed files.** Non-trivial designs are
captured as GitHub issues labeled **`rfc`** (request for comment) — we do **not** commit
proposal/design documents to the repository. Durable *architecture* reference for shipped
features belongs in [`ARCHITECTURE.md`](./ARCHITECTURE.md); an RFC issue holds the forward-looking
design while it's under discussion. This keeps the tree free of point-in-time design notes that
drift out of sync with the code.

### Your First Code Contribution

**Good first issues** are labeled `good-first-issue` on GitHub. These are:
- Well-defined and scoped
- Have clear acceptance criteria
- Don't require deep system knowledge

**Areas needing help:**
- Documentation improvements
- Test coverage
- Bug fixes
- Code examples
- Performance optimizations

### Pull Requests

See [Pull Request Process](#pull-request-process) below for detailed guidelines.

---

## Development Setup

### Prerequisites

**Required:**
- Rust 1.70+ ([rustup.rs]https://rustup.rs/)
- Git

**Optional but recommended:**
- VSCode with rust-analyzer extension
- Cargo watch (`cargo install cargo-watch`)

### Clone Repository

```bash
git clone https://github.com/yourusername/forgedb.git
cd forgedb
```

### Build Project

```bash
# Build all crates
cargo build

# Build specific crate
cargo build --package forgedb-parser

# Build with optimizations
cargo build --release
```

### Run Tests

```bash
# Run all tests
cargo test --lib

# Run tests for specific crate
cargo test --package forgedb-storage

# Run specific test
cargo test test_parse_model --package forgedb-parser

# Run with output
cargo test -- --nocapture

# Run with specific features
cargo test --features "full-text-search"
```

### Run Examples

```bash
# List examples
ls examples/

# Run example
cargo run -- generate all --output ./generated
```

### Development Tools

**Cargo Watch** (auto-rebuild on changes):
```bash
cargo install cargo-watch
cargo watch -x build
cargo watch -x test
```

**Clippy** (linter):
```bash
cargo clippy --all-targets --all-features
```

**Rustfmt** (formatter):
```bash
cargo fmt --all
```

**Documentation**:
```bash
cargo doc --open
```

### IDE Setup

**VSCode** (`.vscode/settings.json`):
```json
{
  "rust-analyzer.cargo.features": "all",
  "rust-analyzer.checkOnSave.command": "clippy",
  "editor.formatOnSave": true,
  "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer"
  }
}
```

---

## Testing Requirements

### Test Coverage Requirements

All contributions must include tests:

**For new features:**
- Unit tests for individual functions
- Integration tests for workflows
- Documentation tests (doctests)
- Test coverage ≥ 80%

**For bug fixes:**
- Regression test that fails without the fix
- Verify fix resolves the issue

### Writing Tests

**Unit Tests:**
```rust
#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_parse_simple_model() {
        let input = r#"
        User {
          id: +uuid
          email: string
        }
        "#;
        
        let result = parse_schema(input);
        assert!(result.is_ok());
        
        let schema = result.unwrap();
        assert_eq!(schema.models.len(), 1);
        assert_eq!(schema.models[0].name, "User");
        assert_eq!(schema.models[0].fields.len(), 2);
    }
    
    #[test]
    fn test_parse_invalid_syntax() {
        let input = "User { id: invalidtype }";
        let result = parse_schema(input);
        assert!(result.is_err());
    }
}
```

**Integration Tests** (`tests/` directory):
```rust
use forgedb_parser::parse_schema;
use forgedb_validation::validate_schema;

#[test]
fn test_full_validation_pipeline() {
    let input = include_str!("fixtures/valid_schema.forge");
    
    let schema = parse_schema(input).expect("Parse failed");
    let errors = validate_schema(&schema);
    
    assert!(errors.is_empty(), "Validation errors: {:?}", errors);
}
```

**Documentation Tests:**
```rust
/// Parse a ForgeDB schema from string.
///
/// # Examples
///
/// ```
/// use forgedb_parser::parse_schema;
///
/// let schema = parse_schema(r#"
///     User {
///       id: +uuid
///       email: string
///     }
/// "#).unwrap();
///
/// assert_eq!(schema.models.len(), 1);
/// ```
pub fn parse_schema(input: &str) -> Result<Schema> {
    // Implementation
}
```

### Running Specific Test Categories

```bash
# Unit tests only
cargo test --lib

# Integration tests only
cargo test --test '*'

# Doc tests only
cargo test --doc

# Specific crate
cargo test --package forgedb-parser

# With code coverage (requires cargo-tarpaulin)
cargo install cargo-tarpaulin
cargo tarpaulin --out Html
```

### Test Organization

```
crate-name/
├── src/
│   ├── lib.rs           # Unit tests in mod tests { }
│   └── parser.rs        # Unit tests in mod tests { }
├── tests/
│   ├── integration_test.rs
│   └── fixtures/
│       └── test_schema.forge
└── Cargo.toml
```

---

## Pull Request Process

### Before Submitting

**1. Create an issue** (if one doesn't exist) describing the change.

**2. Fork and create a branch:**
```bash
git checkout -b feature/my-new-feature
# or
git checkout -b fix/bug-description
```

**3. Make your changes** following [Code Style](#code-style).

**4. Add tests** covering your changes.

**5. Run checks:**
```bash
# Format code
cargo fmt --all

# Run linter
cargo clippy --all-targets --all-features -- -D warnings

# Run tests
cargo test --all

# Build documentation
cargo doc --no-deps
```

**6. Update documentation** (see [Documentation Requirements](#documentation-requirements)).

**7. Commit changes** with clear messages:
```bash
git add .
git commit -m "feat: Add full-text search support

- Implement tokenization
- Add inverted index
- Create search query parser
- Add integration tests

Closes #123"
```

### Commit Message Format

Follow [Conventional Commits](https://www.conventionalcommits.org/):

```
<type>(<scope>): <subject>

<body>

<footer>
```

**Types:**
- `feat`: New feature
- `fix`: Bug fix
- `docs`: Documentation only
- `style`: Code style (formatting, semicolons, etc.)
- `refactor`: Code refactoring
- `perf`: Performance improvements
- `test`: Adding tests
- `chore`: Maintenance tasks

**Examples:**
```
feat(parser): Add support for enum types

Implements enum parsing in schema language.

Closes #45

---

fix(storage): Correct off-by-one error in column indexing

The column index was incorrectly calculated when accessing
variable-length columns, causing data corruption.

Fixes #78

---

docs(contributing): Update testing requirements

Clarify coverage requirements and add examples.
```

### Submitting Pull Request

**1. Push to your fork:**
```bash
git push origin feature/my-new-feature
```

**2. Create PR on GitHub** with:

**PR Title**: Clear, descriptive (like commit message)

**PR Description Template**:
```markdown
## Description
[What does this PR do?]

## Motivation
[Why is this change needed?]

## Changes
- [ ] Feature 1
- [ ] Feature 2
- [ ] Tests added
- [ ] Documentation updated

## Testing
[How was this tested?]

## Checklist
- [ ] Code follows style guidelines
- [ ] Self-reviewed code
- [ ] Commented complex code
- [ ] Updated documentation
- [ ] No new warnings
- [ ] Added tests
- [ ] All tests pass
- [ ] Updated CHANGELOG.md

## Related Issues
Closes #123
```

### Review Process

**1. Automated checks** run (CI):
- Build succeeds
- Tests pass
- Linter passes
- Documentation builds

**2. Code review** by maintainers:
- Code quality
- Test coverage
- Documentation
- API design

**3. Address feedback**:
```bash
# Make changes
git add .
git commit -m "address review feedback"
git push origin feature/my-new-feature
```

**4. Merge** when approved:
- Squash and merge (default)
- Rebase and merge (for clean history)
- Merge commit (for feature branches)

### After Merge

- Delete your branch
- Close related issues
- Update project board (if applicable)

---

## Code Style

### Rust Style Guide

Follow [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/) and use `rustfmt`:

```bash
# Format all code
cargo fmt --all

# Check formatting without changing
cargo fmt --all -- --check
```

### Code Conventions

**Naming:**
```rust
// Types: PascalCase
struct UserTable { }
enum FieldType { }

// Functions: snake_case
fn parse_schema() { }
fn validate_field() { }

// Constants: SCREAMING_SNAKE_CASE
const MAX_FIELD_LENGTH: usize = 255;

// Modules: snake_case
mod parser;
mod validation;
```

**Imports:**
```rust
// Standard library
use std::collections::HashMap;
use std::path::{Path, PathBuf};

// External crates
use serde::{Serialize, Deserialize};
use uuid::Uuid;

// Internal crates
use forgedb_parser::Schema;
use forgedb_storage::Database;

// Local modules
use crate::error::ParseError;
use super::types::FieldType;
```

**Error Handling:**
```rust
// Use Result for recoverable errors
pub fn parse_schema(input: &str) -> Result<Schema, ParseError> {
    // Implementation
}

// Use thiserror for error definitions
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    #[error("Unexpected token: {0}")]
    UnexpectedToken(String),
    
    #[error("Invalid type: {0}")]
    InvalidType(String),
}

// Use anyhow for application code
use anyhow::{Context, Result};

fn load_schema(path: &Path) -> Result<Schema> {
    let content = fs::read_to_string(path)
        .context("Failed to read schema file")?;
    
    parse_schema(&content)
        .context("Failed to parse schema")
}
```

**Documentation:**
```rust
/// Parse a ForgeDB schema from a string.
///
/// This function tokenizes and parses the input, producing an
/// Abstract Syntax Tree (AST) representing the schema structure.
///
/// # Arguments
///
/// * `input` - Schema definition as a string
///
/// # Returns
///
/// * `Ok(Schema)` - Successfully parsed schema
/// * `Err(ParseError)` - Parse error with details
///
/// # Examples
///
/// ```
/// use forgedb_parser::parse_schema;
///
/// let schema = parse_schema(r#"
///     User {
///       id: +uuid
///       email: string
///     }
/// "#)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns `ParseError` if:
/// - Syntax is invalid
/// - Unknown types are referenced
/// - Directives are malformed
pub fn parse_schema(input: &str) -> Result<Schema, ParseError> {
    // Implementation
}
```

**Comments:**
```rust
// Single-line comment for code explanations

/// Doc comment for public items
/// (use /// for functions, structs, etc.)

//! Module-level doc comment
//! (use //! at the top of modules)
```

### Clippy Lints

Enable strict linting:

```rust
// In lib.rs or main.rs
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]  // Allow if appropriate
```

Run clippy:
```bash
cargo clippy --all-targets --all-features -- -D warnings
```

### Performance Guidelines

**Avoid unnecessary allocations:**
```rust
// ❌ Bad
fn process(data: String) -> String {
    data.to_uppercase()
}

// ✅ Good
fn process(data: &str) -> String {
    data.to_uppercase()
}
```

**Use appropriate data structures:**
```rust
// ❌ Bad: O(n) lookups
let users = Vec<User>;
users.iter().find(|u| u.id == target_id);

// ✅ Good: O(1) lookups
let users = HashMap<Uuid, User>;
users.get(&target_id);
```

**Profile before optimizing:**
```bash
# Use cargo-flamegraph for profiling
cargo install flamegraph
cargo flamegraph --test my_test
```

---

## Documentation Requirements

### Code Documentation

**All public items must be documented:**
```rust
/// Public struct: Always document
pub struct Database { }

/// Public function: Always document
pub fn open_database() -> Result<Database> { }

// Private function: Optional but recommended
fn internal_helper() { }
```

**Documentation sections:**
- Brief description (one line)
- Detailed explanation (optional)
- Arguments (`# Arguments`)
- Return value (`# Returns`)
- Examples (`# Examples`)
- Errors (`# Errors`)
- Panics (`# Panics`)
- Safety (`# Safety` for unsafe code)

### README Updates

Update relevant READMEs when changing:
- Public APIs
- Usage patterns
- Configuration options
- Installation instructions

**README structure:**
```markdown
# Crate Name

Brief description.

## Features

- Feature 1
- Feature 2

## Usage

```rust
// Code example
```

## Documentation

See [docs.rs](https://docs.rs/crate-name)

## License

MIT or Apache-2.0
```

### Architecture Documentation

Update architecture docs when changing:
- System design
- Component interactions
- Data flow
- Design decisions

See [ARCHITECTURE.md](./ARCHITECTURE.md).

### Changelog

Update `CHANGELOG.md` for all user-facing changes:

```markdown
## [Unreleased]

### Added
- New feature description (#PR)

### Changed
- Changed behavior description (#PR)

### Fixed
- Bug fix description (#PR)

### Deprecated
- Deprecated feature (#PR)

### Removed
- Removed feature (#PR)

### Security
- Security fix description (#PR)
```

---

## Community

### Communication Channels

- **GitHub Issues**: Bug reports, feature requests
- **GitHub Discussions**: Questions, ideas, showcase

### Getting Help

**Before asking:**
1. Check documentation
2. Search existing issues

**When asking:**
- Provide context and details
- Include code examples
- Share error messages
- Specify your environment

### Recognition

Contributors are recognized in:
- CONTRIBUTORS.md file
- Release notes
- Social media shout-outs

### License

By contributing, you agree that your contributions will be licensed under:
- MIT License OR
- Apache License 2.0

See [LICENSE-MIT](../LICENSE-MIT) and [LICENSE-APACHE](../LICENSE-APACHE).

---

## Additional Resources

- [Architecture Documentation](./ARCHITECTURE.md)
- [Development Guide](./DEVELOPMENT.md)
- [Publishing Process](./PUBLISHING.md)
- [Public Crates Guide](./PUBLIC_CRATES.md)

---

Thank you for contributing to ForgeDB! 🚀