diff --git a/prql-compiler/src/lib.rs b/prql-compiler/src/lib.rs index d1273ce94be9..2288b537b201 100644 --- a/prql-compiler/src/lib.rs +++ b/prql-compiler/src/lib.rs @@ -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 = Lazy::new(|| Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid PRQL version number")); @@ -134,6 +135,12 @@ pub enum Target { Sql(Option), } +impl Default for Target { + fn default() -> Self { + Self::Sql(None) + } +} + impl Target { pub fn names() -> Vec { sql::Dialect::names() @@ -143,6 +150,27 @@ impl Target { } } +impl FromStr for Target { + type Err = Error; + + fn from_str(s: &str) -> Result { + // 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.") + .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 { @@ -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###" + 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, + }, + ) + "###); + } +} diff --git a/prql-compiler/src/sql/dialect.rs b/prql-compiler/src/sql/dialect.rs index efcee40459ce..ecf767817cff 100644 --- a/prql-compiler/src/sql/dialect.rs +++ b/prql-compiler/src/sql/dialect.rs @@ -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 diff --git a/prql-js/src/lib.rs b/prql-js/src/lib.rs index aad322af1fbd..df26be5f40be 100644 --- a/prql-js/src/lib.rs +++ b/prql-js/src/lib.rs @@ -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] @@ -103,13 +103,11 @@ impl CompileOptions { impl From 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, } }