aboutsummaryrefslogblamecommitdiff
path: root/strconv.go
blob: b34241dc0191f8b60c135c3f4b8ab0a9e1644fe5 (plain) (tree)
1
2
3
4
5
6
7





                 
                 




                                                
                                                            






                                                            








                                                                            
                                             



                               














                                                                                    
package koushin

import (
	"fmt"
	"strconv"
	"strings"
	"net/url"
)

func parseUid(s string) (uint32, error) {
	uid, err := strconv.ParseUint(s, 10, 32)
	if err != nil {
		return 0, fmt.Errorf("invalid UID: %v", err)
	}
	if uid == 0 {
		return 0, fmt.Errorf("UID must be non-zero")
	}
	return uint32(uid), nil
}

func parseMboxAndUid(mboxString, uidString string) (string, uint32, error) {
	mboxName, err := url.PathUnescape(mboxString)
	if err != nil {
		return "", 0, fmt.Errorf("invalid mailbox name: %v", err)
	}
	uid, err := parseUid(uidString)
	return mboxName, uid, err
}

func parsePartPath(s string) ([]int, error) {
	if s == "" {
		return nil, nil
	}

	l := strings.Split(s, ".")
	path := make([]int, len(l))
	for i, s := range l {
		var err error
		path[i], err = strconv.Atoi(s)
		if err != nil {
			return nil, err
		}

		if path[i] <= 0 {
			return nil, fmt.Errorf("part num must be strictly positive")
		}
	}
	return path, nil
}