-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathuser.rs
More file actions
209 lines (190 loc) · 6.74 KB
/
Copy pathuser.rs
File metadata and controls
209 lines (190 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::hash::Hash;
use std::marker::PhantomData;
use std::sync::Arc;
use allocative::Allocative;
use buck2_core::provider::id::ProviderId;
use display_container::fmt_keyed_container;
use dupe::Dupe;
use indexmap::map::RawEntryApiV1;
use serde::Serializer;
use starlark::any::ProvidesStaticType;
use starlark::coerce::Coerce;
use starlark::coerce::coerce;
use starlark::collections::Hashed;
use starlark::collections::StarlarkHasher;
use starlark::eval::Evaluator;
use starlark::eval::ParametersParser;
use starlark::typing::Ty;
use starlark::values::Demand;
use starlark::values::Freeze;
use starlark::values::FrozenRef;
use starlark::values::Heap;
use starlark::values::StarlarkValue;
use starlark::values::Trace;
use starlark::values::Value;
use starlark::values::ValueLike;
use starlark::values::starlark_value;
use crate::interpreter::rule_defs::provider::ProviderLike;
use crate::interpreter::rule_defs::provider::callable::UserProviderCallableData;
#[derive(Debug, buck2_error::Error)]
#[buck2(tag = Input)]
enum UserProviderError {
#[error("Value for parameter `{0}` mismatches type `{1}`: `{2}`")]
MismatchedType(String, Ty, String),
#[error("Required parameter `{0}` is missing")]
MissingParameter(String),
}
/// The result of calling the output of `provider()`. This is just a simple data structure of
/// either immediately available values or, later, `FutureValue` types that are resolved
/// asynchronously
#[derive(Debug, Clone, Coerce, Trace, Freeze, ProvidesStaticType, Allocative)]
#[repr(C)]
pub struct UserProviderGen<'v, V: ValueLike<'v>> {
pub(crate) callable: FrozenRef<'static, UserProviderCallableData>,
attributes: Box<[V]>,
_marker: PhantomData<&'v ()>,
}
starlark_complex_value!(pub UserProvider<'v>);
impl<'v, V: ValueLike<'v>> UserProviderGen<'v, V> {
fn iter_items(&self) -> impl Iterator<Item = (&str, V)> {
assert_eq!(self.callable.fields.len(), self.attributes.len());
self.callable
.fields
.keys()
.map(|s| s.as_str())
.zip(self.attributes.iter().copied())
}
}
impl<'v, V: ValueLike<'v>> Display for UserProviderGen<'v, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_keyed_container(
f,
&format!("{}(", self.callable.provider_id.name),
")",
"=",
self.iter_items(),
)
}
}
#[starlark_value(type = "Provider")]
impl<'v, V: ValueLike<'v>> StarlarkValue<'v> for UserProviderGen<'v, V>
where
Self: ProvidesStaticType<'v>,
{
fn dir_attr(&self) -> Vec<String> {
self.callable.fields.keys().cloned().collect()
}
fn get_attr(&self, attribute: &str, heap: &'v Heap) -> Option<Value<'v>> {
self.get_attr_hashed(Hashed::new(attribute), heap)
}
fn get_attr_hashed(&self, attribute: Hashed<&str>, _heap: &'v Heap) -> Option<Value<'v>> {
let index = self
.callable
.fields
.raw_entry_v1()
.index_from_hash(attribute.hash().promote(), |k| k == attribute.key())?;
Some(self.attributes[index].to_value())
}
fn equals(&self, other: Value<'v>) -> starlark::Result<bool> {
let this: &UserProvider = coerce(self);
let other: &UserProvider = match UserProvider::from_value(other) {
Some(other) => other,
None => return Ok(false),
};
if this.callable.provider_id != other.callable.provider_id {
return Ok(false);
}
if this.attributes.len() != other.attributes.len() {
// If provider ids are equal, then providers point to the same provider callable,
// and lengths should be equal. So this code is unreachable.
return Ok(false);
}
for ((k1, v1), (k2, v2)) in this.iter_items().zip(other.iter_items()) {
if k1 != k2 {
// If provider ids are equal, then providers point to the same provider callable,
// and keys should be equal. So this code is unreachable.
return Ok(false);
}
if !v1.equals(v2)? {
return Ok(false);
}
}
Ok(true)
}
fn write_hash(&self, hasher: &mut StarlarkHasher) -> starlark::Result<()> {
self.callable.provider_id.hash(hasher);
for (k, v) in self.iter_items() {
k.hash(hasher);
v.write_hash(hasher)?;
}
Ok(())
}
fn provide(&'v self, demand: &mut Demand<'_, 'v>) {
demand.provide_value::<&dyn ProviderLike>(self);
}
fn typechecker_ty(&self) -> Option<Ty> {
Some(self.callable.ty_provider.dupe())
}
}
impl<'v, V: ValueLike<'v>> serde::Serialize for UserProviderGen<'v, V> {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.collect_map(self.iter_items())
}
}
impl<'v, V: ValueLike<'v>> ProviderLike<'v> for UserProviderGen<'v, V> {
fn id(&self) -> &Arc<ProviderId> {
&self.callable.provider_id
}
fn items(&self) -> Vec<(&str, Value<'v>)> {
self.iter_items().map(|(k, v)| (k, v.to_value())).collect()
}
}
/// Creates instances of mutable `UserProvider`s; called from a `NativeFunction`
pub(crate) fn user_provider_creator<'v>(
callable: FrozenRef<'static, UserProviderCallableData>,
eval: &Evaluator<'v, '_, '_>,
param_parser: &mut ParametersParser<'v, '_>,
) -> buck2_error::Result<Value<'v>> {
let heap = eval.heap();
let values = callable
.fields
.iter()
.map(|(name, field)| match param_parser.next_opt()? {
Some(value) => {
if !field.ty.matches(value) {
return Err(UserProviderError::MismatchedType(
name.to_owned(),
field.ty.as_ty().dupe(),
value.to_repr(),
)
.into());
}
Ok(value)
}
None => match field.default {
Some(default) => Ok(default.to_value()),
None => Err(UserProviderError::MissingParameter(name.to_owned()).into()),
},
})
.collect::<buck2_error::Result<Box<[Value]>>>()?;
Ok(heap.alloc(UserProvider {
callable,
attributes: values,
_marker: PhantomData,
}))
}