Skip to content
Merged
76 changes: 75 additions & 1 deletion prql-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ pub mod sql;
mod test;
mod utils;

pub use error::{downcast, ErrorMessage, ErrorMessages, SourceLocation, Span};
pub use error::{downcast, Error, ErrorMessage, ErrorMessages, Reason, SourceLocation, Span};
pub use utils::IntoOnly;

use once_cell::sync::Lazy;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

pub static PRQL_VERSION: Lazy<Version> =
Lazy::new(|| Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid PRQL version number"));
Expand Down Expand Up @@ -134,6 +135,12 @@ pub enum Target {
Sql(Option<sql::Dialect>),
}

impl Default for Target {
fn default() -> Self {
Self::Sql(None)
}
}

impl Target {
pub fn names() -> Vec<String> {
sql::Dialect::names()
Expand All @@ -143,6 +150,27 @@ impl Target {
}
}

impl FromStr for Target {
type Err = Error;

fn from_str(s: &str) -> Result<Target, Self::Err> {
// We have a closure here because we can't create the error in the
// pipeline, since it needs to be in two places, and we'd need to clone.
// (Though possibly it's too optimize-y.)
let not_found_error = |s| {
Error::new(Reason::NotFound {
name: format!("{s:?}"),
namespace: "target".to_string(),
})
};
s.strip_prefix("sql.")
Comment thread
max-sixty marked this conversation as resolved.
Resolved
.ok_or_else(|| not_found_error(s))
.map(sql::Dialect::from_str)?
.map(|x| Target::Sql(Some(x)))
.map_err(|_| not_found_error(s))
}
}

/// Compilation options for SQL backend of the compiler.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Options {
Expand Down Expand Up @@ -242,3 +270,49 @@ pub mod json {
serde_json::from_str(json).map_err(|e| error::downcast(anyhow::anyhow!(e)))
}
}

#[cfg(test)]
mod tests {
use crate::Target;
use insta::assert_debug_snapshot;
use std::str::FromStr;

#[test]
fn test_target_from_str() {
assert_debug_snapshot!(Target::from_str("sql.postgres"), @r###"
Comment thread
max-sixty marked this conversation as resolved.
Resolved
Ok(
Sql(
Some(
PostgreSql,
),
),
)
"###);

assert_debug_snapshot!(Target::from_str("sql.poostgres"), @r###"
Err(
Error {
span: None,
reason: NotFound {
name: "\"sql.poostgres\"",
namespace: "target",
},
help: None,
},
)
"###);

assert_debug_snapshot!(Target::from_str("postgres"), @r###"
Err(
Error {
span: None,
reason: NotFound {
name: "\"postgres\"",
namespace: "target",
},
help: None,
},
)
"###);
}
}
22 changes: 22 additions & 0 deletions prql-compiler/src/sql/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ impl DialectHandler for DuckDbDialect {
}
}

#[cfg(test)]
mod tests {
use super::Dialect;
use insta::assert_debug_snapshot;
use std::str::FromStr;

#[test]
fn test_dialect_from_str() {
assert_debug_snapshot!(Dialect::from_str("postgres"), @r###"
Ok(
PostgreSql,
)
"###);

assert_debug_snapshot!(Dialect::from_str("foo"), @r###"
Err(
VariantNotFound,
)
"###);
}
}

/*
## Set operations support matrix

Expand Down
8 changes: 3 additions & 5 deletions prql-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod utils;

use std::str::FromStr;

use prql_compiler::sql::Dialect;
use prql_compiler::{sql::Dialect, Target};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -103,13 +103,11 @@ impl CompileOptions {

impl From<CompileOptions> for prql_compiler::Options {
fn from(o: CompileOptions) -> Self {
let maybe_dialect = o.target.strip_prefix("sql.");
let dialect =
maybe_dialect.map(|dialect| Dialect::from_str(dialect).unwrap_or(Dialect::Generic));
let target = Target::from_str(&o.target).unwrap_or(Target::Sql(Some(Dialect::Generic)));

prql_compiler::Options {
format: o.format,
target: prql_compiler::Target::Sql(dialect),
target,
signature_comment: o.signature_comment,
}
}
Expand Down