-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proof-of-concept of type overrides using the CLI #334
base: cli
Are you sure you want to change the base?
Changes from 1 commit
4fc25b5
ff49be6
2faf4be
633a11e
2b88340
75e72f2
b9db690
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use clap::Parser; | ||
use color_eyre::Result; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
use std::path::{Path, PathBuf}; | ||
use color_eyre::eyre::bail; | ||
|
||
#[derive(Parser, Debug)] | ||
#[allow(clippy::struct_excessive_bools)] | ||
pub struct Args { | ||
/// Defines where your TS bindings will be saved by setting TS_RS_EXPORT_DIR | ||
#[arg(long, short)] | ||
pub output_directory: PathBuf, | ||
|
||
/// Disables warnings caused by using serde attributes that ts-rs cannot process | ||
#[arg(long)] | ||
pub no_warnings: bool, | ||
|
||
/// Adds the ".js" extension to import paths | ||
#[arg(long)] | ||
pub esm_imports: bool, | ||
|
||
/// Formats the generated TypeScript files | ||
#[arg(long)] | ||
pub format: bool, | ||
|
||
/// Generates an index.ts file in your --output-directory that re-exports all | ||
/// types generated by ts-rs | ||
#[arg(long = "index")] | ||
pub generate_index_ts: bool, | ||
|
||
/// Generates only a single index.ts file in your --output-directory that | ||
/// contains all exported types | ||
#[arg(long = "merge")] | ||
pub merge_files: bool, | ||
|
||
/// Do not capture `cargo test`'s output, and pass --nocapture to the test binary | ||
#[arg(long = "nocapture")] | ||
pub no_capture: bool, | ||
} | ||
|
||
// keeping this separate from `Args` for now :shrug: | ||
#[derive(Default, Deserialize)] | ||
#[serde(deny_unknown_fields, default)] | ||
pub struct Config { | ||
// type overrides for types implemented inside ts-rs. | ||
pub overrides: HashMap<String, String>, | ||
pub output_directory: PathBuf, | ||
pub no_warnings: bool, | ||
pub esm_imports: bool, | ||
pub format: bool, | ||
pub generate_index_ts: bool, | ||
pub merge_files: bool, | ||
pub no_capture: bool, | ||
} | ||
|
||
impl Config { | ||
pub fn load() -> Result<Self> { | ||
let mut cfg = Self::load_from_file()?; | ||
cfg.merge(Args::parse()); | ||
cfg.verify()?; | ||
Ok(cfg) | ||
} | ||
|
||
fn load_from_file() -> Result<Self> { | ||
// TODO: from where do we actually load the config? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there should be a CLI flag like |
||
let path = Path::new("./ts-rs.toml"); | ||
if !path.is_file() { | ||
return Ok(Self::default()); | ||
} | ||
let content = std::fs::read_to_string(path)?; | ||
Ok(toml::from_str::<Config>(&content)?) | ||
} | ||
|
||
fn verify(&self) -> Result<()> { | ||
if self.merge_files && self.generate_index_ts { | ||
bail!("--index is not compatible with --merge"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn merge( | ||
&mut self, | ||
Args { | ||
output_directory, | ||
no_warnings, | ||
esm_imports, | ||
format, | ||
generate_index_ts, | ||
merge_files, | ||
no_capture, | ||
}: Args, | ||
) { | ||
self.output_directory = output_directory; | ||
self.no_warnings = no_warnings; | ||
self.esm_imports = esm_imports; | ||
self.format = format; | ||
self.generate_index_ts = generate_index_ts; | ||
self.merge_files = merge_files; | ||
self.no_capture = no_capture; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.