Skip to content

Commit

Permalink
Nuke (#1206)
Browse files Browse the repository at this point in the history
- `cargo clippy --fix && cargo fmt`
- Fix `clippy` stuff manually if `--fix` doesn't.
- Delete jsonish `old` unused module.
  • Loading branch information
antoniosarosi authored Dec 2, 2024
1 parent 40edbc2 commit c17e0da
Show file tree
Hide file tree
Showing 195 changed files with 2,385 additions and 3,985 deletions.
8 changes: 7 additions & 1 deletion engine/baml-lib/baml-core/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct Configuration {
pub generators: Vec<Generator>,
}

impl Default for Configuration {
fn default() -> Self {
Self::new()
}
}

impl Configuration {
pub fn new() -> Self {
Self { generators: vec![] }
Expand Down Expand Up @@ -54,7 +60,7 @@ impl CodegenGenerator {
version "{}"
}}"#,
self.name,
self.output_type.to_string(),
self.output_type,
self.output_dir.display(),
self.version,
)
Expand Down
27 changes: 13 additions & 14 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub trait IRHelper {
params: &BamlMap<String, BamlValue>,
coerce_settings: ArgCoercer,
) -> Result<BamlValue>;
fn distribute_type<'a>(
&'a self,
fn distribute_type(
&self,
value: BamlValue,
field_type: FieldType,
) -> Result<BamlValueWithMeta<FieldType>>;
Expand Down Expand Up @@ -108,9 +108,8 @@ impl IRHelper for IntermediateRepr {

fn find_function<'a>(&'a self, function_name: &str) -> Result<FunctionWalker<'a>> {
match self.walk_functions().find(|f| f.name() == function_name) {
Some(f) => match f.item.elem {
repr::Function { .. } => Ok(f),
},
Some(f) => Ok(f),

None => {
// Get best match.
let functions = self.walk_functions().map(|f| f.name()).collect::<Vec<_>>();
Expand Down Expand Up @@ -207,8 +206,8 @@ impl IRHelper for IntermediateRepr {
/// For some `BamlValue` with type `FieldType`, walk the structure of both the value
/// and the type simultaneously, associating each node in the `BamlValue` with its
/// `FieldType`.
fn distribute_type<'a>(
&'a self,
fn distribute_type(
&self,
value: BamlValue,
field_type: FieldType,
) -> anyhow::Result<BamlValueWithMeta<FieldType>> {
Expand All @@ -225,7 +224,7 @@ impl IRHelper for IntermediateRepr {
anyhow::bail!("Could not unify String with {:?}", field_type)
}
BamlValue::Int(i)
if FieldType::Literal(LiteralValue::Int(i.clone())).is_subtype_of(&field_type) =>
if FieldType::Literal(LiteralValue::Int(i)).is_subtype_of(&field_type) =>
{
Ok(BamlValueWithMeta::Int(i, field_type))
}
Expand All @@ -249,9 +248,9 @@ impl IRHelper for IntermediateRepr {
let literal_type = FieldType::Literal(LiteralValue::Bool(b));
let primitive_type = FieldType::Primitive(TypeValue::Bool);

if literal_type.is_subtype_of(&field_type) {
Ok(BamlValueWithMeta::Bool(b, field_type))
} else if primitive_type.is_subtype_of(&field_type) {
if literal_type.is_subtype_of(&field_type)
|| primitive_type.is_subtype_of(&field_type)
{
Ok(BamlValueWithMeta::Bool(b, field_type))
} else {
anyhow::bail!("Could not unify Bool with {:?}", field_type)
Expand Down Expand Up @@ -309,7 +308,7 @@ impl IRHelper for IntermediateRepr {
BamlValue::List(items) => {
let item_types = items
.iter()
.filter_map(|v| infer_type(v))
.filter_map(infer_type)
.dedup()
.collect::<Vec<_>>();
let maybe_item_type = match item_types.len() {
Expand Down Expand Up @@ -448,7 +447,7 @@ const UNIT_TYPE: FieldType = FieldType::Tuple(vec![]);

/// Derive the simplest type that can categorize a given value. This is meant to be used
/// by `distribute_type`, for dynamic fields of classes, whose types are not known statically.
pub fn infer_type<'a>(value: &'a BamlValue) -> Option<FieldType> {
pub fn infer_type(value: &BamlValue) -> Option<FieldType> {
let ret = match value {
BamlValue::Int(_) => Some(FieldType::Primitive(TypeValue::Int)),
BamlValue::Bool(_) => Some(FieldType::Primitive(TypeValue::Bool)),
Expand All @@ -472,7 +471,7 @@ pub fn infer_type<'a>(value: &'a BamlValue) -> Option<FieldType> {
BamlValue::List(items) => {
let item_tys = items
.iter()
.filter_map(|v| infer_type(v))
.filter_map(infer_type)
.dedup()
.collect::<Vec<_>>();
let item_ty = match item_tys.len() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl std::fmt::Display for ScopeStack {
}
}

impl Default for ScopeStack {
fn default() -> Self {
Self::new()
}
}

impl ScopeStack {
pub fn new() -> Self {
Self {
Expand Down
28 changes: 12 additions & 16 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl ArgCoercer {
};

for key in kv.keys() {
if !vec!["file", "media_type"].contains(&key.as_str()) {
if !["file", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on file {}: `media_type` is the only supported property",
key,
Expand All @@ -95,7 +95,7 @@ impl ArgCoercer {
match self.span_path.as_ref() {
Some(span_path) => {
Ok(BamlValue::Media(baml_types::BamlMedia::file(
media_type.clone(),
*media_type,
span_path.clone(),
s.to_string(),
mime_type,
Expand All @@ -118,7 +118,7 @@ impl ArgCoercer {
None => None,
};
for key in kv.keys() {
if !vec!["url", "media_type"].contains(&key.as_str()) {
if !["url", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on url {}: `media_type` is the only supported property",
key,
Expand All @@ -127,7 +127,7 @@ impl ArgCoercer {
}
}
Ok(BamlValue::Media(baml_types::BamlMedia::url(
media_type.clone(),
*media_type,
s.to_string(),
mime_type,
)))
Expand All @@ -143,7 +143,7 @@ impl ArgCoercer {
None => None,
};
for key in kv.keys() {
if !vec!["base64", "media_type"].contains(&key.as_str()) {
if !["base64", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on base64 {}: `media_type` is the only supported property",
key,
Expand All @@ -152,7 +152,7 @@ impl ArgCoercer {
}
}
Ok(BamlValue::Media(baml_types::BamlMedia::base64(
media_type.clone(),
*media_type,
s.to_string(),
mime_type,
)))
Expand All @@ -177,7 +177,7 @@ impl ArgCoercer {
(FieldType::Enum(name), _) => match value {
BamlValue::String(s) => {
if let Ok(e) = ir.find_enum(name) {
if e.walk_values().find(|v| v.item.elem.0 == *s).is_some() {
if e.walk_values().any(|v| v.item.elem.0 == *s) {
Ok(BamlValue::Enum(name.to_string(), s.to_string()))
} else {
scope.push_error(format!(
Expand Down Expand Up @@ -279,7 +279,7 @@ impl ArgCoercer {
}
},
(FieldType::Tuple(_), _) => {
scope.push_error(format!("Tuples are not yet supported"));
scope.push_error("Tuples are not yet supported".to_string());
Err(())
}
(FieldType::Map(k, v), _) => {
Expand Down Expand Up @@ -310,10 +310,8 @@ impl ArgCoercer {
let mut scope = ScopeStack::new();
if first_good_result.is_err() {
let result = self.coerce_arg(ir, option, value, &mut scope);
if !scope.has_errors() {
if first_good_result.is_err() {
first_good_result = result
}
if !scope.has_errors() && first_good_result.is_err() {
first_good_result = result
}
}
}
Expand Down Expand Up @@ -346,7 +344,6 @@ impl ArgCoercer {
let search_for_failures_result = first_failing_assert_nested(ir, &value, field_type)
.map_err(|e| {
scope.push_error(format!("Failed to evaluate assert: {:?}", e));
()
})?;
match search_for_failures_result {
Some(Constraint {
Expand Down Expand Up @@ -378,7 +375,7 @@ fn first_failing_assert_nested<'a>(
.filter_map(|c| {
let constraint = c.clone();
let baml_value: BamlValue = value_node.into();
let result = evaluate_predicate(&&baml_value, &c.expression).map_err(|e| {
let result = evaluate_predicate(&baml_value, &c.expression).map_err(|e| {
anyhow::anyhow!(format!("Error evaluating constraint: {:?}", e))
});
match result {
Expand All @@ -395,8 +392,7 @@ fn first_failing_assert_nested<'a>(
})
.collect::<Vec<_>>()
})
.map(|x| x.into_iter())
.flatten()
.flat_map(|x| x.into_iter())
.next();
first_failure.transpose()
}
Expand Down
23 changes: 12 additions & 11 deletions engine/baml-lib/baml-core/src/ir/jinja_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ fn sum_filter(value: Vec<Value>) -> Value {
if int_sum.is_none() && float_sum.is_none() {
log::warn!("The `sum` jinja filter was run against non-numeric arguments")
}
int_sum.map_or(
float_sum.map_or(Value::from(0), |float| Value::from(float)),
|int| Value::from(int),
)
int_sum.map_or(float_sum.map_or(Value::from(0), Value::from), Value::from)
}

/// Render a bare minijinaja expression with the given context.
Expand All @@ -68,7 +65,7 @@ pub fn evaluate_predicate(
) -> Result<bool, anyhow::Error> {
let ctx: HashMap<String, minijinja::Value> =
HashMap::from([("this".to_string(), minijinja::Value::from_serialize(this))]);
match render_expression(&predicate_expression, &ctx)?.as_ref() {
match render_expression(predicate_expression, &ctx)?.as_ref() {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(anyhow::anyhow!("Predicate did not evaluate to a boolean")),
Expand All @@ -85,9 +82,11 @@ mod tests {
let ctx = vec![
(
"a".to_string(),
BamlValue::List(
vec![BamlValue::Int(1), BamlValue::Int(2), BamlValue::Int(3)].into(),
)
BamlValue::List(vec![
BamlValue::Int(1),
BamlValue::Int(2),
BamlValue::Int(3),
])
.into(),
),
(
Expand Down Expand Up @@ -117,9 +116,11 @@ mod tests {
let ctx = vec![
(
"a".to_string(),
BamlValue::List(
vec![BamlValue::Int(1), BamlValue::Int(2), BamlValue::Int(3)].into(),
)
BamlValue::List(vec![
BamlValue::Int(1),
BamlValue::Int(2),
BamlValue::Int(3),
])
.into(),
),
(
Expand Down
9 changes: 3 additions & 6 deletions engine/baml-lib/baml-core/src/ir/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ impl WithJsonSchema for FunctionArgs {
let mut required_props = vec![];
for (name, t) in args.iter() {
properties[name] = t.json_schema();
match t {
FieldType::Optional(_) => {
required_props.push(name.clone());
}
_ => {}
if let FieldType::Optional(_) = t {
required_props.push(name.clone());
}
}
json!({
Expand Down Expand Up @@ -153,7 +150,7 @@ impl WithJsonSchema for Walker<'_, &Class> {
}
}

impl<'db> WithJsonSchema for FieldType {
impl WithJsonSchema for FieldType {
fn json_schema(&self) -> serde_json::Value {
match self {
FieldType::Class(name) | FieldType::Enum(name) => json!({
Expand Down
Loading

0 comments on commit c17e0da

Please sign in to comment.