summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Gimbel <hallo@kevingimbel.com>2019-11-19 11:22:19 +0100
committerKevin Gimbel <hallo@kevingimbel.com>2019-11-19 11:22:19 +0100
commit901fa5a9d1ad87376154ab39e3e132f463282832 (patch)
treed2033e3565aa690538165a606bba6a2eedcd06aa
parentc176917dc837470584581864ce0893b85f215907 (diff)
Split into lib and bin
This commit splits the code into lib and bin so that the libary can be used by others as well.
-rw-r--r--Cargo.toml8
-rw-r--r--src/bin.rs24
-rw-r--r--src/lib.rs (renamed from src/main.rs)15
3 files changed, 36 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e282305..ac8580a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,14 @@ version = "1.0.0"
authors = ["Kevin Gimbel <hallo@kevingimbel.com>"]
edition = "2018"
+[lib]
+name = "mktoc"
+path = "src/lib.rs"
+
+[[bin]]
+name = "mktoc"
+path = "src/bin.rs"
+
[dependencies]
structopt = "0.3"
regex = "1.3.1" \ No newline at end of file
diff --git a/src/bin.rs b/src/bin.rs
new file mode 100644
index 0000000..7ab5cbc
--- /dev/null
+++ b/src/bin.rs
@@ -0,0 +1,24 @@
+use structopt::StructOpt;
+
+#[derive(StructOpt, Debug)]
+#[structopt(name = "mktoc")]
+struct Cli {
+ #[structopt()]
+ file: String,
+
+ #[structopt(long, short)]
+ write: bool,
+}
+
+fn main() -> std::io::Result<()> {
+ let opts = Cli::from_args();
+ let file = &opts.file.to_owned();
+ let res = mktoc::make_toc(file.to_string());
+ if opts.write {
+ std::fs::write(file, res.as_bytes())?;
+ } else {
+ println!("{}", res);
+ }
+
+ Ok(())
+}
diff --git a/src/main.rs b/src/lib.rs
index df5b123..16c99d2 100644
--- a/src/main.rs
+++ b/src/lib.rs
@@ -58,18 +58,11 @@ fn generate_toc(original_content: String) -> String {
return new_toc;
}
-fn main() -> std::io::Result<()> {
- let opts = Cli::from_args();
- let re_toc = regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc).*(END mktoc -->)").unwrap();
- let content = read_file(opts.file.to_owned()).unwrap();
+pub fn make_toc(file_path_in: String) -> String {
+ let content = read_file(file_path_in).unwrap();
let new_toc = generate_toc(content.to_owned());
+ let re_toc = regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc).*(END mktoc -->)").unwrap();
let res = re_toc.replace_all(content.as_str(), new_toc.as_str());
- if opts.write {
- std::fs::write(opts.file, res.as_bytes())?;
- } else {
- println!("{}", res);
- }
-
- Ok(())
+ return res.into_owned();
}