From d5ff97f42ed1e682a66ea8d51e5f9ed1be547b9c Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 2 Aug 2020 16:56:14 +0200 Subject: =?UTF-8?q?Move=20binary=20into=20separate=20crate=20and=20tidy=20?= =?UTF-8?q?dependencies=20=F0=9F=8E=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 22 +++++++++-------- main/Cargo.toml | 14 +++++++++++ main/main.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/bin/main.rs | 62 ------------------------------------------------ src/diagnostic.rs | 10 +++++--- src/geom.rs | 8 +++++-- src/layout/actions.rs | 5 +++- src/layout/mod.rs | 12 ++++++---- src/layout/model.rs | 3 +-- src/layout/stack.rs | 3 +-- src/lib.rs | 3 +-- src/syntax/decoration.rs | 7 ++++-- src/syntax/span.rs | 11 ++++++--- 13 files changed, 126 insertions(+), 94 deletions(-) create mode 100644 main/Cargo.toml create mode 100644 main/main.rs delete mode 100644 src/bin/main.rs diff --git a/Cargo.toml b/Cargo.toml index e0563b5c..e0495df4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,27 +1,29 @@ [package] name = "typstc" version = "0.1.0" -authors = ["Laurenz Mädje "] +authors = ["The Typst Project Developers"] edition = "2018" +[workspace] +members = ["main"] + [dependencies] async-trait = "0.1" fontdock = { path = "../fontdock", features = ["fs", "serialize"] } -serde = { version = "1", features = ["derive"] } -serde_json = "1" -smallvec = "1" +serde = { version = "1", features = ["derive"], optional = true } tide = { path = "../tide" } ttf-parser = "0.8.2" unicode-xid = "0.2" -futures-executor = { version = "0.3", optional = true } -[[bin]] -name = "typst" -path = "src/bin/main.rs" -required-features = ["futures-executor"] +[features] +serialize = [] + +[dev-dependencies] +futures-executor = "0.3" +serde_json = "1" [[test]] name = "typeset" path = "tests/src/typeset.rs" harness = false -required-features = ["futures-executor"] +required-features = ["serialize"] diff --git a/main/Cargo.toml b/main/Cargo.toml new file mode 100644 index 00000000..dcb75981 --- /dev/null +++ b/main/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "typstc-main" +version = "0.1.0" +authors = ["The Typst Project Developers"] +edition = "2018" + +[dependencies] +typstc = { path = ".." } +fontdock = { path = "../../fontdock", features = ["fs"] } +futures-executor = "0.3" + +[[bin]] +name = "typstc-main" +path = "main.rs" diff --git a/main/main.rs b/main/main.rs new file mode 100644 index 00000000..a04b163c --- /dev/null +++ b/main/main.rs @@ -0,0 +1,60 @@ +use std::cell::RefCell; +use std::error::Error; +use std::fs::{File, read_to_string}; +use std::io::BufWriter; +use std::path::{Path, PathBuf}; +use std::rc::Rc; +use futures_executor::block_on; + +use fontdock::fs::{FsIndex, FsProvider}; +use fontdock::FontLoader; +use typstc::Typesetter; +use typstc::font::DynProvider; +use typstc::export::pdf; + +fn main() { + if let Err(err) = run() { + eprintln!("error: {}", err); + std::process::exit(1); + } +} + +fn run() -> Result<(), Box> { + let args: Vec = std::env::args().collect(); + if args.len() < 2 || args.len() > 3 { + println!("Usage: typst src.typ [out.pdf]"); + std::process::exit(0); + } + + let source = Path::new(&args[1]); + let dest = if args.len() <= 2 { + source.with_extension("pdf") + } else { + PathBuf::from(&args[2]) + }; + + if source == dest { + Err("source and destination path are the same")?; + } + + let src = read_to_string(source) + .map_err(|_| "failed to read from source file")?; + + let mut index = FsIndex::new(); + index.search_dir("fonts"); + index.search_os(); + + let (descriptors, files) = index.into_vecs(); + let provider = FsProvider::new(files.clone()); + let dynamic = Box::new(provider) as Box; + let loader = FontLoader::new(dynamic, descriptors); + let loader = Rc::new(RefCell::new(loader)); + + let typesetter = Typesetter::new(loader.clone()); + let layouts = block_on(typesetter.typeset(&src)).output; + + let writer = BufWriter::new(File::create(&dest)?); + pdf::export(&layouts, &loader, writer)?; + + Ok(()) +} diff --git a/src/bin/main.rs b/src/bin/main.rs deleted file mode 100644 index 6d115c1e..00000000 --- a/src/bin/main.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::cell::RefCell; -use std::error::Error; -use std::fs::{File, read_to_string}; -use std::io::BufWriter; -use std::path::{Path, PathBuf}; -use std::rc::Rc; -use futures_executor::block_on; - -use fontdock::fs::{FsIndex, FsProvider}; -use fontdock::FontLoader; -use typstc::Typesetter; -use typstc::font::DynProvider; -use typstc::export::pdf; - -fn main() { - if let Err(err) = run() { - eprintln!("error: {}", err); - std::process::exit(1); - } -} - -fn run() -> Result<(), Box> { - let args: Vec = std::env::args().collect(); - if args.len() < 2 || args.len() > 3 { - println!("typst"); - println!("usage: {} source [destination]", - args.first().map(|s| s.as_str()).unwrap_or("typst")); - std::process::exit(0); - } - - let source = Path::new(&args[1]); - let dest = if args.len() <= 2 { - source.with_extension("pdf") - } else { - PathBuf::from(&args[2]) - }; - - if source == dest { - Err("source and destination path are the same")?; - } - - let src = read_to_string(source) - .map_err(|_| "failed to read from source file")?; - - let mut index = FsIndex::new(); - index.search_dir("fonts"); - index.search_os(); - - let (descriptors, files) = index.into_vecs(); - let provider = FsProvider::new(files.clone()); - let dynamic = Box::new(provider) as Box; - let loader = FontLoader::new(dynamic, descriptors); - let loader = Rc::new(RefCell::new(loader)); - - let typesetter = Typesetter::new(loader.clone()); - let layouts = block_on(typesetter.typeset(&src)).output; - - let writer = BufWriter::new(File::create(&dest)?); - pdf::export(&layouts, &loader, writer)?; - - Ok(()) -} diff --git a/src/diagnostic.rs b/src/diagnostic.rs index a5c5d0b8..fc332aac 100644 --- a/src/diagnostic.rs +++ b/src/diagnostic.rs @@ -4,14 +4,17 @@ //! layout. However, this is a best effort process and bad things will still //! generate errors and warnings. +#[cfg(feature = "serialize")] use serde::Serialize; + use crate::syntax::span::SpanVec; /// A list of spanned diagnostics. pub type Diagnostics = SpanVec; /// A diagnostic that arose in parsing or layouting. -#[derive(Debug, Clone, Eq, PartialEq, Serialize)] +#[derive(Debug, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Diagnostic { /// How severe / important the diagnostic is. pub level: Level, @@ -20,8 +23,9 @@ pub struct Diagnostic { } /// How severe / important a diagnostic is. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)] -#[serde(rename_all = "camelCase")] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "serialize", derive(Serialize))] +#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))] pub enum Level { Warning, Error, diff --git a/src/geom.rs b/src/geom.rs index 557d244c..f5f818a4 100644 --- a/src/geom.rs +++ b/src/geom.rs @@ -3,11 +3,14 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::*; +#[cfg(feature = "serialize")] use serde::Serialize; + use crate::layout::prelude::*; /// A value in two dimensions. -#[derive(Default, Copy, Clone, Eq, PartialEq, Serialize)] +#[derive(Default, Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Value2 { /// The horizontal component. pub x: T, @@ -176,7 +179,8 @@ impl Neg for Size { } /// A value in four dimensions. -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Value4 { /// The left extent. pub left: T, diff --git a/src/layout/actions.rs b/src/layout/actions.rs index 89c10285..7a32a46a 100644 --- a/src/layout/actions.rs +++ b/src/layout/actions.rs @@ -1,9 +1,11 @@ //! Drawing and configuration actions composing layouts. use std::fmt::{self, Debug, Formatter}; + +#[cfg(feature = "serialize")] use serde::ser::{Serialize, Serializer, SerializeTuple}; -use fontdock::FaceId; +use fontdock::FaceId; use crate::geom::Size; use super::Layout; use self::LayoutAction::*; @@ -22,6 +24,7 @@ pub enum LayoutAction { DebugBox(Size), } +#[cfg(feature = "serialize")] impl Serialize for LayoutAction { fn serialize(&self, serializer: S) -> Result where S: Serializer { match self { diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 510f504a..64a2825b 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,10 +1,11 @@ //! Layouting types and engines. use std::fmt::{self, Display, Formatter}; -use smallvec::SmallVec; + +#[cfg(feature = "serialize")] use serde::Serialize; -use fontdock::FaceId; +use fontdock::FaceId; use crate::geom::{Size, Margins}; use self::prelude::*; @@ -31,12 +32,13 @@ pub mod prelude { pub type MultiLayout = Vec; /// A finished box with content at fixed positions. -#[derive(Debug, Clone, PartialEq, Serialize)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Layout { /// The size of the box. pub dimensions: Size, /// How to align this layout in a parent container. - #[serde(skip)] + #[cfg_attr(feature = "serialize", serde(skip))] pub align: LayoutAlign, /// The actions composing this layout. pub actions: Vec, @@ -59,7 +61,7 @@ impl Layout { /// A vector of layout spaces, that is stack allocated as long as it only /// contains at most 2 spaces. -pub type LayoutSpaces = SmallVec<[LayoutSpace; 2]>; +pub type LayoutSpaces = Vec; /// The space into which content is laid out. #[derive(Debug, Copy, Clone, PartialEq)] diff --git a/src/layout/model.rs b/src/layout/model.rs index 08a9ec0e..bde451e6 100644 --- a/src/layout/model.rs +++ b/src/layout/model.rs @@ -4,7 +4,6 @@ use std::future::Future; use std::pin::Pin; -use smallvec::smallvec; use crate::{Pass, Feedback}; use crate::SharedFontLoader; @@ -271,7 +270,7 @@ impl<'a> ModelLayouter<'a> { // new page style and update it within the layouter. let margins = style.margins(); self.ctx.base = style.dimensions.unpadded(margins); - self.layouter.set_spaces(smallvec![ + self.layouter.set_spaces(vec![ LayoutSpace { dimensions: style.dimensions, padding: margins, diff --git a/src/layout/stack.rs b/src/layout/stack.rs index e684a6ab..48c7b40a 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -21,7 +21,6 @@ //! The position of the first aligned box thus depends on the length of the //! sentence in the second box. -use smallvec::smallvec; use crate::geom::Value4; use super::*; @@ -248,7 +247,7 @@ impl StackLayouter { pub fn remaining(&self) -> LayoutSpaces { let dimensions = self.usable(); - let mut spaces = smallvec![LayoutSpace { + let mut spaces = vec![LayoutSpace { dimensions, padding: Margins::ZERO, expansion: LayoutExpansion::new(false, false), diff --git a/src/lib.rs b/src/lib.rs index a295e152..6f8ea074 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,6 @@ //! serialized to pass it to a suitable renderer. use std::fmt::Debug; -use smallvec::smallvec; use crate::diagnostic::Diagnostics; use crate::font::SharedFontLoader; @@ -109,7 +108,7 @@ impl Typesetter { loader: &self.loader, style: &self.style, base: self.style.page.dimensions.unpadded(margins), - spaces: smallvec![LayoutSpace { + spaces: vec![LayoutSpace { dimensions: self.style.page.dimensions, padding: margins, expansion: LayoutExpansion::new(true, true), diff --git a/src/syntax/decoration.rs b/src/syntax/decoration.rs index ab327237..13a9ad36 100644 --- a/src/syntax/decoration.rs +++ b/src/syntax/decoration.rs @@ -1,14 +1,17 @@ //! Decorations for semantic syntax highlighting. +#[cfg(feature = "serialize")] use serde::Serialize; + use super::span::SpanVec; /// A list of spanned decorations. pub type Decorations = SpanVec; /// Decorations for semantic syntax highlighting. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize)] -#[serde(rename_all = "camelCase")] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serialize", derive(Serialize))] +#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))] pub enum Decoration { /// A valid function name. /// ```typst diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 52b90cee..9b3c7d24 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -2,6 +2,8 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Sub}; + +#[cfg(feature = "serialize")] use serde::Serialize; /// Span offsetting. @@ -23,7 +25,8 @@ impl Offset for SpanVec { } /// A value with the span it corresponds to in the source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Spanned { /// The value. pub v: T, @@ -77,7 +80,8 @@ impl Debug for Spanned { } /// Locates a slice of source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Span { /// The inclusive start position. pub start: Pos, @@ -129,7 +133,8 @@ impl Debug for Span { } /// Zero-indexed line-column position in source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)] +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Pos { /// The zero-indexed line. pub line: usize, -- cgit v1.2.3