summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-09 14:49:36 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-09 14:49:36 +0000
commit9d5758cd7b6bf63c83232fc0a71ae906a2be9c8b (patch)
treeb78f4304820009b17c4c1a35df7b364e4667e4c4
parent41e1968d4f546360c3f19b87a311cd7f6b8ebadc (diff)
downloadsubplot-9d5758cd7b6bf63c83232fc0a71ae906a2be9c8b.tar.gz
chore: Simplify to use resource::read_as_string
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r--src/bindings.rs5
-rw-r--r--src/codegen.rs13
-rw-r--r--src/resource.rs2
-rw-r--r--src/templatespec.rs5
4 files changed, 6 insertions, 19 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index 273a84f..fe26690 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -9,7 +9,6 @@ use serde_aux::prelude::*;
use std::collections::HashMap;
use std::convert::identity;
-use std::io::Read;
use std::path::Path;
use std::str::FromStr;
@@ -507,10 +506,8 @@ impl Bindings {
where
P: AsRef<Path>,
{
- let mut f = resource::open(filename.as_ref())
+ let yaml = resource::read_as_string(filename.as_ref())
.map_err(|e| SubplotError::BindingsFileNotFound(filename.as_ref().into(), e))?;
- let mut yaml = String::new();
- f.read_to_string(&mut yaml)?;
self.add_from_yaml(&yaml)?;
Ok(())
}
diff --git a/src/codegen.rs b/src/codegen.rs
index f436a34..063f8a2 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -1,7 +1,7 @@
use crate::{resource, Document, SubplotError, TemplateSpec};
use std::collections::HashMap;
use std::fs::File;
-use std::io::prelude::{Read, Write};
+use std::io::Write;
use std::path::{Path, PathBuf};
use base64::encode;
@@ -52,8 +52,8 @@ fn context(doc: &mut Document) -> Result<Context> {
let funcs_filenames = doc.meta().functions_filenames();
let mut funcs = vec![];
for filename in funcs_filenames {
- let content =
- cat(filename).map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e))?;
+ let content = resource::read_as_string(filename)
+ .map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e.into()))?;
funcs.push(Func::new(filename, content));
}
context.insert("functions", &funcs);
@@ -73,13 +73,6 @@ fn tera(tmplspec: &TemplateSpec) -> Result<Tera> {
Ok(tera)
}
-fn cat<P: AsRef<Path>>(filename: P) -> Result<String> {
- let mut f = resource::open(filename)?;
- let mut buf = String::new();
- f.read_to_string(&mut buf)?;
- Ok(buf)
-}
-
fn write(filename: &Path, content: &str) -> Result<()> {
let mut f: File = File::create(filename)?;
f.write_all(&content.as_bytes())?;
diff --git a/src/resource.rs b/src/resource.rs
index 15a239e..f34d806 100644
--- a/src/resource.rs
+++ b/src/resource.rs
@@ -19,7 +19,7 @@ pub fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<impl Read>> {
/// falling back to potentially embedded file content
pub fn read_as_string<P: AsRef<Path>>(subpath: P) -> io::Result<String> {
let mut f = open(subpath)?;
- let mut ret = String::new();
+ let mut ret = String::with_capacity(8192);
f.read_to_string(&mut ret)?;
Ok(ret)
}
diff --git a/src/templatespec.rs b/src/templatespec.rs
index 4bb1bda..d2389d8 100644
--- a/src/templatespec.rs
+++ b/src/templatespec.rs
@@ -4,7 +4,6 @@ use crate::SubplotError;
use serde::Deserialize;
-use std::io::Read;
use std::path::{Path, PathBuf};
/// A template specification.
@@ -45,9 +44,7 @@ impl TemplateSpec {
/// Read a template.yaml file and create the corresponding TemplateSpec.
pub fn from_file(filename: &Path) -> Result<TemplateSpec> {
- let mut f = resource::open(filename)?;
- let mut yaml = String::new();
- f.read_to_string(&mut yaml)?;
+ let yaml = resource::read_as_string(filename)?;
let spec = TemplateSpec::from_yaml(&yaml)?;
let dirname = match filename.parent() {
Some(x) => x,