-
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
Draft
NyxCode
wants to merge
7
commits into
cli
Choose a base branch
from
cli-overrides-poc
base: cli
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4fc25b5
proof-of-concept of type overrides using the CLI
NyxCode ff49be6
Fix clippy warnings
gustavo-shigueo 2faf4be
cargo fmt
gustavo-shigueo 633a11e
Allow user to provide custom path to config file
gustavo-shigueo 2b88340
Move impl Drop back into args
gustavo-shigueo 75e72f2
Merge branch 'cli' into cli-overrides-poc
gustavo-shigueo b9db690
Merge branch 'cli' into cli-overrides-poc
gustavo-shigueo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use std::{ | ||
collections::HashMap, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use clap::Parser; | ||
use color_eyre::{eyre::bail, owo_colors::OwoColorize, Result}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Parser, Debug)] | ||
#[allow(clippy::struct_excessive_bools)] | ||
pub struct Args { | ||
#[clap(skip)] | ||
pub overrides: HashMap<String, String>, | ||
|
||
/// Path to the `ts-rs` config file | ||
#[arg(long)] | ||
pub config: Option<PathBuf>, | ||
|
||
/// Defines where your TS bindings will be saved by setting `TS_RS_EXPORT_DIR` | ||
#[arg(long, short)] | ||
pub output_directory: Option<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, rename_all = "kebab-case")] | ||
#[allow(clippy::struct_excessive_bools)] | ||
pub struct Config { | ||
/// Type overrides for types implemented inside ts-rs. | ||
pub overrides: HashMap<String, String>, | ||
pub output_directory: Option<PathBuf>, | ||
pub no_warnings: bool, | ||
pub esm_imports: bool, | ||
pub format: bool, | ||
|
||
#[serde(rename = "index")] | ||
pub generate_index_ts: bool, | ||
|
||
#[serde(rename = "merge")] | ||
pub merge_files: bool, | ||
|
||
#[serde(rename = "nocapture")] | ||
pub no_capture: bool, | ||
} | ||
|
||
impl Args { | ||
pub fn load() -> Result<Self> { | ||
let mut args = Self::parse(); | ||
|
||
let cfg = Config::load_from_file(args.config.as_deref())?; | ||
|
||
args.merge(cfg); | ||
args.verify()?; | ||
|
||
Ok(args) | ||
} | ||
|
||
pub fn output_directory(&self) -> &Path { | ||
self.output_directory | ||
.as_deref() | ||
.expect("Output directory must not be `None`") | ||
} | ||
|
||
fn verify(&self) -> Result<()> { | ||
if self.merge_files && self.generate_index_ts { | ||
bail!( | ||
"{}: --index is not compatible with --merge", | ||
"Error".bold().red() | ||
); | ||
} | ||
|
||
if self.output_directory.is_none() { | ||
bail!("{}: You must provide the output diretory, either through the config file or the --output-directory flag", "Error".bold().red()) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn merge( | ||
&mut self, | ||
Config { | ||
overrides, | ||
output_directory, | ||
no_warnings, | ||
esm_imports, | ||
format, | ||
generate_index_ts, | ||
merge_files, | ||
no_capture, | ||
}: Config, | ||
) { | ||
// QUESTION: This gives the CLI flag priority over the config file's value, | ||
// is this the correct order? | ||
self.output_directory = output_directory.or_else(|| self.output_directory.clone()); | ||
|
||
self.overrides = overrides; | ||
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; | ||
} | ||
} | ||
|
||
impl Config { | ||
fn load_from_file(path: Option<&Path>) -> Result<Self> { | ||
if let Some(path) = path { | ||
if !path.is_file() { | ||
bail!("The provided path doesn't exist"); | ||
} | ||
|
||
let content = std::fs::read_to_string(path)?; | ||
return Ok(toml::from_str(&content)?); | ||
} | ||
|
||
// TODO: from where do we actually load the config? | ||
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(&content)?) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.