Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jpschorr committed Jul 9, 2024
1 parent 69297fd commit d30b53c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions partiql-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ indexmap = { version = "1.9", default-features = false }
rust_decimal = { version = "1.25.0", default-features = false, features = ["std"] }
serde = { version = "1.*", features = ["derive"], optional = true }
pretty = "0.12"
thiserror = "1.0"

[dev-dependencies]
partiql-parser = { path = "../partiql-parser", version = "0.8" }
Expand Down
52 changes: 26 additions & 26 deletions partiql-ast/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::ast::*;
use pretty::{Arena, DocAllocator, DocBuilder, Pretty};
use std::io;
use std::io::Write;
use std::string::FromUtf8Error;
use thiserror::Error;

const MINOR_NEST_INDENT: isize = 2;
const SUBQUERY_INDENT: isize = 6;

Expand All @@ -13,16 +16,28 @@ pub(crate) trait PrettyDoc {
A: Clone;
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ToPrettyError {
#[error("IO error: `{0}`")]
IoError(#[from] std::io::Error),

#[error("FromUtf8Error: `{0}`")]
FromUtf8Error(#[from] FromUtf8Error),
}

type ToPrettyResult<T> = Result<T, ToPrettyError>;

pub trait ToPretty {
fn to_pretty_string(&self, width: usize) -> io::Result<String> {
/// Pretty-prints to a `String`.
fn to_pretty_string(&self, width: usize) -> ToPrettyResult<String> {
let mut out = Vec::new();
self.to_pretty(width, &mut out)?;
// TODO error instead of unwrap
Ok(String::from_utf8(out).unwrap())
Ok(String::from_utf8(out)?)
}

/// Writes a rendered document to a `std::io::Write` object.
fn to_pretty<W>(&self, width: usize, out: &mut W) -> io::Result<()>
/// Pretty-prints to a `std::io::Write` object.
fn to_pretty<W>(&self, width: usize, out: &mut W) -> ToPrettyResult<()>
where
W: ?Sized + io::Write;
}
Expand All @@ -31,13 +46,13 @@ impl<T> ToPretty for AstNode<T>
where
T: PrettyDoc,
{
fn to_pretty<W>(&self, width: usize, out: &mut W) -> io::Result<()>
fn to_pretty<W>(&self, width: usize, out: &mut W) -> ToPrettyResult<()>
where
W: ?Sized + Write,
{
let arena = Arena::new();
let DocBuilder(_, doc) = self.node.pretty_doc::<_, ()>(&arena);
doc.render(width, out)
Ok(doc.render(width, out)?)
}
}

Expand All @@ -56,21 +71,6 @@ where
}
}

impl<T> PrettyDoc for &T
where
T: PrettyDoc,
{
#[inline]
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
where
D: DocAllocator<'b, A>,
D::Doc: Clone,
A: Clone,
{
self.pretty_doc(arena)
}
}

impl<T> PrettyDoc for Box<T>
where
T: PrettyDoc,
Expand Down Expand Up @@ -117,7 +117,9 @@ impl PrettyDoc for TopLevelQuery {
D::Doc: Clone,
A: Clone,
{
// TODO With
if self.with.is_some() {
todo!("WITH Clause")
}
self.query.pretty_doc(arena)
}
}
Expand Down Expand Up @@ -928,8 +930,6 @@ impl PrettyDoc for Join {
predicate,
} = self;

//let left = left.pretty_doc(arena).group();
//let right = right.pretty_doc(arena).group();
let arms = [left.as_ref(), right.as_ref()];
let kw_join = match kind {
JoinKind::Cross => " CROSS JOIN ",
Expand All @@ -940,7 +940,7 @@ impl PrettyDoc for Join {
};

match (kind, predicate) {
(JoinKind::Cross, Some(pred)) => {
(JoinKind::Cross, Some(_)) => {
todo!("CROSS JOIN with predicate")
}
(JoinKind::Cross, None) => pretty_list(arms, 0, arena),
Expand Down

0 comments on commit d30b53c

Please sign in to comment.