Skip to content

Commit

Permalink
split compiler and interpreter into their own folder
Browse files Browse the repository at this point in the history
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
soywod committed Aug 30, 2023
1 parent 9edd30d commit 4f3cd1e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/compl/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use clap::Parser;
use clap_complete::Shell;

/// Generates the completion script for the given shell.
/// Generate the completion script for the given shell.
#[derive(Parser, Debug)]
pub struct GenerateCompletionCommand {
pub shell: Shell,
Expand Down
21 changes: 7 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use env_logger::{Builder as LoggerBuilder, Env, DEFAULT_FILTER_ENV};

#[cfg(feature = "compiler")]
use crate::mml::args::CompileCommand;
#[cfg(feature = "interpreter")]
use crate::mml::args::InterpreterCommand;
use crate::{compl::args::GenerateCompletionCommand, man::args::GenerateManCommand};

#[derive(Parser, Debug)]
#[command(name= "mml", author, version, about, long_about = None, propagate_version = true)]
struct Cli {
Expand All @@ -21,12 +15,12 @@ struct Cli {

#[derive(Subcommand, Debug)]
enum Commands {
Completion(GenerateCompletionCommand),
Man(GenerateManCommand),
Completion(compl::args::GenerateCompletionCommand),
Man(man::args::GenerateManCommand),
#[cfg(feature = "compiler")]
Compile(CompileCommand),
Compile(mml::compiler::args::CompileCommand),
#[cfg(feature = "interpreter")]
Interpret(InterpreterCommand),
Interpret(mml::interpreter::args::InterpretCommand),
}

#[tokio::main]
Expand All @@ -36,13 +30,12 @@ async fn main() -> Result<()> {
.format_timestamp(None)
.init();

let cli = Cli::parse();
match cli.command {
match Cli::parse().command {
Commands::Completion(cmd) => compl::handlers::generate(Cli::command(), cmd.shell),
Commands::Man(cmd) => man::handlers::generate(cmd.dir, Cli::command()),
#[cfg(feature = "compiler")]
Commands::Compile(cmd) => mml::handlers::compile(cmd.mml()).await,
Commands::Compile(cmd) => mml::compiler::handlers::compile(cmd.mml()).await,
#[cfg(feature = "interpreter")]
Commands::Interpret(cmd) => mml::handlers::interpret(cmd.mime()).await,
Commands::Interpret(cmd) => mml::interpreter::handlers::interpret(cmd.mime()).await,
}
}
111 changes: 0 additions & 111 deletions src/mml/args.rs

This file was deleted.

40 changes: 40 additions & 0 deletions src/mml/compiler/args.rs
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)
}
12 changes: 12 additions & 0 deletions src/mml/compiler/handlers.rs
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(())
}
2 changes: 2 additions & 0 deletions src/mml/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod args;
pub mod handlers;
31 changes: 0 additions & 31 deletions src/mml/handlers.rs

This file was deleted.

39 changes: 39 additions & 0 deletions src/mml/interpreter/args.rs
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)
}
11 changes: 11 additions & 0 deletions src/mml/interpreter/handlers.rs
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(())
}
2 changes: 2 additions & 0 deletions src/mml/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod args;
pub mod handlers;
35 changes: 33 additions & 2 deletions src/mml/mod.rs
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
}

0 comments on commit 4f3cd1e

Please sign in to comment.