Skip to content

Commit

Permalink
Add option to write output to a file and fix minor TypeScript coding …
Browse files Browse the repository at this point in the history
…style & formatting issues. (#50)

* Add option to write output to a file and fix minor TypeScript coding style & formatting issues.

* Address comments.

* Replace if else block with match.

Co-authored-by: Yilong Wang <yilongw@amazon.com>
  • Loading branch information
yilong-wang and Yilong Wang authored Mar 18, 2022
1 parent af60628 commit 11461b1
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 79 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ json into beautiful typescript...until now.

Noctilucent will take your json and output the equivalent typescript.

## User Guide
```
cargo build --release
./target/release/noctilucent <INPUT> <OUTPUT>
```
* `INPUT` is the input file path.
* `OUTPUT` is the output file path; if not specified, output will be printed on your command line.

## Implemented

- [x] Fn::FindInMap
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ fn main() {
.required(true)
.index(1),
)
.arg(
Arg::new("OUTPUT")
.help("Sets the output file to use")
.required(false)
.index(2),
)
.get_matches();

let txt_location: &str = matches.value_of("INPUT").unwrap();
Expand All @@ -24,5 +30,11 @@ fn main() {

let cfn_tree = CloudformationParseTree::build(&value).unwrap();
let ir = CloudformationProgramIr::new_from_parse_tree(&cfn_tree).unwrap();
TypescriptSynthesizer::output(ir);
let output: String = TypescriptSynthesizer::output(ir);

if matches.is_present("OUTPUT") {
fs::write(matches.value_of("OUTPUT").unwrap(), output).expect("Unable to write file");
} else {
println!("{}", output);
}
}
6 changes: 3 additions & 3 deletions src/parser/lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ pub enum MappingInnerValue {
impl Display for MappingInnerValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
return match self {
MappingInnerValue::String(string_val) => write!(f, "\"{}\"", string_val),
MappingInnerValue::String(string_val) => write!(f, "'{}'", string_val),
MappingInnerValue::List(list_val) => {
let quoted_list_values: Vec<String> =
list_val.iter().map(|val| format!("\"{}\"", val)).collect();
list_val.iter().map(|val| format!("'{}'", val)).collect();
write!(f, "[{}]", quoted_list_values.join(","))
}
};
Expand Down Expand Up @@ -123,7 +123,7 @@ fn convert_to_string_vector(
"List values for mappings must be a string. Found {:?}, for key {}",
inner_key, vector_val
),
})
});
}
};
string_vector.push(converted_val);
Expand Down
Loading

0 comments on commit 11461b1

Please sign in to comment.