Skip to content

Commit

Permalink
fixed lock/unlock, & archive/unarchive CMD, & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Jul 11, 2024
1 parent 6f5dadd commit 6170236
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 371 deletions.
8 changes: 4 additions & 4 deletions src/calculator/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,29 +307,29 @@ impl Metadata {

// byte 5 is the file type byte
pub fn lock(&mut self) -> Result<(), String> {
self.bytes[5] = FileType::LockedProgram.to_byte();
self.bytes[4] = FileType::LockedProgram.to_byte();
self.file_type = FileType::LockedProgram;

Ok(())
}

pub fn unlock(&mut self) -> Result<(), String> {
self.bytes[5] = FileType::Program.to_byte();
self.bytes[4] = FileType::Program.to_byte();
self.file_type = FileType::Program;

Ok(())
}

// byte 15 is the archived byte
pub fn archive(&mut self) -> Result<(), String> {
self.bytes[15] = Archived::Archived.to_byte();
self.bytes[14] = Archived::Archived.to_byte();
self.archived = Archived::Archived;

Ok(())
}

pub fn unarchive(&mut self) -> Result<(), String> {
self.bytes[15] = Archived::NotArchived.to_byte();
self.bytes[14] = Archived::NotArchived.to_byte();
self.archived = Archived::NotArchived;

Ok(())
Expand Down
61 changes: 0 additions & 61 deletions src/commands/archive.rs

This file was deleted.

66 changes: 0 additions & 66 deletions src/commands/comment.rs

This file was deleted.

9 changes: 2 additions & 7 deletions src/commands/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ use crate::calculator::{DisplayMode, Model};
use crate::tokens::OsVersion;
use std::path::Path;

pub fn details_command(input_path_string: String, model: String) {
let model = match Model::from_string(&model) {
Ok(model) => model,
Err(err) => exit_with_error(&err),
};

pub fn details_command(input_path_string: String) {
let target_version = OsVersion {
model,
model: Model::Latest,
version: "latest".to_string(),
};

Expand Down
19 changes: 19 additions & 0 deletions src/commands/edit/archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn archive_command(input_path_string: String, new_file_path: Option<String>, delete_old: bool) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.metadata.archive();

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Archived program.");
}
24 changes: 24 additions & 0 deletions src/commands/edit/comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn comment_command(
input_path_string: String,
comment: String,
new_file_path: Option<String>,
delete_old: bool,
) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.header.comment(comment);

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Updated comment on program.");
}
19 changes: 19 additions & 0 deletions src/commands/edit/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn lock_command(input_path_string: String, new_file_path: Option<String>, delete_old: bool) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.metadata.lock();

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Locked program.");
}
35 changes: 19 additions & 16 deletions src/commands/unarchive.rs → src/commands/edit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
use super::exit_with_error;
pub mod archive;
pub mod comment;
pub mod lock;
pub mod rename;
pub mod unarchive;
pub mod unlock;

use crate::calculator::program::Program;
use crate::calculator::{DisplayMode, Model};
use crate::commands::exit_with_error;
use crate::tokens::OsVersion;
use std::path::Path;
use std::path::{Path, PathBuf};

pub fn unarchive_command(
input_path_string: String,
new_file_path: Option<String>,
delete_old: bool,
) {
fn load_program(input_path: &PathBuf) -> Program {
let target_version = OsVersion {
model: Model::Latest,
version: "latest".to_string(),
};

let input_path = Path::new(&input_path_string).to_path_buf();

let program = Program::load_from_8xp(
input_path.to_path_buf(),
target_version,
DisplayMode::Accessible,
);

let mut program = match program {
let program = match program {
Ok(program) => program,
Err(err) => exit_with_error(&err),
};

let result = program.metadata.unarchive();

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}
program
}

fn save_edits(
program: Program,
input_path: &PathBuf,
new_file_path: Option<String>,
delete_old: bool,
) {
if new_file_path.is_none() {
let result = program.save_to(input_path.to_path_buf());

Expand Down
24 changes: 24 additions & 0 deletions src/commands/edit/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn rename_command(
input_path_string: String,
name: String,
new_file_path: Option<String>,
delete_old: bool,
) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.metadata.rename(name);

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Renamed program.");
}
23 changes: 23 additions & 0 deletions src/commands/edit/unarchive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn unarchive_command(
input_path_string: String,
new_file_path: Option<String>,
delete_old: bool,
) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.metadata.unarchive();

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Un-archived program.");
}
19 changes: 19 additions & 0 deletions src/commands/edit/unlock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::{load_program, save_edits};
use crate::commands::exit_with_error;
use std::path::Path;

pub fn unlock_command(input_path_string: String, new_file_path: Option<String>, delete_old: bool) {
let input_path = Path::new(&input_path_string).to_path_buf();
let mut program = load_program(&input_path);

let result = program.metadata.unlock();

match result {
Ok(_) => {}
Err(err) => exit_with_error(&err),
}

save_edits(program, &input_path, new_file_path, delete_old);

println!("Unlocked program.");
}
Loading

0 comments on commit 6170236

Please sign in to comment.