Skip to content

Commit

Permalink
Add Preamble Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy-van-Jerry committed Apr 4, 2024
1 parent a5cdc31 commit 25af867
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@
*.v
*.py
*.inst
!*_preamble.py

# macOS
.DS_Store
2 changes: 2 additions & 0 deletions examples/D1_preamble.py
@@ -0,0 +1,2 @@
if_rst = True
if_en = True
10 changes: 9 additions & 1 deletion src/config.rs
Expand Up @@ -83,6 +83,9 @@ struct Args {
/// Variables (multiple occurrences allowed)
#[arg(short, long = "var", value_name = "KEY=VAL")]
vars: Vec<String>,
/// Preamble Python file
#[arg(short, long = "preamble", value_name = "FILE", required = false)]
preamble_py: String
}

impl Config {
Expand Down Expand Up @@ -113,7 +116,7 @@ impl Config {
}

/// Parses the command line arguments and returns a tuple of `Config` and `FileOptions`.
pub fn from_args() -> (Config, FileOptions, Option<Vec<(String, String)>>) {
pub fn from_args() -> (Config, FileOptions, Option<Vec<(String, String)>>, Option<String>) {
let args = Args::parse();
let vars = args
.vars
Expand All @@ -137,6 +140,11 @@ impl Config {
output: args.output,
},
vars.ok(),
if args.preamble_py.is_empty() {
None
} else {
Some(args.preamble_py)
}
)
}

Expand Down
32 changes: 24 additions & 8 deletions src/convert.rs
Expand Up @@ -14,6 +14,7 @@ pub struct Convert {
config: Config,
file_options: FileOptions,
vars: Option<Vec<(String, String)>>,
preamble_py: Option<String>,
}

#[derive(Debug, PartialEq)]
Expand All @@ -32,18 +33,24 @@ impl Default for LineType {

impl Convert {
/// Creates a new `Convert` instance with the given configuration and file options.
pub fn new(config: Config, file_options: FileOptions, vars: Option<Vec<(String, String)>>) -> Convert {
pub fn new(
config: Config,
file_options: FileOptions,
vars: Option<Vec<(String, String)>>,
preamble_py: Option<String>,
) -> Convert {
Convert {
config,
file_options,
vars,
preamble_py,
}
}

/// Creates a new `Convert` instance by parsing command line arguments.
pub fn from_args() -> Convert {
let (config, file_options, vars) = Config::from_args();
Convert::new(config, file_options, vars)
let (config, file_options, vars, preamble_py) = Config::from_args();
Convert::new(config, file_options, vars, preamble_py)
}

/// Opens the input file and reads its contents as a string.
Expand Down Expand Up @@ -246,11 +253,19 @@ impl Convert {
#[cfg(feature = "inst")]
// print user-defined variables
if let Some(vars) = &self.vars {
writeln!(stream, "# User-defined variables:")?;
for (name, value) in vars {
writeln!(stream, "{} = {}", name, value)?;
if !vars.is_empty() {
writeln!(stream, "# User-defined variables:")?;
for (name, value) in vars {
writeln!(stream, "{} = {}", name, value)?;
}
writeln!(stream)?;
}
writeln!(stream)?;
}
// load preamble
if let Some(preamble_py) = &self.preamble_py {
// read from file and write to stream
let preamble_py = std::fs::read_to_string(preamble_py)?;
writeln!(stream, "# Preamble:\n{}", preamble_py)?;
}
writeln!(
stream,
Expand Down Expand Up @@ -303,7 +318,8 @@ impl Convert {
py_indent_prior, &line
))?;
}
py_indent_space = self.update_py_indent_space(&line, py_indent_space) - py_indent_prior;
py_indent_space =
self.update_py_indent_space(&line, py_indent_space) - py_indent_prior;
#[cfg(feature = "inst")]
self.process_python_line(
&line,
Expand Down

0 comments on commit 25af867

Please sign in to comment.