Skip to content

Commit

Permalink
refactor: remove unused modules and update imports in cmd and ui dire…
Browse files Browse the repository at this point in the history
…ctories
  • Loading branch information
Kremilly committed Jul 11, 2024
1 parent 5a0a898 commit a68bc55
Show file tree
Hide file tree
Showing 16 changed files with 15 additions and 442 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ For more help and document, see our documentation:
- [Downloads Block](https://scibun.github.io/ScimonDocs/download-block.html)
- [Readme Block](https://scibun.github.io/ScimonDocs/readme-block.html)
- [Commands Block](https://scibun.github.io/ScimonDocs/commands-block.html) (Experimental)
- [Compress files](https://scibun.github.io/ScimonDocs/compress.html)
- [Open links](https://scibun.github.io/ScimonDocs/open-links.html)
- [Checksum and Checksum Validate](https://scibun.github.io/ScimonDocs/checsum.html)
- [Directives and Comments](https://scibun.github.io/ScimonDocs/directives.html)
- [Markdown render](https://scibun.github.io/ScimonDocs/markdown-render.html)
- [Scrape](https://scibun.github.io/ScimonDocs/scrape.html)
Expand All @@ -40,12 +38,6 @@ For more help and document, see our documentation:
path = "downloads/"
open = "https://github.com/kremilly"
readme = "https://gist.githubusercontent.com/Kremilly/5fd360d994bb0fe108b648d0e4c9e92f/raw/ac524eba2112bf0bdbac1ad27e24f78f678589ec/readme-example.md"
checksum = "https://gist.githubusercontent.com/kremilly/499d6d51d096c1813cea0eade8eb0bc4/raw/d7c5965aeaf005cf0b612e3468ab47c30480083b/scibun.sha256"
checksum.unmatch = "keep"
compress = "downloads.zip"
downloads {
https://arxiv.org/pdf/2405.01513
https://olacesar.com/e-books/protegido.pdf
Expand Down
92 changes: 0 additions & 92 deletions src/cmd/checksum.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod tasks;
pub mod monset;
pub mod checksum;
pub mod monset;
9 changes: 3 additions & 6 deletions src/cmd/monset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{

use crate::{
args_cli::Flags,
cmd::tasks::Tasks,
utils::validation::Validate,
ui::errors_alerts::ErrorsAlerts,

Expand All @@ -37,13 +36,11 @@ impl Monset {
Ok(Cursor::new(buffer.clone()))
}

pub async fn downloads(run: &str, flags: &Flags) -> Result<Vec<String>, Box<dyn Error>> {
pub async fn downloads(run: &str, flags: &Flags) -> Result<(), Box<dyn Error>> {
let mut reader = Self::read_file(run).await?;
let refs = DownloadsBlock::read_lines(&mut reader, flags, run).await?;
let _ = DownloadsBlock::read_lines(&mut reader, flags).await?;

Tasks::compress(run, &refs)?;

Ok(refs)
Ok(())
}

pub async fn run_code(run: &str) -> Result<(), Box<dyn Error>> {
Expand Down
63 changes: 1 addition & 62 deletions src/cmd/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
use is_url::is_url;

use zip::{
CompressionMethod,

write::{
FileOptions,
ExtendedFileOptions
},
};

use std::{
fs::File,
path::Path,
borrow::Cow,
error::Error,

io::{
Read,
Write,
Result as IoResult
},
};

use crate::{
args_cli::Flags,

monset::{
vars::Vars,
macros::Macros,
},
monset::macros::Macros,

ui::{
ui_base::UI,
errors_alerts::ErrorsAlerts,
success_alerts::SuccessAlerts,
compress_alerts::CompressAlerts,
},

system::{
Expand All @@ -50,44 +27,6 @@ pub struct Tasks;

impl Tasks {

pub fn compress(contents: &str, files: &Vec<String>) -> IoResult<()> {
if let Some(zip_file) = Vars::get_compress(contents) {
UI::section_header("Compressing files");
let folder_path = Vars::get_path(contents);

let output_path = Path::new(&zip_file);
let output_file = File::create(output_path)?;

let mut zip = zip::ZipWriter::new(output_file);
let options: FileOptions<ExtendedFileOptions> = FileOptions::default()
.compression_method(CompressionMethod::Deflated)
.unix_permissions(0o755);

for file in files {
let path = Path::new(file);
let name = path.strip_prefix(Path::new(&folder_path)).unwrap();
zip.start_file(name.to_str().unwrap(), options.clone())?;

let mut f = File::open(path)?;
let mut buffer = Vec::new();

f.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;

CompressAlerts::added(
&file.replace(
&format!("{}/", &folder_path), ""
), &zip_file
);
}

zip.finish()?;
CompressAlerts::completed(&zip_file);
}

Ok(())
}

pub async fn download(url: &str, path: &str, flags: &Flags) -> Result<String, Box<dyn Error>> {
let mut line_url = Cow::Borrowed(
url.trim()
Expand Down
27 changes: 5 additions & 22 deletions src/monset/blocks/downloads_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

use crate::{
args_cli::Flags,
cmd::tasks::Tasks,
utils::file::FileUtils,
system::providers::Providers,

Expand All @@ -15,11 +16,6 @@ use crate::{
macros_alerts::MacrosAlerts,
},

cmd::{
tasks::Tasks,
checksum::Checksum,
},

monset::{
vars::Vars,
macros::Macros,
Expand All @@ -31,9 +27,7 @@ pub struct DownloadsBlock;

impl DownloadsBlock {

pub async fn read_lines<R>(reader: R, flags: &Flags, checksum_file: &str) -> Result<Vec<String>, Box<dyn Error>> where R: BufRead {
let mut links = Vec::new();

pub async fn read_lines<R>(reader: R, flags: &Flags) -> Result<(), Box<dyn Error>> where R: BufRead {
let contents = reader.lines().collect::<Result<Vec<_>, _>>()?.join("\n");
let path = Vars::get_path(&contents);

Expand All @@ -46,13 +40,11 @@ impl DownloadsBlock {
let end_index = contents.rfind("}");

if let (Some(start_index), Some(end_index)) = (start_index, end_index) {
let mut refs = Vec::new();

FileUtils::create_path(&path);
let downloads_content = &contents[start_index + "downloads ".len()..end_index];

if downloads_content.trim().starts_with("commands {") {
return Ok(links);
return Ok(());
}

UI::section_header("downloads");
Expand All @@ -73,15 +65,11 @@ impl DownloadsBlock {

if !Macros::handle_check_macro_line(&line, "ignore") {
if !final_url.is_empty() && is_url(&final_url) && final_url.starts_with("http") {
let success = Tasks::download(
Tasks::download(
&url,
&path,
flags,
).await?;

refs.push(success);
let url_no_macros = Macros::remove_macros(&final_url);
links.push(url_no_macros.to_string());
}
} else {
MacrosAlerts::ignore(&final_url);
Expand All @@ -90,16 +78,11 @@ impl DownloadsBlock {

Vars::get_open(&contents, flags.no_open_link).await;
ReadMeBlock::render_var_and_save_file(&contents, flags).await?;

Checksum::generate_hashes(&path, checksum_file, &refs, flags).await?;
Checksum::compare_lines(&contents, checksum_file, flags).await?;

Tasks::compress(&contents, &refs)?;
} else {
eprintln!("'downloads' block not found in file.");
}

Ok(links)
Ok(())
}

}
30 changes: 0 additions & 30 deletions src/monset/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,5 @@ impl Vars {
None
}
}

pub fn get_compress(contents: &str) -> Option<String> {
let readme_pattern = Regex::new(BlocksRegExp::GET_COMPRESS_VAR).unwrap();

if let Some(caps) = readme_pattern.captures(&contents) {
caps.get(1).map(|m| m.as_str().to_string())
} else {
None
}
}

pub async fn get_checksum(contents: &str) -> Option<String> {
let readme_pattern = Regex::new(BlocksRegExp::GET_CHECKSUM_VAR).unwrap();

if let Some(caps) = readme_pattern.captures(&contents) {
caps.get(1).map(|m| m.as_str().to_string())
} else {
None
}
}

pub fn get_checksum_unmatch(contents: &str) -> Option<String> {
let readme_pattern = Regex::new(BlocksRegExp::GET_CHECKSUM_UNMATCH_ACTION).unwrap();

if let Some(caps) = readme_pattern.captures(&contents) {
caps.get(1).map(|m| m.as_str().to_string())
} else {
None
}
}

}
6 changes: 0 additions & 6 deletions src/regexp/regex_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,4 @@ impl BlocksRegExp {

pub const GET_README_VAR: &'static str = r#"(?i)readme\s*=\s*"([^"]+)""#;

pub const GET_COMPRESS_VAR: &'static str = r#"(?i)compress\s*=\s*"([^"]+)""#;

pub const GET_CHECKSUM_VAR: &'static str = r#"(?i)checksum\s*=\s*"([^"]+)""#;

pub const GET_CHECKSUM_UNMATCH_ACTION: &'static str = r#"(?i)checksum\.unmatch\s*=\s*"([^"]+)""#;

}
2 changes: 0 additions & 2 deletions src/regexp/regex_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ impl CoreRegExp {
pub const RENDER_EXTRA_GIST: &'static str = r#"\[!extra gist data=['"](.*?)['"]\]"#;
pub const RENDER_EXTRA_QRCODE: &'static str = r#"\[!extra qrcode data=['"](.*?)['"], size=(\d+)\]"#;

pub const GET_CHECKSUM: &'static str = r"(?i)\b([a-f0-9]{64})\b\s+(.+)";

pub const CLEAN_LINE: &'static str = r"\s*\{\s*\}\s*";

}
Loading

0 comments on commit a68bc55

Please sign in to comment.