001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.revetsec.scim;
017
018import org.jspecify.annotations.NonNull;
019
020import com.google.errorprone.annotations.CheckReturnValue;
021import javax.annotation.concurrent.Immutable;
022
023import static java.util.Objects.requireNonNull;
024
025/**
026 * The result of bounded SCIM filter parsing. Direct syntax parsing does not bind
027 * attributes or approve a policy; a provider's result includes those checks.
028 * Neither entry point authorizes the caller or executes a query. Results have
029 * identity equality and redacted diagnostics.
030 *
031 * @since 1.0.0
032 */
033@Immutable
034public sealed interface ScimFilterParseResult permits ScimFilterParseResult.Succeeded,
035        ScimFilterParseResult.Rejected {
036    /**
037     * An immutable filter. Direct parser results await schema and policy checks;
038     * provider results have passed the provider's configured checks.
039     *
040     * @since 1.0.0
041     */
042    @Immutable
043    final class Succeeded implements ScimFilterParseResult {
044        private final @NonNull ScimFilter filter;
045
046        Succeeded(@NonNull ScimFilter filter) { this.filter = requireNonNull(filter); }
047
048        /**
049         * Returns the parsed tree. A provider result has passed schema and filter-policy
050         * checks, but caller and tenant authorization remain application-owned.
051         *
052         * @return the filter
053         * @since 1.0.0
054         */
055        @CheckReturnValue
056        public @NonNull ScimFilter getFilter() { return this.filter; }
057
058        /**
059         * Returns a fixed description without filter content.
060         *
061         * @return redacted description
062         * @since 1.0.0
063         */
064        @Override
065        public @NonNull String toString() { return "ScimFilterParseResult.Succeeded{filter=<redacted>}"; }
066    }
067
068    /**
069     * Invalid syntax or a configured bound was exceeded; no partial tree is released.
070     *
071     * @since 1.0.0
072     */
073    @Immutable
074    final class Rejected implements ScimFilterParseResult {
075        private final @NonNull ScimError error;
076
077        Rejected(@NonNull ScimError error) { this.error = requireNonNull(error); }
078
079        /**
080         * Returns the fixed error and an optional expression offset.
081         *
082         * @return the error
083         * @since 1.0.0
084         */
085        @CheckReturnValue
086        public @NonNull ScimError getError() { return this.error; }
087
088        /**
089         * Returns a fixed description without filter content.
090         *
091         * @return redacted description
092         * @since 1.0.0
093         */
094        @Override
095        public @NonNull String toString() { return "ScimFilterParseResult.Rejected{error=<redacted>}"; }
096    }
097}