-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split compiler and interpreter into their own folder
This allows to minimize the number of #[cfg(feature = "…")] macros, this way they just need to be put at the mod.rs level.
- Loading branch information
Showing
11 changed files
with
147 additions
and
159 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use log::warn; | ||
use shellexpand_utils::try_shellexpand_path; | ||
use std::{fs, path::PathBuf}; | ||
|
||
use crate::mml::{format_stdin, format_str}; | ||
|
||
type MmlMessage = String; | ||
|
||
/// Compile the given MML message to a valid MIME message | ||
#[derive(Parser, Debug)] | ||
pub struct CompileCommand { | ||
/// Read the mssage from the given file path. | ||
#[arg(value_parser = parse_mml)] | ||
mml: Option<MmlMessage>, | ||
} | ||
|
||
impl CompileCommand { | ||
/// Return the command-line provided message or read one from stdin. | ||
pub fn mml(self) -> MmlMessage { | ||
match self.mml { | ||
Some(mml) => mml, | ||
None => format_stdin(), | ||
} | ||
} | ||
} | ||
|
||
fn parse_mml(raw: &str) -> Result<MmlMessage, String> { | ||
let mml = match try_shellexpand_path(raw) { | ||
Ok(path) => fs::read_to_string(PathBuf::from(path)).map_err(|e| e.to_string())?, | ||
Err(err) => { | ||
warn!("{err}"); | ||
warn!("invalid path, processing it as raw MML message"); | ||
format_str(raw) | ||
} | ||
}; | ||
|
||
Ok(mml) | ||
} |
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,12 @@ | ||
use anyhow::{Context, Result}; | ||
use mml::MmlCompiler; | ||
|
||
pub async fn compile(mml: String) -> Result<()> { | ||
let mime = MmlCompiler::new() | ||
.compile(mml) | ||
.await | ||
.context("cannot compile mml message")? | ||
.write_to_string()?; | ||
print!("{mime}"); | ||
Ok(()) | ||
} |
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,2 @@ | ||
pub mod args; | ||
pub mod handlers; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use log::warn; | ||
use shellexpand_utils::try_shellexpand_path; | ||
use std::{fs, path::PathBuf}; | ||
|
||
use crate::mml::{format_stdin, format_str}; | ||
|
||
type MimeMessage = String; | ||
|
||
/// Interpret the given MIME message as a MML message | ||
#[derive(Parser, Debug)] | ||
pub struct InterpretCommand { | ||
/// Read the mssage from the given file path. | ||
#[arg(value_parser = parse_mime)] | ||
mime: Option<MimeMessage>, | ||
} | ||
|
||
impl InterpretCommand { | ||
/// Return the command-line provided message or read one from stdin. | ||
pub fn mime(self) -> MimeMessage { | ||
match self.mime { | ||
Some(mime) => mime, | ||
None => format_stdin(), | ||
} | ||
} | ||
} | ||
|
||
fn parse_mime(raw: &str) -> Result<MimeMessage, String> { | ||
let mime = match try_shellexpand_path(raw) { | ||
Ok(path) => fs::read_to_string(PathBuf::from(path)).map_err(|e| e.to_string())?, | ||
Err(err) => { | ||
warn!("{err}"); | ||
warn!("invalid path, processing it as raw MIME message"); | ||
format_str(raw) | ||
} | ||
}; | ||
Ok(mime) | ||
} |
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,11 @@ | ||
use anyhow::{Context, Result}; | ||
use mml::MimeInterpreter; | ||
|
||
pub async fn interpret(mime: String) -> Result<()> { | ||
let mml = MimeInterpreter::new() | ||
.interpret_bytes(mime.as_bytes()) | ||
.await | ||
.context("cannot interpreter mime message")?; | ||
print!("{mml}"); | ||
Ok(()) | ||
} |
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,2 @@ | ||
pub mod args; | ||
pub mod handlers; |
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 |
---|---|---|
@@ -1,2 +1,33 @@ | ||
pub mod args; | ||
pub mod handlers; | ||
use std::io::{self, BufRead, BufReader}; | ||
|
||
#[cfg(feature = "compiler")] | ||
pub mod compiler; | ||
#[cfg(feature = "interpreter")] | ||
pub mod interpreter; | ||
|
||
pub(crate) fn format_str(input: &str) -> String { | ||
let input = input.replace("\\r", "").replace("\\n", "\n"); | ||
let mut lines = input.lines(); | ||
let mut output = String::new(); | ||
|
||
while let Some(ref line) = lines.next() { | ||
output.push_str(line); | ||
output.push('\r'); | ||
output.push('\n'); | ||
} | ||
|
||
output | ||
} | ||
|
||
pub(crate) fn format_stdin() -> String { | ||
let mut lines = BufReader::new(io::stdin()).lines(); | ||
let mut output = String::new(); | ||
|
||
while let Some(Ok(ref line)) = lines.next() { | ||
output.push_str(line); | ||
output.push('\r'); | ||
output.push('\n'); | ||
} | ||
|
||
output | ||
} |