From 631a887288541199109985518ea326f73943aeeb Mon Sep 17 00:00:00 2001 From: Jake Dawkins Date: Fri, 4 Dec 2020 16:13:00 -0500 Subject: [PATCH] rename SchemaLocation -> SchemaSource --- src/command/graph/push.rs | 6 +++--- src/command/subgraph/push.rs | 6 +++--- src/utils/loaders.rs | 16 ++++++++-------- src/utils/parsers.rs | 19 +++++++++---------- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/command/graph/push.rs b/src/command/graph/push.rs index 6df7c38ca..cf3ef3076 100644 --- a/src/command/graph/push.rs +++ b/src/command/graph/push.rs @@ -7,15 +7,15 @@ use rover_client::query::schema::push; use crate::client::get_studio_client; use crate::command::RoverStdout; use crate::utils::loaders::load_schema_from_flag; -use crate::utils::parsers::{parse_schema_location, SchemaLocation}; +use crate::utils::parsers::{parse_schema_source, SchemaSource}; #[derive(Debug, Serialize, StructOpt)] pub struct Push { /// The schema file to push /// Can pass `-` to use stdin instead of a file - #[structopt(long, short = "s", parse(try_from_str = parse_schema_location))] + #[structopt(long, short = "s", parse(try_from_str = parse_schema_source))] #[serde(skip_serializing)] - schema: SchemaLocation, + schema: SchemaSource, /// Name of graph variant in Apollo Studio to push to #[structopt(long, default_value = "current")] diff --git a/src/command/subgraph/push.rs b/src/command/subgraph/push.rs index 76eb791eb..c22d4ecdf 100644 --- a/src/command/subgraph/push.rs +++ b/src/command/subgraph/push.rs @@ -7,15 +7,15 @@ use crate::client::get_studio_client; use crate::command::RoverStdout; use crate::utils::loaders::load_schema_from_flag; -use crate::utils::parsers::{parse_schema_location, SchemaLocation}; +use crate::utils::parsers::{parse_schema_source, SchemaSource}; #[derive(Debug, Serialize, StructOpt)] pub struct Push { /// The schema file to push /// Can pass `-` to use stdin instead of a file - #[structopt(long, short = "s", parse(try_from_str = parse_schema_location))] + #[structopt(long, short = "s", parse(try_from_str = parse_schema_source))] #[serde(skip_serializing)] - schema: SchemaLocation, + schema: SchemaSource, /// Name of graph variant in Apollo Studio to push to #[structopt(long, default_value = "current")] diff --git a/src/utils/loaders.rs b/src/utils/loaders.rs index 4fa5c787e..47897e9cf 100644 --- a/src/utils/loaders.rs +++ b/src/utils/loaders.rs @@ -1,4 +1,4 @@ -use crate::utils::parsers::SchemaLocation; +use crate::utils::parsers::SchemaSource; use anyhow::{Context, Result}; use std::io::Read; use std::path::Path; @@ -6,16 +6,16 @@ use std::path::Path; /// this fn takes 2 args: the first, an enum describing where to look to load /// a schema - from stdin or a file's PathBuf, and the second, the reference to /// stdin to load from, should it be needed. -pub fn load_schema_from_flag(loc: &SchemaLocation, mut stdin: impl Read) -> Result { +pub fn load_schema_from_flag(loc: &SchemaSource, mut stdin: impl Read) -> Result { match loc { - SchemaLocation::Stdin => { + SchemaSource::Stdin => { let mut buffer = String::new(); stdin .read_to_string(&mut buffer) .context("Failed while attempting to read SDL from stdin")?; Ok(buffer) } - SchemaLocation::File(path) => { + SchemaSource::File(path) => { if Path::exists(&path) { let contents = std::fs::read_to_string(path)?; Ok(contents) @@ -31,7 +31,7 @@ pub fn load_schema_from_flag(loc: &SchemaLocation, mut stdin: impl Read) -> Resu #[cfg(test)] mod tests { - use super::{load_schema_from_flag, SchemaLocation}; + use super::{load_schema_from_flag, SchemaSource}; use assert_fs::prelude::*; use std::path::PathBuf; @@ -45,7 +45,7 @@ mod tests { .unwrap(); let test_path = test_file.path().to_path_buf(); - let loc = SchemaLocation::File(test_path); + let loc = SchemaSource::File(test_path); let schema = load_schema_from_flag(&loc, std::io::stdin()).unwrap(); assert_eq!(schema, "type Query { hello: String! }".to_string()); @@ -54,7 +54,7 @@ mod tests { #[test] fn load_schema_from_flag_errs_on_bad_path() { let empty_path = "./wow.graphql"; - let loc = SchemaLocation::File(PathBuf::from(empty_path)); + let loc = SchemaSource::File(PathBuf::from(empty_path)); let schema = load_schema_from_flag(&loc, std::io::stdin()); assert_eq!(schema.is_err(), true); @@ -65,7 +65,7 @@ mod tests { // input implements std::io::Read, so it should be a suitable // replacement for stdin let input = b"type Query { hello: String! }"; - let loc = SchemaLocation::Stdin; + let loc = SchemaSource::Stdin; let schema = load_schema_from_flag(&loc, &input[..]).unwrap(); assert_eq!(schema, std::str::from_utf8(input).unwrap()); diff --git a/src/utils/parsers.rs b/src/utils/parsers.rs index 3dab18c45..2c85863b3 100644 --- a/src/utils/parsers.rs +++ b/src/utils/parsers.rs @@ -2,39 +2,38 @@ use anyhow::Result; use std::path::PathBuf; #[derive(Debug, PartialEq)] -pub enum SchemaLocation { +pub enum SchemaSource { Stdin, File(PathBuf), } -// Stdin(Box), -pub fn parse_schema_location(loc: &str) -> Result { +pub fn parse_schema_source(loc: &str) -> Result { if loc == "-" { - Ok(SchemaLocation::Stdin) + Ok(SchemaSource::Stdin) } else if loc.is_empty() { Err(anyhow::anyhow!( "The path provided to find a schema is empty" )) } else { let path = PathBuf::from(loc); - Ok(SchemaLocation::File(path)) + Ok(SchemaSource::File(path)) } } #[cfg(test)] mod tests { - use super::{parse_schema_location, SchemaLocation}; + use super::{parse_schema_source, SchemaSource}; #[test] fn it_correctly_parses_stdin_flag() { - assert_eq!(parse_schema_location("-").unwrap(), SchemaLocation::Stdin); + assert_eq!(parse_schema_source("-").unwrap(), SchemaSource::Stdin); } #[test] fn it_correctly_parses_path_option() { - let loc = parse_schema_location("./schema.graphql").unwrap(); + let loc = parse_schema_source("./schema.graphql").unwrap(); match loc { - SchemaLocation::File(buf) => { + SchemaSource::File(buf) => { assert_eq!(buf.to_str().unwrap(), "./schema.graphql"); } _ => panic!("parsed incorrectly as stdin"), @@ -43,7 +42,7 @@ mod tests { #[test] fn it_errs_with_empty_path() { - let loc = parse_schema_location(""); + let loc = parse_schema_source(""); assert!(loc.is_err()); } }