Skip to content

Commit

Permalink
Little improvements in debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicparga committed Sep 1, 2023
1 parent 1599336 commit 22ae54b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions .vscode/debugging.billo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions .vscode/greetings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/billo/target/debug/billo",
"args": [],
"args": ["run", "--config", "${workspaceFolder}/.vscode/debugging.billo.json"],
"cwd": "${workspaceFolder}"
}
// {
Expand Down
8 changes: 6 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
{
"id": "args",
"description": "List of arguments",
"default": "--help",
"type": "promptString"
"default": "run --config ${workspaceFolder}/.vscode/debugging.billo.json",
"type": "pickString",
"options": [
"--help",
"run --config ${workspaceFolder}/.vscode/debugging.billo.json"
]
}
]
}
34 changes: 34 additions & 0 deletions billo/lib/err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{
fmt::{self, Display},
io, result,
};

pub type Feedback = result::Result<(), Msg>;
pub type Result<T> = result::Result<T, Msg>;

#[derive(Debug)]
pub struct Msg(String);

impl Display for Msg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl From<io::Error> for Msg {
fn from(e: io::Error) -> Msg {
Msg(format!("{}", e))
}
}

impl From<String> for Msg {
fn from(s: String) -> Msg {
Msg(s)
}
}

impl From<&str> for Msg {
fn from(s: &str) -> Msg {
Msg(s.to_owned())
}
}
1 change: 1 addition & 0 deletions billo/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod err;
pub mod running;
34 changes: 27 additions & 7 deletions billo/lib/running.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
use crate::err;
use std::fmt::Write as _;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

fn eval_input(config_filepathbuf: &PathBuf) -> err::Feedback {
let mut err_msg: String = String::new();

if !config_filepathbuf.is_file() {
writeln!(
err_msg,
"Expected config as file, but dir is provided: {}",
config_filepathbuf.to_string_lossy()
)
.unwrap();
}

if err_msg.is_empty() {
Ok(())
} else {
Err(err::Msg::from(err_msg))
}
}

pub fn run(config_filepathbuf: PathBuf) {
let config_dirpathbuf: &Path = {
if config_filepathbuf.is_file() {
config_filepathbuf.parent().expect("Expected a parent")
} else {
&config_filepathbuf
}
};
if let Err(err) = eval_input(&config_filepathbuf) {
println!("{}", err);
std::process::exit(1)
}

let config_dirpathbuf: &Path = config_filepathbuf.parent().expect("Expected a parent");

fs::write(config_dirpathbuf.join("greetings.txt"), "Hello world!")
.expect("Unable to write file");
Expand Down

0 comments on commit 22ae54b

Please sign in to comment.