Skip to content

Commit

Permalink
Make examples less sensitive to the exact path from which they're run. (
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi committed Apr 5, 2023
1 parent ef02bb3 commit e41d382
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 40 deletions.
5 changes: 3 additions & 2 deletions trustfall/examples/feeds/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern crate lazy_static;
use crate::adapter::FeedAdapter;

mod adapter;
mod util;

const PCGAMER_FEED_URI: &str =
"https://airedale.futurecdn.net/feeds/en_feed_96a4cb95.rss-fse?nb_results=50&site=pcgamer";
Expand All @@ -28,7 +29,7 @@ const WIRED_FEED_LOCATION: &str = "/tmp/feeds-wired.xml";

lazy_static! {
static ref SCHEMA: Schema =
Schema::parse(fs::read_to_string("./examples/feeds/feeds.graphql").unwrap()).unwrap();
Schema::parse(util::read_file("./examples/feeds/feeds.graphql")).unwrap();
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -61,7 +62,7 @@ fn refresh_data() {
}

fn run_query(path: &str) {
let content = fs::read_to_string(path).unwrap();
let content = util::read_file(path);
let input_query: InputQuery = ron::from_str(&content).unwrap();

let data = read_feed_data();
Expand Down
35 changes: 35 additions & 0 deletions trustfall/examples/feeds/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{
fs,
path::{Components, PathBuf},
};

/// Pop path components from the front of the path component iterator, then try the read again.
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> {
match iter.next() {
Some(_) => match fs::read_to_string(iter.as_path()) {
Ok(content) => Ok(content),
Err(_) => {
if tries_remaining > 0 {
path_compensating_read(iter, tries_remaining - 1)
} else {
Err(())
}
}
},
None => Err(()),
}
}

pub(super) fn read_file(path: &str) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
// Maybe the user is too deep in the directory tree.
// Try skipping some components from the front of the path.
let path = PathBuf::from(path);
path_compensating_read(path.components(), 3)
.map_err(|_| e)
.expect("failed to read file")
}
}
}
8 changes: 4 additions & 4 deletions trustfall/examples/hackernews/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::sync::Arc;
use std::{cell::RefCell, fs};
use std::{env, process};

use serde::Deserialize;
Expand All @@ -13,12 +13,12 @@ use crate::adapter::HackerNewsAdapter;
extern crate lazy_static;

pub mod adapter;
mod util;
pub mod vertex;

lazy_static! {
static ref SCHEMA: Schema =
Schema::parse(fs::read_to_string("./examples/hackernews/hackernews.graphql").unwrap())
.unwrap();
Schema::parse(util::read_file("./examples/hackernews/hackernews.graphql")).unwrap();
}

#[derive(Debug, Clone, Deserialize)]
Expand All @@ -29,7 +29,7 @@ struct InputQuery<'a> {
}

fn run_query(path: &str, max_results: Option<usize>) {
let content = fs::read_to_string(path).unwrap();
let content = util::read_file(path);
let input_query: InputQuery = ron::from_str(&content).unwrap();

let adapter = Rc::new(RefCell::new(HackerNewsAdapter::new()));
Expand Down
35 changes: 35 additions & 0 deletions trustfall/examples/hackernews/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{
fs,
path::{Components, PathBuf},
};

/// Pop path components from the front of the path component iterator, then try the read again.
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> {
match iter.next() {
Some(_) => match fs::read_to_string(iter.as_path()) {
Ok(content) => Ok(content),
Err(_) => {
if tries_remaining > 0 {
path_compensating_read(iter, tries_remaining - 1)
} else {
Err(())
}
}
},
None => Err(()),
}
}

pub(super) fn read_file(path: &str) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
// Maybe the user is too deep in the directory tree.
// Try skipping some components from the front of the path.
let path = PathBuf::from(path);
path_compensating_read(path.components(), 3)
.map_err(|_| e)
.expect("failed to read file")
}
}
}
37 changes: 3 additions & 34 deletions trustfall/examples/weather/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Components, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use std::{cell::RefCell, fs};
Expand All @@ -20,10 +19,11 @@ extern crate lazy_static;

mod adapter;
mod metar;
mod util;

lazy_static! {
static ref SCHEMA: Schema =
Schema::parse(read_file("./examples/weather/metar_weather.graphql"))
Schema::parse(util::read_file("./examples/weather/metar_weather.graphql"))
.expect("failed to parse schema");
}

Expand Down Expand Up @@ -88,39 +88,8 @@ fn read_metar_data() -> Vec<MetarReport> {
metars
}

/// Pop path components from the front of the path component iterator, then try the read again.
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> {
match iter.next() {
Some(_) => match fs::read_to_string(iter.as_path()) {
Ok(content) => Ok(content),
Err(_) => {
if tries_remaining > 0 {
path_compensating_read(iter, tries_remaining - 1)
} else {
Err(())
}
}
},
None => Err(()),
}
}

fn read_file(path: &str) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
// Maybe the user is too deep in the directory tree.
// Try skipping some components from the front of the path.
let path = PathBuf::from(path);
path_compensating_read(path.components(), 3)
.map_err(|_| e)
.expect("failed to read file")
}
}
}

fn run_query(path: &str) {
let content = read_file(path);
let content = util::read_file(path);
let input_query: InputQuery = ron::from_str(&content).unwrap();

let data = read_metar_data();
Expand Down
35 changes: 35 additions & 0 deletions trustfall/examples/weather/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{
fs,
path::{Components, PathBuf},
};

/// Pop path components from the front of the path component iterator, then try the read again.
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> {
match iter.next() {
Some(_) => match fs::read_to_string(iter.as_path()) {
Ok(content) => Ok(content),
Err(_) => {
if tries_remaining > 0 {
path_compensating_read(iter, tries_remaining - 1)
} else {
Err(())
}
}
},
None => Err(()),
}
}

pub(super) fn read_file(path: &str) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
// Maybe the user is too deep in the directory tree.
// Try skipping some components from the front of the path.
let path = PathBuf::from(path);
path_compensating_read(path.components(), 3)
.map_err(|_| e)
.expect("failed to read file")
}
}
}

0 comments on commit e41d382

Please sign in to comment.