convex_typegen/config.rs
1//! Build-time inputs for the generator.
2//!
3//! Paths are resolved like Cargo does for `build.rs`: relative paths are relative to the **consumer**
4//! package root (the crate that depends on `convex-typegen`), not this library’s source tree.
5
6use std::path::PathBuf;
7
8/// Paths and overrides for one codegen run (typically built in `build.rs`).
9#[derive(Debug, Clone)]
10pub struct Configuration
11{
12 /// Path to the Convex schema file (default: "convex/schema.ts")
13 pub schema_path: PathBuf,
14
15 /// Output file path for generated Rust types (default: "src/convex_types.rs")
16 pub out_file: PathBuf,
17
18 /// Convex backend directory (default: `"convex"`, i.e. next to `Cargo.toml` when the build
19 /// runs from the package root). When [`function_paths`](Self::function_paths) is empty, all
20 /// `*.ts` files under this directory are used as function sources except `schema.ts` (same
21 /// file as [`schema_path`](Self::schema_path)), `_generated/`, `node_modules/`, and `*.d.ts`.
22 pub convex_dir: PathBuf,
23
24 /// When non-empty, only these files are parsed as Convex functions (directory discovery is
25 /// skipped). Use this in tests or for unusual layouts.
26 pub function_paths: Vec<PathBuf>,
27}
28
29impl Default for Configuration
30{
31 fn default() -> Self
32 {
33 Self {
34 schema_path: PathBuf::from("convex/schema.ts"),
35 out_file: PathBuf::from("src/convex_types.rs"),
36 convex_dir: PathBuf::from("convex"),
37 function_paths: Vec::new(),
38 }
39 }
40}
41
42#[cfg(test)]
43mod default_tests
44{
45 use super::*;
46
47 #[test]
48 fn default_paths_match_convex_layout_convention()
49 {
50 let c = Configuration::default();
51 assert_eq!(c.schema_path, PathBuf::from("convex/schema.ts"));
52 assert_eq!(c.out_file, PathBuf::from("src/convex_types.rs"));
53 assert_eq!(c.convex_dir, PathBuf::from("convex"));
54 assert!(c.function_paths.is_empty());
55 }
56
57 #[test]
58 fn configuration_clone_roundtrip()
59 {
60 let c = Configuration {
61 schema_path: PathBuf::from("a/schema.ts"),
62 out_file: PathBuf::from("b/out.rs"),
63 convex_dir: PathBuf::from("c"),
64 function_paths: vec![PathBuf::from("d/e.ts")],
65 };
66 assert_eq!(c.clone().schema_path, c.schema_path);
67 assert_eq!(c.clone().function_paths, c.function_paths);
68 }
69}