Skip to content

Commit a58d2cf

Browse files
committed
refactor (#386)
1 parent f607797 commit a58d2cf

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

git-sec/src/lib.rs

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,63 +22,59 @@ pub mod identity {
2222
pub password: String,
2323
}
2424

25+
use std::borrow::Cow;
26+
use std::path::Path;
27+
28+
/// Obtain the owner of the given `path`.
29+
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
30+
impl_::from_path(path)
31+
}
32+
33+
/// Obtain the of the currently running process.
34+
pub fn from_process() -> Result<UserId, from_process::Error> {
35+
impl_::from_process()
36+
}
37+
2538
///
26-
pub mod user_id {
39+
pub mod from_process {
40+
use crate::identity::impl_;
41+
42+
/// The error returned by [from_process()][super::from_process()].
43+
pub type Error = impl_::FromProcessError;
44+
}
45+
46+
#[cfg(not(windows))]
47+
mod impl_ {
2748
use crate::identity::UserId;
2849
use std::borrow::Cow;
2950
use std::path::Path;
3051

31-
/// Obtain the owner of the given `path`.
3252
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
33-
impl_::from_path(path)
53+
use std::os::unix::fs::MetadataExt;
54+
let meta = std::fs::symlink_metadata(path)?;
55+
Ok(meta.uid())
3456
}
3557

36-
/// Obtain the of the currently running process.
37-
pub fn from_process() -> Result<UserId, from_process::Error> {
38-
impl_::from_process()
58+
pub type FromProcessError = std::convert::Infallible;
59+
pub fn from_process() -> Result<UserId, FromProcessError> {
60+
// SAFETY: there is no documented possibility for failure
61+
#[allow(unsafe_code)]
62+
let uid = unsafe { libc::geteuid() };
63+
Ok(uid)
3964
}
65+
}
4066

41-
///
42-
pub mod from_process {
43-
use crate::identity::user_id::impl_;
44-
45-
/// The error returned by [from_process()][super::from_process()].
46-
pub type Error = impl_::FromProcessError;
47-
}
48-
49-
#[cfg(not(windows))]
50-
mod impl_ {
51-
use crate::identity::UserId;
52-
use std::borrow::Cow;
53-
use std::path::Path;
54-
55-
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
56-
use std::os::unix::fs::MetadataExt;
57-
let meta = std::fs::symlink_metadata(path)?;
58-
Ok(meta.uid())
59-
}
67+
#[cfg(windows)]
68+
mod impl_ {
69+
use crate::identity::UserId;
70+
use std::borrow::Cow;
71+
use std::path::Path;
6072

61-
pub type FromProcessError = std::convert::Infallible;
62-
pub fn from_process() -> Result<UserId, FromProcessError> {
63-
// SAFETY: there is no documented possibility for failure
64-
#[allow(unsafe_code)]
65-
let uid = unsafe { libc::geteuid() };
66-
Ok(uid)
67-
}
73+
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
74+
todo!("unix")
6875
}
69-
70-
#[cfg(windows)]
71-
mod impl_ {
72-
use crate::identity::UserId;
73-
use std::borrow::Cow;
74-
use std::path::Path;
75-
76-
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
77-
todo!("unix")
78-
}
79-
pub fn from_process() -> std::io::Result<UserId> {
80-
todo!("process")
81-
}
76+
pub fn from_process() -> std::io::Result<UserId> {
77+
todo!("process")
8278
}
8379
}
8480
}

git-sec/tests/identity/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod uid {
22
#[test]
33
fn from_path() {
44
let dir = tempfile::tempdir().unwrap();
5-
let owner = git_sec::identity::user_id::from_path(dir.path().into()).unwrap();
6-
assert_eq!(owner, git_sec::identity::user_id::from_process().unwrap());
5+
let owner = git_sec::identity::from_path(dir.path().into()).unwrap();
6+
assert_eq!(owner, git_sec::identity::from_process().unwrap());
77
}
88
}

0 commit comments

Comments
 (0)