Skip to content

Commit 7aae84b

Browse files
committed
switch to FromStr
1 parent dca5c86 commit 7aae84b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

crates/mcp-apollo-server/src/custom_scalar_map.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use rmcp::{
33
schemars::schema::{Schema, SchemaObject, SingleOrVec},
44
serde_json,
55
};
6-
use std::{collections::HashMap, path::PathBuf};
6+
use std::{collections::HashMap, path::PathBuf, str::FromStr};
77

8-
impl TryFrom<&str> for CustomScalarMap {
9-
type Error = ServerError;
8+
impl FromStr for CustomScalarMap {
9+
type Err = ServerError;
1010

11-
fn try_from(string_custom_scalar_file: &str) -> Result<Self, Self::Error> {
11+
fn from_str(string_custom_scalar_file: &str) -> Result<Self, Self::Err> {
1212
// Parse the string into an initial map of serde_json::Values
1313
let parsed_custom_scalar_file: serde_json::Map<String, serde_json::Value> =
1414
serde_json::from_str(string_custom_scalar_file)
@@ -44,7 +44,7 @@ impl TryFrom<&PathBuf> for CustomScalarMap {
4444
let custom_scalars_config_path = file_path_buf.as_path();
4545
tracing::info!(custom_scalars_config=?custom_scalars_config_path, "Loading custom_scalars_config");
4646
let string_custom_scalar_file = std::fs::read_to_string(custom_scalars_config_path)?;
47-
CustomScalarMap::try_from(string_custom_scalar_file.as_str())
47+
CustomScalarMap::from_str(string_custom_scalar_file.as_str())
4848
}
4949
}
5050

@@ -79,7 +79,10 @@ fn has_invalid_schema(schema: &Schema) -> bool {
7979

8080
#[cfg(test)]
8181
mod tests {
82-
use std::collections::{BTreeMap, HashMap};
82+
use std::{
83+
collections::{BTreeMap, HashMap},
84+
str::FromStr,
85+
};
8386

8487
use rmcp::schemars::schema::{
8588
InstanceType, ObjectValidation, Schema, SchemaObject, SingleOrVec,
@@ -89,7 +92,7 @@ mod tests {
8992

9093
#[test]
9194
fn empty_file() {
92-
let result = CustomScalarMap::try_from("").err().unwrap();
95+
let result = CustomScalarMap::from_str("").err().unwrap();
9396

9497
insta::assert_debug_snapshot!(result, @r###"
9598
CustomScalarConfig(
@@ -100,7 +103,7 @@ mod tests {
100103

101104
#[test]
102105
fn only_spaces() {
103-
let result = CustomScalarMap::try_from(" ").err().unwrap();
106+
let result = CustomScalarMap::from_str(" ").err().unwrap();
104107

105108
insta::assert_debug_snapshot!(result, @r###"
106109
CustomScalarConfig(
@@ -111,7 +114,7 @@ mod tests {
111114

112115
#[test]
113116
fn invalid_json() {
114-
let result = CustomScalarMap::try_from("Hello: }").err().unwrap();
117+
let result = CustomScalarMap::from_str("Hello: }").err().unwrap();
115118

116119
insta::assert_debug_snapshot!(result, @r###"
117120
CustomScalarConfig(
@@ -122,7 +125,7 @@ mod tests {
122125

123126
#[test]
124127
fn invalid_simple_schema() {
125-
let result = CustomScalarMap::try_from(
128+
let result = CustomScalarMap::from_str(
126129
r###"{
127130
"custom": {
128131
"test": true
@@ -143,7 +146,7 @@ mod tests {
143146

144147
#[test]
145148
fn invalid_complex_schema() {
146-
let result = CustomScalarMap::try_from(
149+
let result = CustomScalarMap::from_str(
147150
r###"{
148151
"custom": {
149152
"type": "object",
@@ -174,7 +177,7 @@ mod tests {
174177

175178
#[test]
176179
fn valid_schema() {
177-
let result = CustomScalarMap::try_from(
180+
let result = CustomScalarMap::from_str(
178181
r###"
179182
{
180183
"simple": {

crates/mcp-apollo-server/src/operations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl graphql::Executable for Operation {
459459

460460
#[cfg(test)]
461461
mod tests {
462-
use std::{collections::HashSet, sync::LazyLock};
462+
use std::{collections::HashSet, str::FromStr, sync::LazyLock};
463463

464464
use apollo_compiler::{Schema, parser::Parser, validation::Valid};
465465
use rmcp::{model::Tool, serde_json};
@@ -1062,7 +1062,7 @@ mod tests {
10621062
let operation = Operation::from_document(
10631063
"query QueryName($id: RealCustomScalar) { id }",
10641064
&SCHEMA,
1065-
Some(&CustomScalarMap::try_from("{}").unwrap()),
1065+
Some(&CustomScalarMap::from_str("{}").unwrap()),
10661066
)
10671067
.unwrap();
10681068
let tool = Tool::from(operation);
@@ -1086,7 +1086,7 @@ mod tests {
10861086
#[test]
10871087
fn custom_scalar_with_map() {
10881088
let custom_scalar_map =
1089-
CustomScalarMap::try_from("{ \"RealCustomScalar\": { \"type\": \"string\" }}");
1089+
CustomScalarMap::from_str("{ \"RealCustomScalar\": { \"type\": \"string\" }}");
10901090

10911091
let operation = Operation::from_document(
10921092
"query QueryName($id: RealCustomScalar) { id }",

0 commit comments

Comments
 (0)