Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from kiesraad/github-actions-workflow
Browse files Browse the repository at this point in the history
Add GitHub Actions workflow
  • Loading branch information
cikzh authored Mar 21, 2024
2 parents 3f20791 + b3fe6ed commit 9f7f1b1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Rust

on:
push:
workflow_dispatch:

env:
CARGO_TERM_COLOR: always

jobs:
rust:
name: Compile, lint, test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Rust
run: rustup update stable && rustup default stable
- name: Cargo cache
uses: actions/cache@v4
with:
path: |
~/.cargo/.crates.toml
~/.cargo/.crates2.json
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Build
run: cargo build --verbose
- name: Check rustfmt
run: cargo fmt --all -- --check
- name: Check clippy
run: cargo clippy -- -D warnings
- name: Run tests
run: cargo test --verbose
4 changes: 1 addition & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ pub struct Kandidaat {
}

#[derive(Serialize, Deserialize)]
pub struct ModelP22_1Input {

}
pub struct ModelP22_1Input {}

impl PdfModel {
/// Get the filename for the input and template
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{fs::read_to_string, time::Instant};

use typst::{diag::eco_format, eval::Tracer, foundations::Smart};
use typst_pdf;

use crate::world::PdfWorld;

Expand All @@ -12,7 +11,9 @@ fn main() {
println!("Initializing...");
let start = Instant::now();
let mut world = PdfWorld::new();
world.set_input_model(input::PdfModel::ModelO7(serde_json::from_str(&read_to_string("templates/inputs/model-o-7.json").unwrap()).unwrap()));
world.set_input_model(input::PdfModel::ModelO7(
serde_json::from_str(&read_to_string("templates/inputs/model-o-7.json").unwrap()).unwrap(),
));
println!("Initialization took {} ms", start.elapsed().as_millis());

println!("Starting compilation...");
Expand All @@ -29,7 +30,10 @@ fn main() {
std::fs::write("./test.pdf", buffer)
.map_err(|err| eco_format!("failed to write PDF file ({err})"))
.unwrap();
println!("Pdf generation took {} ms", pdf_gen_start.elapsed().as_millis());
println!(
"Pdf generation took {} ms",
pdf_gen_start.elapsed().as_millis()
);
}
Err(err) => eprintln!("{:?}", err),
}
Expand Down
41 changes: 24 additions & 17 deletions src/world.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::collections::HashMap;

use comemo::Prehashed;
use typst::{diag::{FileError, FileResult}, foundations::{Bytes, Datetime}, syntax::{FileId, Source, VirtualPath}, text::{Font, FontBook}, Library, World};
use typst::{
diag::{FileError, FileResult},
foundations::{Bytes, Datetime},
syntax::{FileId, Source, VirtualPath},
text::{Font, FontBook},
Library, World,
};

use crate::input::PdfModel;

Expand Down Expand Up @@ -34,7 +40,10 @@ impl PdfWorld {
assets,
library: Prehashed::new(Library::builder().build()),
main_source: Source::new(FileId::new(None, VirtualPath::new("empty.typ")), "".into()),
input_data: (FileId::new(None, VirtualPath::new("input.json")), Bytes::from_static(&[])),
input_data: (
FileId::new(None, VirtualPath::new("input.json")),
Bytes::from_static(&[]),
),
}
}

Expand All @@ -44,21 +53,19 @@ impl PdfWorld {
/// for that template is.
pub fn set_input_model(&mut self, input: PdfModel) {
let main_source_path = input.as_template_path();
let main_source = self.sources.iter()
let main_source = self
.sources
.iter()
.find(|s| s.id().vpath().as_rootless_path() == main_source_path)
.cloned()
.unwrap();
let input_path = input.as_input_path();
let input_data = input.get_input().unwrap();
self.main_source = main_source;
self.input_data = (
FileId::new(None, VirtualPath::new(input_path)),
input_data,
);
self.input_data = (FileId::new(None, VirtualPath::new(input_path)), input_data);
}
}


impl World for PdfWorld {
fn library(&self) -> &Prehashed<Library> {
&self.library
Expand Down Expand Up @@ -128,7 +135,7 @@ macro_rules! include_filedata {
} else {
include_bytes!(concat!("../", $path)) as &'static [u8]
}
}
};
}

/// Macro that loads data as a string from a file
Expand All @@ -142,21 +149,22 @@ macro_rules! include_strdata {
} else {
include_str!(concat!("../", $path)) as &'static str
}
}
};
}

/// Load all sources available from the `templates/` directory (i.e. all typst
/// files).
fn load_sources() -> Vec<Source> {
macro_rules! include_source {
($path:literal) => {
Source::new(FileId::new(None, VirtualPath::new($path)), include_strdata!($path).to_string())
}
Source::new(
FileId::new(None, VirtualPath::new($path)),
include_strdata!($path).to_string(),
)
};
}

vec![
include_source!("templates/model-o-7.typ"),
]
vec![include_source!("templates/model-o-7.typ")]
}

/// Load all fonts available from the `fonts/` directory
Expand All @@ -170,8 +178,7 @@ fn load_fonts() -> (Vec<Font>, FontBook) {
let font = Font::new(Bytes::from_static(fontdata), 0).expect("Error reading font file");
fontbook.push(font.info().clone());
fonts.push(font);

}
};
}

include_font!("fonts/bitstream-vera/Vera.ttf");
Expand Down

0 comments on commit 9f7f1b1

Please sign in to comment.