Skip to content

Commit 0e3781f

Browse files
authored
Support turning a FileMode into a u32 (#647)
Sometimes it's helpful to have the underlying numeric mode values, rather than the FileMode enum. Provide a From instance to turn FileMode into a u32.
1 parent 1cc1ef6 commit 0e3781f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,19 @@ pub enum FileMode {
10491049
Commit,
10501050
}
10511051

1052+
impl From<FileMode> for u32 {
1053+
fn from(mode: FileMode) -> u32 {
1054+
match mode {
1055+
FileMode::Unreadable => raw::GIT_FILEMODE_UNREADABLE as u32,
1056+
FileMode::Tree => raw::GIT_FILEMODE_TREE as u32,
1057+
FileMode::Blob => raw::GIT_FILEMODE_BLOB as u32,
1058+
FileMode::BlobExecutable => raw::GIT_FILEMODE_BLOB_EXECUTABLE as u32,
1059+
FileMode::Link => raw::GIT_FILEMODE_LINK as u32,
1060+
FileMode::Commit => raw::GIT_FILEMODE_COMMIT as u32,
1061+
}
1062+
}
1063+
}
1064+
10521065
bitflags! {
10531066
/// Return codes for submodule status.
10541067
///
@@ -1441,12 +1454,18 @@ impl Default for ReferenceFormat {
14411454

14421455
#[cfg(test)]
14431456
mod tests {
1444-
use super::ObjectType;
1457+
use super::{FileMode, ObjectType};
14451458

14461459
#[test]
14471460
fn convert() {
14481461
assert_eq!(ObjectType::Blob.str(), "blob");
14491462
assert_eq!(ObjectType::from_str("blob"), Some(ObjectType::Blob));
14501463
assert!(ObjectType::Blob.is_loose());
14511464
}
1465+
1466+
#[test]
1467+
fn convert_filemode() {
1468+
assert_eq!(u32::from(FileMode::Blob), 0o100644);
1469+
assert_eq!(u32::from(FileMode::BlobExecutable), 0o100755);
1470+
}
14521471
}

0 commit comments

Comments
 (0)