package vars

import (
	
	
	
	

	
	
	
)

var (
	pathListSeparator = string(os.PathListSeparator)
	forbiddenInPath   = pathListSeparator + "\x00"
)

// Errors
var (
	ErrPathMustBeString           = errors.New("path must be string")
	ErrPathCannotContainColonZero = errors.New(`path cannot contain colon or \0`)
)

// NewEnvListVar returns a variable whose value is a list synchronized with an
// environment variable with the elements joined by os.PathListSeparator.
//
// Elements in the value of the variable must be strings, and cannot contain
// os.PathListSeparator or \0; attempting to put any in its elements will result in
// an error.
func ( string) Var {
	return &envListVar{envName: }
}

type envListVar struct {
	sync.RWMutex
	envName    string
	cacheFor   string
	cacheValue interface{}
}

// Get returns a Value for an EnvPathList.
func ( *envListVar) () interface{} {
	.Lock()
	defer .Unlock()

	 := os.Getenv(.envName)
	if  == .cacheFor {
		return .cacheValue
	}
	.cacheFor = 
	 := vector.Empty
	for ,  := range strings.Split(, pathListSeparator) {
		 = .Cons()
	}
	.cacheValue = 
	return .cacheValue
}

// Set sets an EnvPathList. The underlying environment variable is set.
func ( *envListVar) ( interface{}) error {
	var (
		      []string
		 error
	)
	 := vals.Iterate(, func( interface{}) bool {
		,  := .(string)
		if ! {
			 = ErrPathMustBeString
			return false
		}
		 := 
		if strings.ContainsAny(, forbiddenInPath) {
			 = ErrPathCannotContainColonZero
			return false
		}
		 = append(, )
		return true
	})

	if  != nil ||  != nil {
		return diag.Errors(, )
	}

	.Lock()
	defer .Unlock()
	os.Setenv(.envName, strings.Join(, pathListSeparator))
	return nil
}