Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: simplify string formatting for readability #1072

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl IRHelper for IntermediateRepr {
} else {
// Check if the parameter is optional.
if !param_type.is_optional() {
scope.push_error(format!("Missing required parameter: {}", param_name));
scope.push_error(format!("Missing required parameter: {param_name}"));
}
}
scope.pop(false);
Expand Down
14 changes: 7 additions & 7 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/scope_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl GenericScope {
#[allow(dead_code)]
fn push_type_error(&mut self, expected: &str, got: &str) {
self.errors
.push(format!("Expected type {}, got `{}`", expected, got));
.push(format!("Expected type {expected}, got `{got}`"));
}
}

Expand All @@ -81,13 +81,13 @@ impl std::fmt::Display for ScopeStack {
}
let indent = " ".repeat(depth);
if let Some(name) = &scope.name {
writeln!(f, "{}{}:", indent, name)?;
writeln!(f, "{indent}{name}:")?;
}
for error in &scope.errors {
writeln!(f, "{} Error: {}", indent, error)?;
writeln!(f, "{indent} Error: {error}")?;
}
for warning in &scope.warnings {
writeln!(f, "{} Warning: {}", indent, warning)?;
writeln!(f, "{indent} Warning: {warning}")?;
}
}
Ok(())
Expand Down Expand Up @@ -126,15 +126,15 @@ impl ScopeStack {
if errors_as_warnings {
parent_scope
.warnings
.extend(scope.errors.iter().map(|e| format!("{}: {}", name, e)));
.extend(scope.errors.iter().map(|e| format!("{name}: {e}")));
} else {
parent_scope
.errors
.extend(scope.errors.iter().map(|e| format!("{}: {}", name, e)));
.extend(scope.errors.iter().map(|e| format!("{name}: {e}")));
}
parent_scope
.warnings
.extend(scope.warnings.iter().map(|e| format!("{}: {}", name, e)));
.extend(scope.warnings.iter().map(|e| format!("{name}: {e}")));
} else {
if errors_as_warnings {
parent_scope.warnings.extend(scope.errors);
Expand Down
48 changes: 20 additions & 28 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use baml_types::{BamlMap, BamlMediaType, BamlValue, FieldType, LiteralValue, TypeValue};
use baml_types::{BamlMap, BamlValue, FieldType, LiteralValue, TypeValue};
use core::result::Result;
use std::path::PathBuf;

Expand All @@ -15,13 +15,12 @@ pub struct ParameterError {
impl ParameterError {
pub(super) fn required_param_missing(&mut self, param_name: &str) {
self.vec
.push(format!("Missing required parameter: {}", param_name));
.push(format!("Missing required parameter: {param_name}"));
}

pub fn invalid_param_type(&mut self, param_name: &str, expected: &str, got: &str) {
self.vec.push(format!(
"Invalid parameter type for {}: expected {}, got {}",
param_name, expected, got
"Invalid parameter type for {param_name}: expected {expected}, got {got}"
));
}
}
Expand Down Expand Up @@ -49,7 +48,7 @@ impl ArgCoercer {
BamlValue::Bool(false) => Ok(BamlValue::String("false".to_string())),
BamlValue::Null => Ok(BamlValue::String("null".to_string())),
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
scope.push_error(format!("Expected type {t:?}, got `{value}`"));
Err(())
}
},
Expand All @@ -58,7 +57,7 @@ impl ArgCoercer {
BamlValue::Int(val) => Ok(BamlValue::Float(*val as f64)),
BamlValue::Float(_) => Ok(value.clone()),
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
scope.push_error(format!("Expected type {t:?}, got `{value}`"));
Err(())
}
},
Expand All @@ -82,9 +81,7 @@ impl ArgCoercer {
for key in kv.keys() {
if !vec!["file", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on file {}: `media_type` is the only supported property",
key,
media_type
"Invalid property `{key}` on file {media_type}: `media_type` is the only supported property"
));
}
}
Expand Down Expand Up @@ -116,9 +113,7 @@ impl ArgCoercer {
for key in kv.keys() {
if !vec!["url", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on url {}: `media_type` is the only supported property",
key,
media_type
"Invalid property `{key}` on url {media_type}: `media_type` is the only supported property"
));
}
}
Expand All @@ -141,9 +136,7 @@ impl ArgCoercer {
for key in kv.keys() {
if !vec!["base64", "media_type"].contains(&key.as_str()) {
scope.push_error(format!(
"Invalid property `{}` on base64 {}: `media_type` is the only supported property",
key,
media_type
"Invalid property `{key}` on base64 {media_type}: `media_type` is the only supported property"
));
}
}
Expand All @@ -154,19 +147,18 @@ impl ArgCoercer {
)))
} else {
scope.push_error(format!(
"Invalid image: expected `file`, `url`, or `base64`, got `{}`",
value
"Invalid image: expected `file`, `url`, or `base64`, got `{value}`"
));
Err(())
}
}
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
scope.push_error(format!("Expected type {t:?}, got `{value}`"));
Err(())
}
},
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
scope.push_error(format!("Expected type {t:?}, got `{value}`"));
Err(())
}
},
Expand All @@ -188,13 +180,13 @@ impl ArgCoercer {
Err(())
}
} else {
scope.push_error(format!("Enum {} not found", name));
scope.push_error(format!("Enum {name} not found"));
Err(())
}
}
BamlValue::Enum(n, _) if n == name => Ok(value.clone()),
_ => {
scope.push_error(format!("Invalid enum {}: Got `{}`", name, value));
scope.push_error(format!("Invalid enum {name}: Got `{value}`"));
Err(())
}
},
Expand All @@ -205,7 +197,7 @@ impl ArgCoercer {
}
(LiteralValue::Bool(lit), BamlValue::Bool(baml)) if lit == baml => value.clone(),
_ => {
scope.push_error(format!("Expected literal {:?}, got `{}`", literal, value));
scope.push_error(format!("Expected literal {literal:?}, got `{value}`"));
return Err(());
}
}),
Expand Down Expand Up @@ -250,12 +242,12 @@ impl ArgCoercer {
Ok(BamlValue::Class(name.to_string(), fields))
}
Err(_) => {
scope.push_error(format!("Class {} not found", name));
scope.push_error(format!("Class {name} not found"));
Err(())
}
},
_ => {
scope.push_error(format!("Expected class {}, got `{}`", name, value));
scope.push_error(format!("Expected class {name}, got `{value}`"));
Err(())
}
},
Expand All @@ -270,7 +262,7 @@ impl ArgCoercer {
Ok(BamlValue::List(items))
}
_ => {
scope.push_error(format!("Expected array, got `{}`", value));
scope.push_error(format!("Expected array, got `{value}`"));
Err(())
}
},
Expand All @@ -296,7 +288,7 @@ impl ArgCoercer {
}
Ok(BamlValue::Map(map))
} else {
scope.push_error(format!("Expected map, got `{}`", value));
scope.push_error(format!("Expected map, got `{value}`"));
Err(())
}
}
Expand All @@ -308,7 +300,7 @@ impl ArgCoercer {
return result;
}
}
scope.push_error(format!("Expected one of {:?}, got `{}`", options, value));
scope.push_error(format!("Expected one of {options:?}, got `{value}`"));
Err(())
}
FieldType::Optional(inner) => {
Expand All @@ -318,7 +310,7 @@ impl ArgCoercer {
let mut inner_scope = ScopeStack::new();
let baml_arg = self.coerce_arg(ir, inner, value, &mut inner_scope);
if inner_scope.has_errors() {
scope.push_error(format!("Expected optional {}, got `{}`", inner, value));
scope.push_error(format!("Expected optional {inner}, got `{value}`"));
Err(())
} else {
baml_arg
Expand Down
5 changes: 1 addition & 4 deletions engine/baml-lib/baml-core/src/ir/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use baml_types::TypeValue;
use serde_json::json;

use super::{
repr::{self},
Class, Enum, FieldType, FunctionArgs, FunctionNode, IntermediateRepr, Walker,
};
use super::{Class, Enum, FieldType, FunctionArgs, FunctionNode, IntermediateRepr, Walker};

pub trait WithJsonSchema {
fn json_schema(&self) -> serde_json::Value;
Expand Down
6 changes: 3 additions & 3 deletions engine/baml-lib/baml-core/src/ir/repr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, Result};
use baml_types::FieldType;
use either::Either;
use indexmap::IndexMap;
Expand Down Expand Up @@ -231,7 +231,7 @@ fn to_ir_attributes(
let ir_expr = match d {
ast::Expression::StringValue(s, _) => Expression::String(s.clone()),
ast::Expression::RawStringValue(s) => Expression::RawString(s.value().to_string()),
_ => panic!("Couldn't deal with description: {:?}", d),
_ => panic!("Couldn't deal with description: {d:?}"),
};
attributes.insert("description".to_string(), ir_expr);
}
Expand Down Expand Up @@ -770,7 +770,7 @@ fn process_field(
},
AliasedKey {
key: original_name.to_string(),
alias: Expression::String(format!("{}: {}", alias, description)),
alias: Expression::String(format!("{alias}: {description}")),
},
]
} else {
Expand Down
2 changes: 1 addition & 1 deletion engine/baml-lib/baml-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn validate_configuration(
}
Err(err) => {
diagnostics.push_error(DatamodelError::new_validation_error(
&format!("Failed to create lock file: {}", err),
&format!("Failed to create lock file: {err}"),
gen.span.clone(),
));
None
Expand Down
14 changes: 6 additions & 8 deletions engine/baml-lib/baml-core/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ impl LockFileWrapper {
// Not ok as prev is set, but current is not.
diag.push_error(DatamodelError::new_validation_error(
&format!(
"The last CLI version was {}. The current version of baml isn't found.",
b
"The last CLI version was {b}. The current version of baml isn't found."
),
self.span.clone().unwrap(),
));
Expand All @@ -173,15 +172,15 @@ impl LockFileWrapper {
std::cmp::Ordering::Less => {
// Not ok as prev is newer than current.
diag.push_error(DatamodelError::new_validation_error(
&format!("The last CLI version was {}. You're currently at: {}. Please run `baml update`", b,a),
&format!("The last CLI version was {b}. You're currently at: {a}. Please run `baml update`"),
self.span.clone().unwrap(),
));
}
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Greater => {
// Ok as prev is older than current.
diag.push_warning(DatamodelWarning::new(
format!("Upgrading generated code with latest CLI: {}", a),
format!("Upgrading generated code with latest CLI: {a}"),
self.span.clone().unwrap(),
));
}
Expand All @@ -198,8 +197,7 @@ impl LockFileWrapper {
// Not ok as prev is set, but current is not.
diag.push_warning(DatamodelWarning::new(
format!(
"The last client version was {}. The current version of the client isn't found. Have you run `baml update-client`?",
b
"The last client version was {b}. The current version of the client isn't found. Have you run `baml update-client`?"
),
self.span.clone().unwrap(),
));
Expand All @@ -209,15 +207,15 @@ impl LockFileWrapper {
std::cmp::Ordering::Less => {
// Not ok as prev is newer than current.
diag.push_error(DatamodelError::new_validation_error(
&format!("The last client version was {}. You're using an older version: {}. Please run: `baml update-client`", b,a),
&format!("The last client version was {b}. You're using an older version: {a}. Please run: `baml update-client`"),
self.span.clone().unwrap(),
));
}
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Greater => {
// Ok as prev is older than current.
diag.push_warning(DatamodelWarning::new(
format!("Upgrading generated code with latest baml client: {}", a),
format!("Upgrading generated code with latest baml client: {a}"),
self.span.clone().unwrap(),
));
}
Expand Down
10 changes: 5 additions & 5 deletions engine/baml-lib/baml-core/src/validate/generator_loader/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn parse_required_key<'a>(
Some(expr) => expr,
None => {
return Err(DatamodelError::new_validation_error(
&format!("The `{}` argument is required for a generator.", key),
&format!("The `{key}` argument is required for a generator."),
generator_span.clone(),
))
}
Expand All @@ -37,7 +37,7 @@ fn parse_required_key<'a>(
match expr.as_string_value() {
Some((name, _)) => Ok(name),
None => Err(DatamodelError::new_validation_error(
&format!("`{}` must be a string.", key),
&format!("`{key}` must be a string."),
expr.span().clone(),
)),
}
Expand All @@ -57,7 +57,7 @@ fn parse_optional_key<'a>(
match expr.as_string_value() {
Some((name, _)) => Ok(Some(name)),
None => Err(DatamodelError::new_validation_error(
&format!("`{}` must be a string.", key),
&format!("`{key}` must be a string."),
expr.span().clone(),
)),
}
Expand Down Expand Up @@ -117,7 +117,7 @@ pub(crate) fn parse_generator(
}
Err(_) => {
errors.push(DatamodelError::new_validation_error(
&format!("The language '{}' is not supported.", name),
&format!("The language '{name}' is not supported."),
ast_generator.span().clone(),
));
}
Expand Down Expand Up @@ -177,7 +177,7 @@ pub(crate) fn parse_generator(
}

let _test_command = match command_prefix {
Some(prefix) => format!("{} python -m pytest", prefix),
Some(prefix) => format!("{prefix} python -m pytest"),
None => "python -m pytest".into(),
};

Expand Down
Loading
Loading