-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resectioning sequences #35
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
441ecbc
Created initial 'it works' test
bookdude13 f97327b
Restructured into separate tests. 'is-working' tests complete?
bookdude13 d62314b
Restructured 'all working' test
bookdude13 33f9ef8
Added more tests. Reorganized again
bookdude13 9a29661
One more test
bookdude13 6d34109
Added test for seq sec editor added on perm change. Implemented; passing
bookdude13 6568a8b
All tests implemented except working and 0 sections
bookdude13 cf3ef58
Resectioning to 0 sections not allowed
bookdude13 ed38573
All tests passing
bookdude13 c93b3c5
Restructured so sequence not returned on resection
bookdude13 26485bc
Updated README
bookdude13 a23a9cf
Added debug message
bookdude13 fbb946b
Converted main code to new permissions api
bookdude13 1990790
Tests using new api. Removed check for sequence section editor. Some …
bookdude13 ed51ca4
All tests passing
bookdude13 66da0e5
Removed editor from sequence_section type
bookdude13 8aed7fb
Tests now want seq sec in range [0, numSections)
bookdude13 e23466b
SeqSec now 0-indexed. Removed last traces of editor field
bookdude13 b640012
Replaced loops with iterators
bookdude13 043d496
More iter().enumerate()
bookdude13 0e607d7
Updated README and fixed error in main
bookdude13 3e9d36f
Removed unnecessary println
bookdude13 6a31815
PR tweaks
bookdude13 61892e0
Added checks
bookdude13 fffeab0
Simplified getting all sections
bookdude13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,55 @@ | ||
|
||
use std::path::Path; | ||
use std::ascii::AsciiExt; | ||
|
||
use error::Error; | ||
use utils; | ||
|
||
|
||
#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)] | ||
pub enum PermissionEnum { | ||
pub enum Permission { | ||
Administrate, | ||
EditSeq, | ||
EditSeqSec, | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)] | ||
pub struct Permission { | ||
pub which: PermissionEnum, | ||
pub target: Option<String>, | ||
EditSeq(String), | ||
EditSeqSec(String, u32), | ||
} | ||
|
||
impl Permission { | ||
/// Creates a new Permission, joining a permission type with a target | ||
/// Returns an error if the target is invalid | ||
pub fn new(which_enum: PermissionEnum, t: Option<String>) -> Result<Permission, Error> { | ||
// Make sure the target is valid for the given permission type | ||
try!(Permission::validate_permission(&which_enum, &t)); | ||
|
||
// Create permission if valid | ||
Ok(Permission { | ||
which: which_enum, | ||
target: t, | ||
}) | ||
} | ||
|
||
/// Validates the target for the given permission type | ||
/// Returns error if invalid target | ||
fn validate_permission(permission: &PermissionEnum, target: &Option<String>) -> Result<(), Error> { | ||
|
||
let valid = match permission { | ||
&PermissionEnum::Administrate => { | ||
target == &None::<String> | ||
}, | ||
&PermissionEnum::EditSeq => { | ||
if target.is_none() { | ||
false | ||
/// Creates a new Permission | ||
/// Assumes target options are from Docopt, and are therefore safe to unwrap | ||
/// if the permission name is correct | ||
pub fn new( | ||
perm_name: &str, | ||
target_sequence: Option<String>, | ||
target_section: Option<u32> | ||
) -> Result<Permission, Error> { | ||
|
||
match perm_name.to_ascii_lowercase().as_ref() { | ||
"administrate" => Ok(Permission::Administrate), | ||
"editseq" => { | ||
let sequence_name = target_sequence.unwrap(); | ||
let project = try!(utils::read_protonfile(None::<&Path>)); | ||
if project.find_sequence_by_name(&sequence_name).is_none() { | ||
Err(Error::SequenceNotFound(sequence_name)) | ||
} else { | ||
let seq_name = target.to_owned().unwrap(); | ||
let project = try!(utils::read_protonfile(None::<&Path>)); | ||
project.find_sequence_by_name(&seq_name).is_some() | ||
Ok(Permission::EditSeq(sequence_name)) | ||
} | ||
}, | ||
&PermissionEnum::EditSeqSec => { | ||
if target.is_none() { | ||
false | ||
"editseqsec" => { | ||
let sequence_name = target_sequence.unwrap(); | ||
let section_idx = target_section.unwrap(); | ||
let project = try!(utils::read_protonfile(None::<&Path>)); | ||
let sequence_opt = project.find_sequence_by_name(&sequence_name); | ||
if sequence_opt.is_none() { | ||
Err(Error::SequenceNotFound(sequence_name)) | ||
} else { | ||
let target_str = target.to_owned().unwrap(); | ||
let targets: Vec<&str> = target_str.split(",").collect(); | ||
if targets.len() != 2 { | ||
println!("EditSeqSec target must be of the form \"name,section\""); | ||
false | ||
let sequence = sequence_opt.unwrap().to_owned(); | ||
if !sequence.section_in_range(section_idx) { | ||
Err(Error::InvalidSequenceSection(section_idx)) | ||
} else { | ||
let seq_name = targets[0]; | ||
let section_num_str = targets[1]; | ||
let section_num = match section_num_str.parse::<u32>() { | ||
Ok(n) => n, | ||
Err(_) => return Err(Error::InvalidPermissionTarget), | ||
}; | ||
let project = try!(utils::read_protonfile(None::<&Path>)); | ||
match project.find_sequence_by_name(&seq_name) { | ||
Some(seq) => { | ||
let in_range = section_num > 0 && section_num <= seq.num_sections; | ||
if !in_range { | ||
println!("EditSeqSec target must be of the form \"name,section\""); | ||
} | ||
in_range | ||
}, | ||
None => { | ||
println!("EditSeqSec target must be of the form \"name,section\""); | ||
false | ||
}, | ||
} | ||
|
||
Ok(Permission::EditSeqSec(sequence_name, section_idx)) | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
if valid { | ||
Ok(()) | ||
} else { | ||
Err(Error::InvalidPermissionTarget) | ||
_ => Err(Error::InvalidPermissionName(perm_name.to_owned())) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you reverse this if/else? Use is_some instead. Idk, it just feels nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to have the errors first. That way the error messages/types are right next to the check that triggered them. It's stylistic preference, so I think keep it like this unless you give me a better reason to switch it.