1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.pcc.chronicle.api.policy.builder
18 
19 /**
20  * A class to encapsulate policy checks. We use a string to capture the check, but we will make this
21  * more structured as the need arises.
22  */
23 data class PolicyCheck(val check: String) {
toStringnull24   override fun toString(): String = check
25 }
26 
27 /** The possible results of a call to check the adherence to a [Policy]: either [Pass] or [Fail]. */
28 sealed class PolicyCheckResult {
29   /** Denotes a successful policy check. */
30   object Pass : PolicyCheckResult()
31 
32   /** Denotes a failed policy check. */
33   data class Fail(val failingChecks: List<PolicyCheck>) : PolicyCheckResult() {
34     val message = failingChecks.toString()
35   }
36 
37   companion object {
38     /**
39      * A factory method to return the appropriate version of PolicyCheckResult based on whether the
40      * provided [violations] list is empty.
41      */
42     fun make(violations: List<PolicyCheck>): PolicyCheckResult {
43       return if (violations.isEmpty()) PolicyCheckResult.Pass
44       else PolicyCheckResult.Fail(violations)
45     }
46   }
47 }
48