Skip to content

Commit

Permalink
Include Crystal::System::Group instead of extending it (#14930)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Aug 23, 2024
1 parent c462cd6 commit d4fc67a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/crystal/system/group.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module Crystal::System::Group
# def system_name : String

# def system_id : String

# def self.from_name?(groupname : String) : ::System::Group?

# def self.from_id?(groupid : String) : ::System::Group?
end

{% if flag?(:wasi) %}
require "./wasi/group"
{% elsif flag?(:unix) %}
Expand Down
19 changes: 15 additions & 4 deletions src/crystal/system/unix/group.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ require "../unix"
module Crystal::System::Group
private GETGR_R_SIZE_MAX = 1024 * 16

private def from_struct(grp)
new(String.new(grp.gr_name), grp.gr_gid.to_s)
def initialize(@name : String, @id : String)
end

private def from_name?(groupname : String)
def system_name
@name
end

def system_id
@id
end

private def self.from_struct(grp)
::System::Group.new(String.new(grp.gr_name), grp.gr_gid.to_s)
end

def self.from_name?(groupname : String)
groupname.check_no_null_byte

grp = uninitialized LibC::Group
Expand All @@ -21,7 +32,7 @@ module Crystal::System::Group
end
end

private def from_id?(groupid : String)
def self.from_id?(groupid : String)
groupid = groupid.to_u32?
return unless groupid

Expand Down
16 changes: 12 additions & 4 deletions src/crystal/system/wasi/group.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
module Crystal::System::Group
private def from_name?(groupname : String)
raise NotImplementedError.new("Crystal::System::Group#from_name?")
def system_name
raise NotImplementedError.new("Crystal::System::Group#system_name")
end

private def from_id?(groupid : String)
raise NotImplementedError.new("Crystal::System::Group#from_id?")
def system_id
raise NotImplementedError.new("Crystal::System::Group#system_id")
end

def self.from_name?(groupname : String)
raise NotImplementedError.new("Crystal::System::Group.from_name?")
end

def self.from_id?(groupid : String)
raise NotImplementedError.new("Crystal::System::Group.from_id?")
end
end
19 changes: 10 additions & 9 deletions src/system/group.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ class System::Group
class NotFoundError < Exception
end

extend Crystal::System::Group
include Crystal::System::Group

# The group's name.
getter name : String
def name : String
system_name
end

# The group's identifier.
getter id : String

def_equals_and_hash @id

private def initialize(@name, @id)
def id : String
system_id
end

def_equals_and_hash id

# Returns the group associated with the given name.
#
# Raises `NotFoundError` if no such group exists.
Expand All @@ -41,7 +42,7 @@ class System::Group
#
# Returns `nil` if no such group exists.
def self.find_by?(*, name : String) : System::Group?
from_name?(name)
Crystal::System::Group.from_name?(name)
end

# Returns the group associated with the given ID.
Expand All @@ -55,7 +56,7 @@ class System::Group
#
# Returns `nil` if no such group exists.
def self.find_by?(*, id : String) : System::Group?
from_id?(id)
Crystal::System::Group.from_id?(id)
end

def to_s(io)
Expand Down

0 comments on commit d4fc67a

Please sign in to comment.