Skip to content

Commit 9b2e0da

Browse files
committed
cargo clippy && cargo fmt
1 parent 40edbc2 commit 9b2e0da

File tree

195 files changed

+2387
-3985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+2387
-3985
lines changed

engine/baml-lib/baml-core/src/configuration.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ pub struct Configuration {
1010
pub generators: Vec<Generator>,
1111
}
1212

13+
impl Default for Configuration {
14+
fn default() -> Self {
15+
Self::new()
16+
}
17+
}
18+
1319
impl Configuration {
1420
pub fn new() -> Self {
1521
Self { generators: vec![] }
@@ -54,7 +60,7 @@ impl CodegenGenerator {
5460
version "{}"
5561
}}"#,
5662
self.name,
57-
self.output_type.to_string(),
63+
self.output_type,
5864
self.output_dir.display(),
5965
self.version,
6066
)

engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub trait IRHelper {
5252
params: &BamlMap<String, BamlValue>,
5353
coerce_settings: ArgCoercer,
5454
) -> Result<BamlValue>;
55-
fn distribute_type<'a>(
56-
&'a self,
55+
fn distribute_type(
56+
&self,
5757
value: BamlValue,
5858
field_type: FieldType,
5959
) -> Result<BamlValueWithMeta<FieldType>>;
@@ -108,9 +108,10 @@ impl IRHelper for IntermediateRepr {
108108

109109
fn find_function<'a>(&'a self, function_name: &str) -> Result<FunctionWalker<'a>> {
110110
match self.walk_functions().find(|f| f.name() == function_name) {
111-
Some(f) => match f.item.elem {
112-
repr::Function { .. } => Ok(f),
113-
},
111+
Some(f) => {
112+
let repr::Function { .. } = f.item.elem;
113+
Ok(f)
114+
}
114115
None => {
115116
// Get best match.
116117
let functions = self.walk_functions().map(|f| f.name()).collect::<Vec<_>>();
@@ -207,8 +208,8 @@ impl IRHelper for IntermediateRepr {
207208
/// For some `BamlValue` with type `FieldType`, walk the structure of both the value
208209
/// and the type simultaneously, associating each node in the `BamlValue` with its
209210
/// `FieldType`.
210-
fn distribute_type<'a>(
211-
&'a self,
211+
fn distribute_type(
212+
&self,
212213
value: BamlValue,
213214
field_type: FieldType,
214215
) -> anyhow::Result<BamlValueWithMeta<FieldType>> {
@@ -225,7 +226,7 @@ impl IRHelper for IntermediateRepr {
225226
anyhow::bail!("Could not unify String with {:?}", field_type)
226227
}
227228
BamlValue::Int(i)
228-
if FieldType::Literal(LiteralValue::Int(i.clone())).is_subtype_of(&field_type) =>
229+
if FieldType::Literal(LiteralValue::Int(i)).is_subtype_of(&field_type) =>
229230
{
230231
Ok(BamlValueWithMeta::Int(i, field_type))
231232
}
@@ -249,9 +250,9 @@ impl IRHelper for IntermediateRepr {
249250
let literal_type = FieldType::Literal(LiteralValue::Bool(b));
250251
let primitive_type = FieldType::Primitive(TypeValue::Bool);
251252

252-
if literal_type.is_subtype_of(&field_type) {
253-
Ok(BamlValueWithMeta::Bool(b, field_type))
254-
} else if primitive_type.is_subtype_of(&field_type) {
253+
if literal_type.is_subtype_of(&field_type)
254+
|| primitive_type.is_subtype_of(&field_type)
255+
{
255256
Ok(BamlValueWithMeta::Bool(b, field_type))
256257
} else {
257258
anyhow::bail!("Could not unify Bool with {:?}", field_type)
@@ -309,7 +310,7 @@ impl IRHelper for IntermediateRepr {
309310
BamlValue::List(items) => {
310311
let item_types = items
311312
.iter()
312-
.filter_map(|v| infer_type(v))
313+
.filter_map(infer_type)
313314
.dedup()
314315
.collect::<Vec<_>>();
315316
let maybe_item_type = match item_types.len() {
@@ -448,7 +449,7 @@ const UNIT_TYPE: FieldType = FieldType::Tuple(vec![]);
448449

449450
/// Derive the simplest type that can categorize a given value. This is meant to be used
450451
/// by `distribute_type`, for dynamic fields of classes, whose types are not known statically.
451-
pub fn infer_type<'a>(value: &'a BamlValue) -> Option<FieldType> {
452+
pub fn infer_type(value: &BamlValue) -> Option<FieldType> {
452453
let ret = match value {
453454
BamlValue::Int(_) => Some(FieldType::Primitive(TypeValue::Int)),
454455
BamlValue::Bool(_) => Some(FieldType::Primitive(TypeValue::Bool)),
@@ -472,7 +473,7 @@ pub fn infer_type<'a>(value: &'a BamlValue) -> Option<FieldType> {
472473
BamlValue::List(items) => {
473474
let item_tys = items
474475
.iter()
475-
.filter_map(|v| infer_type(v))
476+
.filter_map(infer_type)
476477
.dedup()
477478
.collect::<Vec<_>>();
478479
let item_ty = match item_tys.len() {

engine/baml-lib/baml-core/src/ir/ir_helpers/scope_diagnostics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ impl std::fmt::Display for ScopeStack {
9494
}
9595
}
9696

97+
impl Default for ScopeStack {
98+
fn default() -> Self {
99+
Self::new()
100+
}
101+
}
102+
97103
impl ScopeStack {
98104
pub fn new() -> Self {
99105
Self {

engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ArgCoercer {
8484
};
8585

8686
for key in kv.keys() {
87-
if !vec!["file", "media_type"].contains(&key.as_str()) {
87+
if !["file", "media_type"].contains(&key.as_str()) {
8888
scope.push_error(format!(
8989
"Invalid property `{}` on file {}: `media_type` is the only supported property",
9090
key,
@@ -95,7 +95,7 @@ impl ArgCoercer {
9595
match self.span_path.as_ref() {
9696
Some(span_path) => {
9797
Ok(BamlValue::Media(baml_types::BamlMedia::file(
98-
media_type.clone(),
98+
*media_type,
9999
span_path.clone(),
100100
s.to_string(),
101101
mime_type,
@@ -118,7 +118,7 @@ impl ArgCoercer {
118118
None => None,
119119
};
120120
for key in kv.keys() {
121-
if !vec!["url", "media_type"].contains(&key.as_str()) {
121+
if !["url", "media_type"].contains(&key.as_str()) {
122122
scope.push_error(format!(
123123
"Invalid property `{}` on url {}: `media_type` is the only supported property",
124124
key,
@@ -127,7 +127,7 @@ impl ArgCoercer {
127127
}
128128
}
129129
Ok(BamlValue::Media(baml_types::BamlMedia::url(
130-
media_type.clone(),
130+
*media_type,
131131
s.to_string(),
132132
mime_type,
133133
)))
@@ -143,7 +143,7 @@ impl ArgCoercer {
143143
None => None,
144144
};
145145
for key in kv.keys() {
146-
if !vec!["base64", "media_type"].contains(&key.as_str()) {
146+
if !["base64", "media_type"].contains(&key.as_str()) {
147147
scope.push_error(format!(
148148
"Invalid property `{}` on base64 {}: `media_type` is the only supported property",
149149
key,
@@ -152,7 +152,7 @@ impl ArgCoercer {
152152
}
153153
}
154154
Ok(BamlValue::Media(baml_types::BamlMedia::base64(
155-
media_type.clone(),
155+
*media_type,
156156
s.to_string(),
157157
mime_type,
158158
)))
@@ -177,7 +177,7 @@ impl ArgCoercer {
177177
(FieldType::Enum(name), _) => match value {
178178
BamlValue::String(s) => {
179179
if let Ok(e) = ir.find_enum(name) {
180-
if e.walk_values().find(|v| v.item.elem.0 == *s).is_some() {
180+
if e.walk_values().any(|v| v.item.elem.0 == *s) {
181181
Ok(BamlValue::Enum(name.to_string(), s.to_string()))
182182
} else {
183183
scope.push_error(format!(
@@ -279,7 +279,7 @@ impl ArgCoercer {
279279
}
280280
},
281281
(FieldType::Tuple(_), _) => {
282-
scope.push_error(format!("Tuples are not yet supported"));
282+
scope.push_error("Tuples are not yet supported".to_string());
283283
Err(())
284284
}
285285
(FieldType::Map(k, v), _) => {
@@ -310,10 +310,8 @@ impl ArgCoercer {
310310
let mut scope = ScopeStack::new();
311311
if first_good_result.is_err() {
312312
let result = self.coerce_arg(ir, option, value, &mut scope);
313-
if !scope.has_errors() {
314-
if first_good_result.is_err() {
315-
first_good_result = result
316-
}
313+
if !scope.has_errors() && first_good_result.is_err() {
314+
first_good_result = result
317315
}
318316
}
319317
}
@@ -346,7 +344,6 @@ impl ArgCoercer {
346344
let search_for_failures_result = first_failing_assert_nested(ir, &value, field_type)
347345
.map_err(|e| {
348346
scope.push_error(format!("Failed to evaluate assert: {:?}", e));
349-
()
350347
})?;
351348
match search_for_failures_result {
352349
Some(Constraint {
@@ -378,7 +375,7 @@ fn first_failing_assert_nested<'a>(
378375
.filter_map(|c| {
379376
let constraint = c.clone();
380377
let baml_value: BamlValue = value_node.into();
381-
let result = evaluate_predicate(&&baml_value, &c.expression).map_err(|e| {
378+
let result = evaluate_predicate(&baml_value, &c.expression).map_err(|e| {
382379
anyhow::anyhow!(format!("Error evaluating constraint: {:?}", e))
383380
});
384381
match result {
@@ -395,8 +392,7 @@ fn first_failing_assert_nested<'a>(
395392
})
396393
.collect::<Vec<_>>()
397394
})
398-
.map(|x| x.into_iter())
399-
.flatten()
395+
.flat_map(|x| x.into_iter())
400396
.next();
401397
first_failure.transpose()
402398
}

engine/baml-lib/baml-core/src/ir/jinja_helpers.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ fn sum_filter(value: Vec<Value>) -> Value {
4040
if int_sum.is_none() && float_sum.is_none() {
4141
log::warn!("The `sum` jinja filter was run against non-numeric arguments")
4242
}
43-
int_sum.map_or(
44-
float_sum.map_or(Value::from(0), |float| Value::from(float)),
45-
|int| Value::from(int),
46-
)
43+
int_sum.map_or(float_sum.map_or(Value::from(0), Value::from), Value::from)
4744
}
4845

4946
/// Render a bare minijinaja expression with the given context.
@@ -68,7 +65,7 @@ pub fn evaluate_predicate(
6865
) -> Result<bool, anyhow::Error> {
6966
let ctx: HashMap<String, minijinja::Value> =
7067
HashMap::from([("this".to_string(), minijinja::Value::from_serialize(this))]);
71-
match render_expression(&predicate_expression, &ctx)?.as_ref() {
68+
match render_expression(predicate_expression, &ctx)?.as_ref() {
7269
"true" => Ok(true),
7370
"false" => Ok(false),
7471
_ => Err(anyhow::anyhow!("Predicate did not evaluate to a boolean")),
@@ -85,9 +82,11 @@ mod tests {
8582
let ctx = vec![
8683
(
8784
"a".to_string(),
88-
BamlValue::List(
89-
vec![BamlValue::Int(1), BamlValue::Int(2), BamlValue::Int(3)].into(),
90-
)
85+
BamlValue::List(vec![
86+
BamlValue::Int(1),
87+
BamlValue::Int(2),
88+
BamlValue::Int(3),
89+
])
9190
.into(),
9291
),
9392
(
@@ -117,9 +116,11 @@ mod tests {
117116
let ctx = vec![
118117
(
119118
"a".to_string(),
120-
BamlValue::List(
121-
vec![BamlValue::Int(1), BamlValue::Int(2), BamlValue::Int(3)].into(),
122-
)
119+
BamlValue::List(vec![
120+
BamlValue::Int(1),
121+
BamlValue::Int(2),
122+
BamlValue::Int(3),
123+
])
123124
.into(),
124125
),
125126
(

engine/baml-lib/baml-core/src/ir/json_schema.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ impl WithJsonSchema for FunctionArgs {
7878
let mut required_props = vec![];
7979
for (name, t) in args.iter() {
8080
properties[name] = t.json_schema();
81-
match t {
82-
FieldType::Optional(_) => {
83-
required_props.push(name.clone());
84-
}
85-
_ => {}
81+
if let FieldType::Optional(_) = t {
82+
required_props.push(name.clone());
8683
}
8784
}
8885
json!({
@@ -153,7 +150,7 @@ impl WithJsonSchema for Walker<'_, &Class> {
153150
}
154151
}
155152

156-
impl<'db> WithJsonSchema for FieldType {
153+
impl WithJsonSchema for FieldType {
157154
fn json_schema(&self) -> serde_json::Value {
158155
match self {
159156
FieldType::Class(name) | FieldType::Enum(name) => json!({

0 commit comments

Comments
 (0)