Skip to content
Merged
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
35 changes: 19 additions & 16 deletions gel-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod validation;

use derive_more::{Display, Error};
use indexmap::IndexMap;
use std::{borrow::Cow, fmt::Debug};
use std::{borrow::Cow, fmt::Debug, str::FromStr};
use toml::Value as TomlValue;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -34,18 +34,21 @@ impl PrimitiveType {
PrimitiveType::Duration => "duration",
}
}
}

pub fn from_str(s: &str) -> Option<Self> {
impl FromStr for PrimitiveType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"str" => Some(PrimitiveType::String),
"int64" => Some(PrimitiveType::Int64),
"int32" => Some(PrimitiveType::Int32),
"int16" => Some(PrimitiveType::Int16),
"float64" => Some(PrimitiveType::Float64),
"float32" => Some(PrimitiveType::Float32),
"bool" => Some(PrimitiveType::Boolean),
"duration" => Some(PrimitiveType::Duration),
_ => None,
"str" => Ok(PrimitiveType::String),
"int64" => Ok(PrimitiveType::Int64),
"int32" => Ok(PrimitiveType::Int32),
"int16" => Ok(PrimitiveType::Int16),
"float64" => Ok(PrimitiveType::Float64),
"float32" => Ok(PrimitiveType::Float32),
"bool" => Ok(PrimitiveType::Boolean),
"duration" => Ok(PrimitiveType::Duration),
_ => Err(()),
}
}
}
Expand All @@ -70,11 +73,11 @@ pub enum Value {
impl Debug for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::Injected(s) => write!(f, "{}", s),
Value::Injected(s) => write!(f, "{s}"),
Value::Set(values) => {
write!(f, "[")?;
for (i, value) in values.iter().enumerate() {
write!(f, "{:?}", value)?;
write!(f, "{value:?}")?;
if i < values.len() - 1 {
write!(f, ", ")?;
}
Expand All @@ -84,17 +87,17 @@ impl Debug for Value {
Value::Array(values) => {
write!(f, "[")?;
for (i, value) in values.iter().enumerate() {
write!(f, "{:?}", value)?;
write!(f, "{value:?}")?;
if i < values.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, "]")
}
Value::Insert { typ, values } => {
write!(f, "insert {} = {{\n", typ)?;
writeln!(f, "insert {typ} = {{")?;
for (key, value) in values {
write!(f, " {}: {:?},\n", key, value)?;
writeln!(f, " {key}: {value:?},")?;
}
write!(f, "}}")
}
Expand Down
12 changes: 6 additions & 6 deletions gel-config/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct ConfigureSet {
impl Debug for ConfigureSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(extension_name) = &self.extension_name {
write!(f, "configure {}", extension_name)?;
write!(f, "configure {extension_name}")?;
} else {
write!(f, "configure")?;
}
Expand All @@ -56,22 +56,22 @@ pub struct Commands {
impl Debug for Commands {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for set in &self.set {
write!(f, "{:?};\n", set)?;
writeln!(f, "{set:?};")?;
}
for (object_name, insert) in &self.insert {
for value in &insert.values {
if let Some(extension_name) = &insert.extension_name {
write!(f, "insert {}", extension_name)?;
write!(f, "insert {extension_name}")?;
} else {
write!(f, "insert")?;
}
write!(f, " {object_name} {{\n")?;
writeln!(f, " {object_name} {{")?;
for (key, value) in value {
let mut value = format!("{value:?}");
value = value.replace("\n", "\n ");
write!(f, " {key}: {value},\n", key = key, value = value)?;
writeln!(f, " {key}: {value},")?;
}
write!(f, "}};\n")?;
writeln!(f, "}};")?;
}
}
Ok(())
Expand Down
10 changes: 5 additions & 5 deletions gel-config/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn test_complex() {
let toml = toml::Table::deserialize(toml).unwrap();
let branch = toml.get("branch").unwrap().get("config").unwrap();
let commands = validate(branch.clone(), &schema).unwrap();
println!("branch:\n{:?}", commands);
println!("branch:\n{commands:?}");
let instance = toml.get("instance").unwrap().get("config").unwrap();
let commands = validate(instance.clone(), &schema).unwrap();
println!("instance:\n{:?}", commands);
println!("instance:\n{commands:?}");
}

#[test]
Expand All @@ -23,10 +23,10 @@ fn test_full() {
let toml = toml::Table::deserialize(toml).unwrap();
let branch = toml.get("branch").unwrap().get("config").unwrap();
let commands = validate(branch.clone(), &schema).unwrap();
println!("branch:\n{:?}", commands);
println!("branch:\n{commands:?}");
let instance = toml.get("instance").unwrap().get("config").unwrap();
let commands = validate(instance.clone(), &schema).unwrap();
println!("instance:\n{:?}", commands);
println!("instance:\n{commands:?}");
}

#[test]
Expand All @@ -37,5 +37,5 @@ fn test_object() {
let toml = toml::Table::deserialize(toml).unwrap();
let branch = toml.get("branch").unwrap().get("config").unwrap();
let commands = validate(branch.clone(), &schema).unwrap();
println!("{:?}", commands);
println!("{commands:?}");
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.85"
channel = "1.88"
components = ["rustc", "cargo", "rust-std", "rust-src", "clippy", "rustfmt", "rust-analyzer"]
Loading