package netsplit

import (
	"net"
)

// SetParent sets the net.IPNet for a Splitter.
func (b *BaseSplitter) SetParent(pfx net.IPNet) {

	b.network = &pfx

}

// MarshalText lets a BaseSplitter conform to an encoding.TextMarshaler.
func (b *BaseSplitter) MarshalText() (text []byte, err error) {

	if b == nil || b.network == nil {
		return
	}

	text = []byte(b.network.String())

	return
}

/*
UnmarshalText lets a BaseSplitter conform to an encoding.TextUnmarshaler.

This is a potentially lossy operation! Any host bits set in the prefix's address will be lost.
They will not be set if the output was originally generated by `subnetter`.
*/
func (b *BaseSplitter) UnmarshalText(text []byte) (err error) {

	var s string
	var n *net.IPNet

	if text == nil {
		return
	}
	s = string(text)

	if _, n, err = net.ParseCIDR(s); err != nil {
		return
	}

	*b = BaseSplitter{
		network: n,
	}

	return
}