<commit_before>/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import SwiftUI
import FirebaseFirestore

/// The strategy to use when an error occurs during mapping Firestore documents
/// to the target type of `FirestoreQuery`.
///
public enum DecodingFailureStrategy {
  /// Ignore any errors that occur when mapping Firestore documents.
  case ignore

  /// Raise an error when mapping a Firestore document fails.
  case raise
}

/// A property wrapper that listens to a Firestore collection.
///
/// In the following example, `FirestoreQuery` will fetch all documents from the
/// `fruits` collection, filtering only documents whose `isFavourite` attribute
/// is equal to `true`, map members of result set to the `Fruit` type, and make
/// them available via the wrapped value `fruits`.
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "fruits",
///         predicates: [.whereField("isFavourite", isEqualTo: true)]
///       ) var fruits: [Fruit]
///
///       var body: some View {
///         List(fruits) { fruit in
///           Text(fruit.name)
///         }
///       }
///     }
///
/// `FirestoreQuery` also supports returning a `Result` type. The `.success` case
/// returns an array of elements, whereas the `.failiure` case returns an error
/// in case mapping the Firestore docments wasn't successful:
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "fruits",
///         predicates: [.whereField("isFavourite", isEqualTo: true)]
///       ) var fruitResults: Result<[Fruit], Error>
///
///     var body: some View {
///       if case let .success(fruits) = fruitResults {
///         List(fruits) { fruit in
///           Text(fruit.name)
///         }
///       } else if case let .failure(error) = fruitResults {
///         Text("Couldn't map data: \(error.localizedDescription)")
///       }
///     }
///
/// Alternatively, the _projected value_ of the property wrapper provides access to
/// the `error` as well. This allows you to display a list of all successfully mapped
/// documents, as well as an error mesage with details about the documents that couldn't
/// be mapped successfully (e.g. because of a field name mismatch).
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "mappingFailure",
///         decodingFailureStrategy: .ignore
///       ) private var fruits: [Fruit]
///
///       var body: some View {
///         VStack(alignment: .leading) {
///           List(fruits) { fruit in
///             Text(fruit.name)
///           }
///           if $fruits.error != nil {<commit_msg>Fix doc typos in FirestoreQuery.swift (#8920)<commit_after>/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import SwiftUI
import FirebaseFirestore

/// The strategy to use when an error occurs during mapping Firestore documents
/// to the target type of `FirestoreQuery`.
///
public enum DecodingFailureStrategy {
  /// Ignore any errors that occur when mapping Firestore documents.
  case ignore

  /// Raise an error when mapping a Firestore document fails.
  case raise
}

/// A property wrapper that listens to a Firestore collection.
///
/// In the following example, `FirestoreQuery` will fetch all documents from the
/// `fruits` collection, filtering only documents whose `isFavourite` attribute
/// is equal to `true`, map members of result set to the `Fruit` type, and make
/// them available via the wrapped value `fruits`.
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "fruits",
///         predicates: [.whereField("isFavourite", isEqualTo: true)]
///       ) var fruits: [Fruit]
///
///       var body: some View {
///         List(fruits) { fruit in
///           Text(fruit.name)
///         }
///       }
///     }
///
/// `FirestoreQuery` also supports returning a `Result` type. The `.success` case
/// returns an array of elements, whereas the `.failure` case returns an error
/// in case mapping the Firestore docments wasn't successful:
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "fruits",
///         predicates: [.whereField("isFavourite", isEqualTo: true)]
///       ) var fruitResults: Result<[Fruit], Error>
///
///     var body: some View {
///       if case let .success(fruits) = fruitResults {
///         List(fruits) { fruit in
///           Text(fruit.name)
///         }
///       } else if case let .failure(error) = fruitResults {
///         Text("Couldn't map data: \(error.localizedDescription)")
///       }
///     }
///
/// Alternatively, the _projected value_ of the property wrapper provides access to
/// the `error` as well. This allows you to display a list of all successfully mapped
/// documents, as well as an error message with details about the documents that couldn't
/// be mapped successfully (e.g. because of a field name mismatch).
///
///     struct ContentView: View {
///       @FirestoreQuery(
///         collectionPath: "mappingFailure",
///         decodingFailureStrategy: .ignore
///       ) private var fruits: [Fruit]
///
///       var body: some View {
///         VStack(alignment: .leading) {
///           List(fruits) { fruit in
///             Text(fruit.name)
///           }
///           if $fruits.error != nil {
{'fraction_non_alphanumeric': 0.12565932361154203, 'fraction_numerical': 0.003102699348433137, 'mean_word_length': 2.3595622720166753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '978516', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1699, 'n_words': 691}