Skip to content

Commit 33a66de

Browse files
authored
Merge pull request #492 from malbarbo/dirbundle
Add DirBundle
2 parents 19f9852 + a129f92 commit 33a66de

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

src/bin/tectonic.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use tectonic;
77
use structopt::StructOpt;
88

99
use std::env;
10-
use std::fs::File;
1110
use std::path::{Path, PathBuf};
1211
use std::process;
1312
use std::str::FromStr;
@@ -16,11 +15,10 @@ use std::time;
1615
use tectonic::config::PersistentConfig;
1716
use tectonic::driver::{OutputFormat, PassSetting, ProcessingSessionBuilder};
1817
use tectonic::errors::{ErrorKind, Result};
19-
use tectonic::io::zipbundle::ZipBundle;
2018
use tectonic::status::termcolor::TermcolorStatusBackend;
2119
use tectonic::status::{ChatterLevel, StatusBackend};
2220

23-
use tectonic::{ctry, errmsg, tt_error, tt_error_styled, tt_note};
21+
use tectonic::{errmsg, tt_error, tt_error_styled, tt_note};
2422

2523
#[derive(Debug, StructOpt)]
2624
#[structopt(name = "Tectonic", about = "Process a (La)TeX document")]
@@ -31,16 +29,10 @@ struct CliOptions {
3129
/// The name of the "format" file used to initialize the TeX engine
3230
#[structopt(long, short, name = "path", default_value = "latex")]
3331
format: String,
34-
/// Use this Zip-format bundle file to find resource files instead of the default
35-
#[structopt(
36-
takes_value(true),
37-
parse(from_os_str),
38-
long,
39-
short,
40-
name = "zip_file_path"
41-
)]
32+
/// Use this directory or Zip-format bundle file to find resource files instead of the default
33+
#[structopt(takes_value(true), parse(from_os_str), long, short, name = "file_path")]
4234
bundle: Option<PathBuf>,
43-
/// Use this URL find resource files instead of the default
35+
/// Use this URL to find resource files instead of the default
4436
#[structopt(takes_value(true), long, short, name = "url")]
4537
// TODO add URL validation
4638
web_bundle: Option<String>,
@@ -166,16 +158,10 @@ fn inner(
166158
if only_cached {
167159
tt_note!(status, "using only cached resource files");
168160
}
169-
if let Some(p) = args.bundle {
170-
let zb = ctry!(ZipBundle::<File>::open(&p); "error opening bundle");
171-
sess_builder.bundle(Box::new(zb));
161+
if let Some(path) = args.bundle {
162+
sess_builder.bundle(config.make_local_file_provider(path, status)?);
172163
} else if let Some(u) = args.web_bundle {
173-
sess_builder.bundle(Box::new(config.make_cached_url_provider(
174-
&u,
175-
only_cached,
176-
None,
177-
status,
178-
)?));
164+
sess_builder.bundle(config.make_cached_url_provider(&u, only_cached, None, status)?);
179165
} else {
180166
sess_builder.bundle(config.default_bundle(only_cached, status)?);
181167
}

src/config.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
1313
#[cfg(feature = "serde")]
1414
use serde::{Deserialize, Serialize};
15-
use std::ffi::OsStr;
1615
use std::fs::File;
1716
use std::path::{Path, PathBuf};
1817
use std::sync::atomic::{AtomicBool, Ordering};
1918

2019
use crate::app_dirs;
2120
use crate::errors::{ErrorKind, Result};
2221
use crate::io::cached_itarbundle::CachedITarBundle;
22+
use crate::io::dirbundle::DirBundle;
2323
use crate::io::zipbundle::ZipBundle;
2424
use crate::io::Bundle;
2525
use crate::status::StatusBackend;
@@ -119,12 +119,15 @@ impl PersistentConfig {
119119

120120
pub fn make_local_file_provider(
121121
&self,
122-
file_path: &OsStr,
122+
file_path: PathBuf,
123123
_status: &mut dyn StatusBackend,
124124
) -> Result<Box<dyn Bundle>> {
125-
let zip_bundle = ZipBundle::<File>::open(Path::new(file_path))?;
126-
127-
Ok(Box::new(zip_bundle) as _)
125+
let bundle: Box<dyn Bundle> = if file_path.is_dir() {
126+
Box::new(DirBundle::new(file_path))
127+
} else {
128+
Box::new(ZipBundle::open(file_path)?)
129+
};
130+
Ok(bundle)
128131
}
129132

130133
pub fn default_bundle(
@@ -153,9 +156,7 @@ impl PersistentConfig {
153156
let file_path = url.to_file_path().map_err(|_| {
154157
io::Error::new(io::ErrorKind::InvalidInput, "failed to parse local path")
155158
})?;
156-
let zip_bundle = self.make_local_file_provider(file_path.as_os_str(), status)?;
157-
158-
return Ok(Box::new(zip_bundle) as _);
159+
return self.make_local_file_provider(file_path, status);
159160
}
160161
let bundle =
161162
self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, None, status)?;

src/io/dirbundle.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::ffi::OsStr;
2+
use std::fs::File;
3+
use std::io::BufReader;
4+
use std::path::PathBuf;
5+
6+
use super::{Bundle, InputHandle, InputOrigin, IoProvider, OpenResult};
7+
use crate::status::StatusBackend;
8+
9+
pub struct DirBundle {
10+
dir: PathBuf,
11+
}
12+
13+
impl DirBundle {
14+
pub fn new(dir: PathBuf) -> DirBundle {
15+
DirBundle { dir }
16+
}
17+
}
18+
19+
impl IoProvider for DirBundle {
20+
fn input_open_name(
21+
&mut self,
22+
name: &OsStr,
23+
_status: &mut dyn StatusBackend,
24+
) -> OpenResult<InputHandle> {
25+
let mut path = self.dir.clone();
26+
path.push(name);
27+
28+
if path.is_file() {
29+
match File::open(path) {
30+
Err(e) => OpenResult::Err(e.into()),
31+
Ok(f) => OpenResult::Ok(InputHandle::new(
32+
name,
33+
BufReader::new(f),
34+
InputOrigin::Filesystem,
35+
)),
36+
}
37+
} else {
38+
OpenResult::NotAvailable
39+
}
40+
}
41+
}
42+
43+
impl Bundle for DirBundle {}

src/io/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::errors::{Error, ErrorKind, Result};
1818
use crate::status::StatusBackend;
1919

2020
pub mod cached_itarbundle;
21+
pub mod dirbundle;
2122
pub mod filesystem;
2223
pub mod format_cache;
2324
pub mod memory;

src/io/zipbundle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<R: Read + Seek> ZipBundle<R> {
2626
}
2727

2828
impl ZipBundle<File> {
29-
pub fn open(path: &Path) -> Result<ZipBundle<File>> {
29+
pub fn open<P: AsRef<Path>>(path: P) -> Result<ZipBundle<File>> {
3030
Self::new(File::open(path)?)
3131
}
3232
}

0 commit comments

Comments
 (0)