shithub: mc

ref: 92840bc1ade4d670ce1b6cd3dc7be48b16148a5e
dir: /lib/std/dialparse+posixy.myr/

View raw version
use "alloc"
use "chartype"
use "hasprefix"
use "intparse"
use "ipparse"
use "option"
use "resolve"
use "result"
use "slcp"
use "sleq"
use "strfind"
use "striter"
use "traits"

pkg std =
	pkglocal const parseport	: (port : byte[:] -> option(uint16))
	pkglocal const getaddr		: (addr : byte[:] -> result(netaddr, byte[:]))
	pkglocal const nameseg		: (str : byte[:] -> option((byte[:], byte[:])))
;;

const parseport = {port
	match intparse(port)
	| `Some n:	-> `Some n
	| `None:
		/* a small number of hardcoded ports */
		if eq(port, "http")
			-> `Some 80
		elif eq(port, "https")
			-> `Some 443
		elif eq(port, "ircd")
			-> `Some 6667
		elif eq(port, "dns")
			-> `Some 53
		;;
	;;
	-> `None
}

const getaddr = {addr
	var ip

	match ipparse(addr)
	| `Some a:
		ip = a
	| `None:
		match resolve(addr)
		| `Ok hi:
			if hi.len > 0
				ip = hi[0].addr
				slfree(hi)
			else
				slfree(hi)
				-> `Err "no hosts resolved for addr"
			;;
		| `Err m:
			-> `Err "could not get host info"
		;;
	;;
	-> `Ok ip
}

const nameseg = {str
	match strfind(str, "!")
	| `Some idx:
		-> `Some (str[:idx], str[idx+1:])
	| `None:
		-> `None
	;;
}