nim-lib/tests/test_object.nim

42 lines
1.0 KiB
Nim

type
PortState = enum Open, Closed
PortItem = object
port: int
state: PortState
# A simple container for PortItems
type
PortList = seq[PortItem]
# Add a new port to the list
proc addPort(portList: var PortList; newPort: PortItem) =
portList.add(newPort)
# Modify an existing port in the list
proc modifyPort(portList: var PortList; portNum: int; newState: PortState) =
for i in 0..<portList.len:
if portList[i].port == portNum:
portList[i].state = newState
return
echo "Port not found."
# Delete a port from the list
proc deletePort(portList: var PortList; portNum: int) =
for i in 0..<portList.len:
if portList[i].port == portNum:
portList.del(i)
return
echo "Port not found."
# Sample usage
var ports: PortList
addPort(ports, PortItem(port: 80, state: PortState.Open))
addPort(ports, PortItem(port: 443, state: PortState.Closed))
echo ports
modifyPort(ports, 80, PortState.Closed)
echo ports
deletePort(ports, 443)
echo ports