Skip to content
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

chore: introduce the Synthesizer trait #101

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clap::{Arg, Command};
use noctilucent::ir::CloudformationProgramIr;
use noctilucent::synthesizer::typescript_synthesizer::TypescriptSynthesizer;
use noctilucent::synthesizer::Synthesizer;
use noctilucent::CloudformationParseTree;
use serde_json::Value;
use std::fs;
use std::{fs, io};

fn main() {
let matches = Command::new("Translates cfn templates to cdk typescript")
Expand Down Expand Up @@ -45,11 +46,16 @@ fn main() {

let cfn_tree = CloudformationParseTree::build(&value).unwrap();
let ir = CloudformationProgramIr::new_from_parse_tree(&cfn_tree).unwrap();
let output: String = TypescriptSynthesizer::output(ir);
let synthesizer: &dyn Synthesizer = &TypescriptSynthesizer {};

if matches.is_present("OUTPUT") {
fs::write(matches.value_of("OUTPUT").unwrap(), output).expect("Unable to write file");
let mut output: Box<dyn io::Write> = if matches.is_present("OUTPUT") {
Box::new(
fs::File::create(matches.value_of("OUTPUT").unwrap()).expect("unable to create file"),
)
} else {
println!("{output}");
}
Box::new(io::stdout())
};

ir.synthesize(synthesizer, &mut output)
.expect("unable to synthesize");
}
15 changes: 15 additions & 0 deletions src/synthesizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
use std::io;

use crate::ir::CloudformationProgramIr;

pub mod typescript_synthesizer;

pub trait Synthesizer {
fn synthesize(&self, ir: CloudformationProgramIr, into: &mut dyn io::Write) -> io::Result<()>;
}

impl CloudformationProgramIr {
#[inline(always)]
pub fn synthesize(self, using: &dyn Synthesizer, into: &mut dyn io::Write) -> io::Result<()> {
using.synthesize(self, into)
}
}
Loading