all: rename functions, deprecate old ones

Rename get_by_* to user_by_* and get_all to users. users() now can return errors.
This commit is contained in:
ge
2026-03-18 15:46:31 +03:00
parent 326d93714a
commit bdaaaaae58
3 changed files with 32 additions and 8 deletions

26
pwd.c.v
View File

@@ -76,7 +76,14 @@ fn make_error(name string, uid int) IError {
// get_by_uid returns the passwd database entry by user ID. // get_by_uid returns the passwd database entry by user ID.
// If the entry is not found, the EntryNotFoundError error will be returned. // If the entry is not found, the EntryNotFoundError error will be returned.
@[deprecated: 'use user_by_uid() instead']
pub fn get_by_uid(uid int) !Passwd { pub fn get_by_uid(uid int) !Passwd {
return user_by_uid(uid)!
}
// user_by_uid returns the passwd database entry by user ID.
// If the entry is not found, the EntryNotFoundError error will be returned.
pub fn user_by_uid(uid int) !Passwd {
pwd_mutex.lock() pwd_mutex.lock()
defer { defer {
pwd_mutex.unlock() pwd_mutex.unlock()
@@ -88,9 +95,16 @@ pub fn get_by_uid(uid int) !Passwd {
return make_passwd(pw) return make_passwd(pw)
} }
// get_by_uid returns the passwd database entry by user name. // get_by_name returns the passwd database entry by user name.
// If the entry is not found, the EntryNotFoundError error will be returned. // If the entry is not found, the EntryNotFoundError error will be returned.
@[deprecated: 'use user_by_name() instead']
pub fn get_by_name(name string) !Passwd { pub fn get_by_name(name string) !Passwd {
return user_by_name(name)!
}
// user_by_name returns the passwd database entry by user name.
// If the entry is not found, the EntryNotFoundError error will be returned.
pub fn user_by_name(name string) !Passwd {
pwd_mutex.lock() pwd_mutex.lock()
defer { defer {
pwd_mutex.unlock() pwd_mutex.unlock()
@@ -103,7 +117,13 @@ pub fn get_by_name(name string) !Passwd {
} }
// get_all returns all entries from passwd database in arbitrary order. // get_all returns all entries from passwd database in arbitrary order.
@[deprecated: 'use users() instead']
pub fn get_all() []Passwd { pub fn get_all() []Passwd {
return users() or { []Passwd{} }
}
// users returns all entries from passwd database in arbitrary order.
pub fn users() ![]Passwd {
mut pwds := []Passwd{} mut pwds := []Passwd{}
pwd_mutex.lock() pwd_mutex.lock()
C.setpwent() C.setpwent()
@@ -113,6 +133,10 @@ pub fn get_all() []Passwd {
} }
for { for {
pw := C.getpwent() pw := C.getpwent()
err := make_error('', -1)
if err.code() != 0 {
return err
}
if isnil(pw) { if isnil(pw) {
break break
} }

View File

@@ -1,17 +1,17 @@
// vtest build: !windows // vtest build: !windows
import pwd import pwd
fn test_get_by_uid() { fn test_user_by_uid() {
pw := pwd.get_by_uid(0)! pw := pwd.user_by_uid(0)!
assert pw.name == 'root' assert pw.name == 'root'
} }
fn test_get_by_name() { fn test_user_by_name() {
pw := pwd.get_by_name('root')! pw := pwd.user_by_name('root')!
assert pw.uid == 0 assert pw.uid == 0
} }
fn test_get_all() { fn test_users() {
pws := pwd.get_all() pws := pwd.users()!
assert pws.len > 0 assert pws.len > 0
} }

2
v.mod
View File

@@ -1,7 +1,7 @@
Module { Module {
name: 'pwd' name: 'pwd'
description: 'Access to the UNIX password database' description: 'Access to the UNIX password database'
version: '0.1.0' version: '0.2.0'
license: 'Unlicense' license: 'Unlicense'
dependencies: [] dependencies: []
} }