Skip to content

Commit 4cf619b

Browse files
committed
feat: generate schemas and well-known instances resolves #70
Signed-off-by: devjow <me@devjow.com>
1 parent cd8ed5b commit 4cf619b

32 files changed

Lines changed: 3272 additions & 184 deletions

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gts-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/main.rs"
2121

2222
[dependencies]
2323
gts.workspace = true
24+
gts-id.workspace = true
2425
serde.workspace = true
2526
serde_json.workspace = true
2627
anyhow.workspace = true

gts-cli/src/cli.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::{Parser, Subcommand};
33
use gts::GtsOps;
44
use std::io::Write;
55

6+
use crate::gen_instances::generate_instances_from_rust;
67
use crate::gen_schemas::generate_schemas_from_rust;
78
use crate::server::GtsHttpServer;
89

@@ -119,20 +120,35 @@ pub enum Commands {
119120
#[arg(long, default_value = "8000")]
120121
port: u16,
121122
},
122-
/// Generate GTS schemas from Rust source code with `#[struct_to_gts_schema]` annotations
123+
/// Generate GTS artifacts from Rust source code with `#[struct_to_gts_schema]` /
124+
/// `#[gts_well_known_instance]` annotations
123125
GenerateFromRust {
124-
/// Source directory or file to scan for annotated structs
126+
/// Source directory or file to scan for annotated items
125127
#[arg(long)]
126128
source: String,
127-
/// Output directory for generated schemas (optional: uses paths from macro if not specified)
129+
/// Output directory for generated files (optional: uses paths from macro if not specified)
128130
#[arg(long)]
129131
output: Option<String>,
130132
/// Exclude patterns (can be specified multiple times). Supports glob patterns.
131133
/// Example: --exclude "tests/*" --exclude "examples/*"
132134
#[arg(long, action = clap::ArgAction::Append)]
133135
exclude: Vec<String>,
136+
/// What to generate: schemas (default), instances, or all
137+
#[arg(long, default_value = "schemas")]
138+
mode: GenerateMode,
134139
},
135140
}
141+
142+
/// Controls what `generate-from-rust` generates.
143+
#[derive(clap::ValueEnum, Clone, Debug, PartialEq, Eq)]
144+
pub enum GenerateMode {
145+
/// Generate JSON schemas from `#[struct_to_gts_schema]` annotations (default)
146+
Schemas,
147+
/// Generate well-known instance JSON files from `#[gts_well_known_instance]` annotations
148+
Instances,
149+
/// Generate both schemas and instances
150+
All,
151+
}
136152
/// Run the CLI application
137153
///
138154
/// # Errors
@@ -259,9 +275,19 @@ async fn run_command(cli: Cli) -> Result<()> {
259275
source,
260276
output,
261277
exclude,
262-
} => {
263-
generate_schemas_from_rust(&source, output.as_deref(), &exclude, cli.verbose)?;
264-
}
278+
mode,
279+
} => match mode {
280+
GenerateMode::Schemas => {
281+
generate_schemas_from_rust(&source, output.as_deref(), &exclude, cli.verbose)?;
282+
}
283+
GenerateMode::Instances => {
284+
generate_instances_from_rust(&source, output.as_deref(), &exclude, cli.verbose)?;
285+
}
286+
GenerateMode::All => {
287+
generate_schemas_from_rust(&source, output.as_deref(), &exclude, cli.verbose)?;
288+
generate_instances_from_rust(&source, output.as_deref(), &exclude, cli.verbose)?;
289+
}
290+
},
265291
}
266292

267293
Ok(())
@@ -357,10 +383,12 @@ mod tests {
357383
source,
358384
output,
359385
exclude,
386+
mode,
360387
} => {
361388
assert_eq!(source, "/src/path");
362389
assert_eq!(output, Some("/out/path".to_owned()));
363390
assert_eq!(exclude, vec!["tests/*", "examples/*"]);
391+
assert_eq!(mode, GenerateMode::Schemas);
364392
}
365393
_ => panic!("Expected GenerateFromRust command"),
366394
}
@@ -639,10 +667,12 @@ mod tests {
639667
source,
640668
output,
641669
exclude,
670+
mode,
642671
} => {
643672
assert_eq!(source, "/src/path");
644673
assert_eq!(output, None);
645674
assert!(exclude.is_empty());
675+
assert_eq!(mode, GenerateMode::Schemas);
646676
}
647677
_ => panic!("Expected GenerateFromRust command"),
648678
}

0 commit comments

Comments
 (0)