1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package compliance
16
17import (
18	"fmt"
19)
20
21// LicenseCondition identifies a recognized license condition by setting the
22// corresponding bit.
23type LicenseCondition uint16
24
25// LicenseConditionMask is a bitmask for the recognized license conditions.
26const LicenseConditionMask = LicenseCondition(0x1ff)
27
28const (
29	// UnencumberedCondition identifies public domain or public domain-
30	// like license that disclaims copyright.
31	UnencumberedCondition = LicenseCondition(0x0001)
32	// PermissiveCondition identifies a license without notice or other
33	// significant requirements.
34	PermissiveCondition = LicenseCondition(0x0002)
35	// NoticeCondition identifies a typical open-source license with only
36	// notice or attribution requirements.
37	NoticeCondition = LicenseCondition(0x0004)
38	// ReciprocalCondition identifies a license with requirement to share
39	// the module's source only.
40	ReciprocalCondition = LicenseCondition(0x0008)
41	// RestrictedCondition identifies a license with requirement to share
42	// all source code linked to the module's source.
43	RestrictedCondition = LicenseCondition(0x0010)
44	// WeaklyRestrictedCondition identifies a RestrictedCondition waived
45	// for dynamic linking.
46	WeaklyRestrictedCondition = LicenseCondition(0x0020)
47	// ProprietaryCondition identifies a license with source privacy
48	// requirements.
49	ProprietaryCondition = LicenseCondition(0x0040)
50	// ByExceptionOnly identifies a license where policy requires product
51	// counsel review prior to use.
52	ByExceptionOnlyCondition = LicenseCondition(0x0080)
53	// NotAllowedCondition identifies a license with onerous conditions
54	// where policy prohibits use.
55	NotAllowedCondition = LicenseCondition(0x0100)
56)
57
58var (
59	// RecognizedConditionNames maps condition strings to LicenseCondition.
60	RecognizedConditionNames = map[string]LicenseCondition{
61		"unencumbered":                    UnencumberedCondition,
62		"permissive":                      PermissiveCondition,
63		"notice":                          NoticeCondition,
64		"reciprocal":                      ReciprocalCondition,
65		"restricted":                      RestrictedCondition,
66		"restricted_if_statically_linked": WeaklyRestrictedCondition,
67		"proprietary":                     ProprietaryCondition,
68		"by_exception_only":               ByExceptionOnlyCondition,
69		"not_allowed":                     NotAllowedCondition,
70	}
71)
72
73// Name returns the condition string corresponding to the LicenseCondition.
74func (lc LicenseCondition) Name() string {
75	switch lc {
76	case UnencumberedCondition:
77		return "unencumbered"
78	case PermissiveCondition:
79		return "permissive"
80	case NoticeCondition:
81		return "notice"
82	case ReciprocalCondition:
83		return "reciprocal"
84	case RestrictedCondition:
85		return "restricted"
86	case WeaklyRestrictedCondition:
87		return "restricted_if_statically_linked"
88	case ProprietaryCondition:
89		return "proprietary"
90	case ByExceptionOnlyCondition:
91		return "by_exception_only"
92	case NotAllowedCondition:
93		return "not_allowed"
94	}
95	panic(fmt.Errorf("unrecognized license condition: %#v", lc))
96}
97