Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Nov 8, 2024
1 parent a121570 commit f699957
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
14 changes: 3 additions & 11 deletions crates/cli/src/commands/submit.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
use anyhow::Result;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use std::env;
use std::fs;
use std::path::Path;
use webbrowser;

use crate::dir;

pub async fn submit_challenge() -> Result<()> {
// Get the current directory name as slug
let current_dir = env::current_dir()?;
let slug = current_dir
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| anyhow::anyhow!("Failed to get directory name"))?;
let slug = dir::get_current_dir()?;

// Read the code from ./src/lib.rs
let lib_path = Path::new("./src/lib.rs");
let code = fs::read_to_string(lib_path)?;

// Encode the code in base64
let encoded_code = BASE64_STANDARD.encode(&code);

// Construct the URL
let url = format!(
"https://www.rustfinity.com/practice/rust/challenges/{}/description?code={}",
slug, encoded_code
);

// Open the user's browser to the URL
webbrowser::open(&url)?;

Ok(())
Expand Down
24 changes: 24 additions & 0 deletions crates/cli/src/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::env;

pub fn get_current_dir() -> anyhow::Result<String> {
let current_dir = env::current_dir().unwrap();

let current_dir = current_dir
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| anyhow::anyhow!("Failed to get directory name"))?;

Ok(current_dir.to_string())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_current_dir() {
let current_dir = get_current_dir();

assert_eq!(current_dir.unwrap(), "cli")
}
}
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod cli;
mod commands;
mod constants;
mod crates_io;
mod dir;
mod download;
mod editor;

Expand Down

0 comments on commit f699957

Please sign in to comment.