Skip to content

Commit

Permalink
add github action checks
Browse files Browse the repository at this point in the history
Add checks for build, `cargo fmt`, `cargo check`, and tests.

The tests are currently failing (known), but I want to get the GH checks
in place anyway.
  • Loading branch information
yshavit authored Jun 8, 2024
1 parent ce4fcc5 commit abfcde0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 37 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/cargo_to_gh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

json_opts=()
if ! echo "$*" | grep -q -- '--message-format json' ; then
echo "NO: $*"
json_opts=(--message-format json)
fi

exit_code=0
while IFS= read -r line; do
echo "$line"
exit_code=1
done < <(
set -x
cargo "$@" ${json_opts[@]} |
jq -r '
select(.message.level and (.message.spans | length > 0))
| {level: (.message.level | sub("note"; "notice")), message: .message.message} as $ctx
| .message.spans[]
| "::\($ctx.level) file=\(.file_name),line=\(.line_start),col=\(.column_start),endLine=\(.line_end),columnEnd=\(.column_end),title=rust check::\($ctx.message)"'
)

exit "$exit_code"
42 changes: 42 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: cargo build
run: .github/workflows/cargo_to_gh rustc --message-format json -- -Awarnings

check:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: cargo check
run: .github/workflows/cargo_to_gh check

test:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: cargo test
run: cargo test --verbose

fmt:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: cargo fmt
run: cargo fmt --check
33 changes: 17 additions & 16 deletions src/fmt_md.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::fmt_str::{pad_to, Paddable, standard_align};
use crate::fmt_str::{pad_to, standard_align};
use crate::output::Block::Inlined;
use crate::output::{Block, Output};
use crate::tree::{CodeVariant, Inline, InlineVariant, MdqNode, SpanVariant};
use std::borrow::Borrow;
use std::cmp::max;
use std::fmt::{Alignment};
use std::fmt::Alignment;
use std::io::Write;
use crate::output::Block::Inlined;

pub fn write_md<N, W>(out: &mut Output<W>, nodes: &[N])
where
N: Borrow<MdqNode>,
W: Write,
where
N: Borrow<MdqNode>,
W: Write,
{
let mut iter = nodes.iter().peekable();
while let Some(node) = iter.next() {
Expand All @@ -22,8 +22,8 @@ pub fn write_md<N, W>(out: &mut Output<W>, nodes: &[N])
}

pub fn write_one_md<W>(out: &mut Output<W>, node: &MdqNode)
where
W: Write,
where
W: Write,
{
match node {
MdqNode::Root { body } => write_md(out, body),
Expand Down Expand Up @@ -59,7 +59,8 @@ pub fn write_one_md<W>(out: &mut Output<W>, node: &MdqNode)
match &mut index {
None => prefix.push_str("- "),
Some(i) => {
std::fmt::Write::write_fmt(&mut prefix, format_args!("{}. ", &i)).unwrap();
std::fmt::Write::write_fmt(&mut prefix, format_args!("{}. ", &i))
.unwrap();
*i += 1;
}
};
Expand Down Expand Up @@ -218,18 +219,18 @@ pub fn write_one_md<W>(out: &mut Output<W>, node: &MdqNode)
}

pub fn write_line<E, W>(out: &mut Output<W>, elems: &[E])
where
E: Borrow<Inline>,
W: Write,
where
E: Borrow<Inline>,
W: Write,
{
for elem in elems {
write_inline_element(out, elem.borrow());
}
}

pub fn write_inline_element<W>(out: &mut Output<W>, elem: &Inline)
where
W: Write,
where
W: Write,
{
match elem {
Inline::Span { variant, children } => {
Expand All @@ -256,8 +257,8 @@ pub fn write_inline_element<W>(out: &mut Output<W>, elem: &Inline)
}

fn line_to_string<E>(line: &[E]) -> String
where
E: Borrow<Inline>,
where
E: Borrow<Inline>,
{
let bytes: Vec<u8> = Vec::with_capacity(line.len() * 10); // rough guess
let mut out = Output::new(bytes);
Expand Down
16 changes: 10 additions & 6 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct PreWriter<'a, W: Write> {
output: &'a mut Output<W>,
}

#[derive(Debug,PartialEq)]
#[derive(Debug, PartialEq)]
pub enum Block {
/// A plain block; just paragraph text.
Plain,
Expand All @@ -31,7 +31,7 @@ pub enum Block {
/// indentation continues.§
///
/// (where `§` signifies the start and end of an inlined paragraph)
Inlined(usize)
Inlined(usize),
}

impl<W: Write> Output<W> {
Expand Down Expand Up @@ -74,7 +74,11 @@ impl<W: Write> Output<W> {
}

pub fn push_block(&mut self, block: Block) {
let prev_is_inline = self.indents.last().map(|b| matches!(b, Block::Inlined(_))).unwrap_or(false);
let prev_is_inline = self
.indents
.last()
.map(|b| matches!(b, Block::Inlined(_)))
.unwrap_or(false);
let (block_close, add_newlines) = match block {
Block::Plain => (BlockClose::NoAction, true),
b @ Block::Quote => {
Expand Down Expand Up @@ -135,9 +139,9 @@ impl<W: Write> Output<W> {
fn write_indent(&mut self) {
for idx in 0..self.indents.len() {
match &self.indents[idx] {
Block::Plain => {},
Block::Quote => { self.write_raw(">")},
Block::Inlined(size) => {(0..*size).for_each(|_| self.write_raw(" "))},
Block::Plain => {}
Block::Quote => self.write_raw(">"),
Block::Inlined(size) => (0..*size).for_each(|_| self.write_raw(" ")),
}
}
}
Expand Down
33 changes: 18 additions & 15 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ impl Selector {
}

let result = match node {
MdqNode::Root { body } =>
SelectResult::Recurse(body),
MdqNode::Root { body } => SelectResult::Recurse(body),
MdqNode::Header { title, body, .. } => {
if let Selector::Heading(matcher) = self {
if matcher.matches(&Self::line_to_string(title)) {
Expand All @@ -82,17 +81,21 @@ impl Selector {
MdqNode::Paragraph { .. } => {
SelectResult::None // see TODO on Selector
}
MdqNode::BlockQuote { body } =>
SelectResult::Recurse(body),
MdqNode::List { starting_index, items } => {
MdqNode::BlockQuote { body } => SelectResult::Recurse(body),
MdqNode::List {
starting_index,
items,
} => {
let _is_ordered = starting_index.is_some(); // TODO use in selected
SelectResult::RecurseOwned(items
.iter()
.flat_map(|li| {
// TODO check selected
self.find_in_children(&li.item)
})
.collect())
SelectResult::RecurseOwned(
items
.iter()
.flat_map(|li| {
// TODO check selected
self.find_in_children(&li.item)
})
.collect(),
)
}
MdqNode::Table { .. } => {
SelectResult::None // TODO need to recurse
Expand All @@ -106,7 +109,7 @@ impl Selector {
(_, _) => false,
};
if matched {
SelectResult::Found(vec!(node))
SelectResult::Found(vec![node])
} else {
SelectResult::None
}
Expand All @@ -121,7 +124,7 @@ impl Selector {
Inline::Text { .. } => false,
};
if matched {
SelectResult::Found(vec!(node))
SelectResult::Found(vec![node])
} else {
SelectResult::None
}
Expand All @@ -132,7 +135,7 @@ impl Selector {
SelectResult::Recurse(children) => self.find_in_children(children),
SelectResult::RecurseOwned(children) => {
children.iter().flat_map(|elem| self.find(elem)).collect()
},
}
SelectResult::None => Vec::new(),
}
}
Expand Down

0 comments on commit abfcde0

Please sign in to comment.