diff --git a/.changepacks/changepack_log_parser_round41.json b/.changepacks/changepack_log_parser_round41.json new file mode 100644 index 00000000..7bea7f8a --- /dev/null +++ b/.changepacks/changepack_log_parser_round41.json @@ -0,0 +1 @@ +{"changes":{"crates/vespera/Cargo.toml":"Patch","crates/vespera_core/Cargo.toml":"Patch","crates/vespera_macro/Cargo.toml":"Patch","crates/vespera_inprocess/Cargo.toml":"Patch","crates/vespera_jni/Cargo.toml":"Patch","libs/vespera-bridge/build.gradle.kts":"Patch","libs/vespera-bridge-gradle-plugin/build.gradle.kts":"Patch"},"note":"schema parser fixes: a raw string literal (r\"...\" / r#\"...\"#) as a serde default or rename value is read on the raw-token fallback path too; crate::Schema, self::Schema and super::Schema name the bare Schema component instead of a CrateSchema / SelfSchema / SuperSchema component.","date":"2026-11-24T00:00:00.000Z"} diff --git a/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs b/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs index 491111cf..53ba9cad 100644 --- a/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs +++ b/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs @@ -12,14 +12,27 @@ pub(super) fn quoted_value_after_key(tokens: &str, key: &str) -> Option None } -/// The value of the `"..."` literal `value_part` starts with (after -/// whitespace); a value of any other kind yields `None`. The closing quote is -/// the first one not escaped by a backslash, and the literal's escapes -/// (`\"`, `\\`, `\u{..}`) are decoded by `syn`, as rustc decodes them. +/// The value of the string literal `value_part` starts with (after +/// whitespace), plain (`"..."`) or raw (`r"..."`, `r#"..."#`); a value of any +/// other kind yields `None`. A plain literal's closing quote is the first one +/// not escaped by a backslash, a raw literal's is the quote followed by the +/// same number of `#`. The literal is decoded by `syn`, as rustc decodes it. fn leading_string_literal(value_part: &str) -> Option { - let body = value_part.trim_start().strip_prefix('"')?; + let text = value_part.trim_start(); + let end = if text.starts_with('"') { + quoted_literal_end(text)? + } else { + raw_literal_end(text)? + }; + syn::parse_str::(&text[..end]) + .ok() + .map(|literal| literal.value()) +} + +/// Byte index just past the plain literal `text` (which starts with `"`). +fn quoted_literal_end(text: &str) -> Option { let mut escaped = false; - let end = body.char_indices().find_map(|(index, ch)| { + text.char_indices().skip(1).find_map(|(index, ch)| { if escaped { escaped = false; None @@ -27,12 +40,21 @@ fn leading_string_literal(value_part: &str) -> Option { escaped = true; None } else { - (ch == '"').then_some(index) + (ch == '"').then_some(index + 1) } - })?; - syn::parse_str::(&format!("\"{}\"", &body[..end])) - .ok() - .map(|literal| literal.value()) + }) +} + +/// Byte index just past the raw literal `text` (`r` + `#`s + `"` ... `"` + +/// the same `#`s); `None` when `text` opens no raw literal. +fn raw_literal_end(text: &str) -> Option { + let after_r = text.strip_prefix('r')?; + let hashes = after_r.len() - after_r.trim_start_matches('#').len(); + let body = after_r[hashes..].strip_prefix('"')?; + let closing = format!("\"{}", "#".repeat(hashes)); + let close = body.find(&closing)?; + // `r` + hashes + `"` + body + closing + Some(1 + hashes + 1 + close + closing.len()) } pub(super) fn contains_standalone_word(tokens: &str, word: &str) -> bool { @@ -194,6 +216,32 @@ mod tests { ); } + /// The raw-token fallback reads a raw string literal value (`r"..."`, + /// `r#"..."#`) the same as a plain one, for `default` and `rename`. + #[rstest] + #[case::plain("\"f\"", "f")] + #[case::raw("r\"f\"", "f")] + #[case::raw_hashed("r#\"f\"#", "f")] + #[case::raw_keeps_backslash("r#\"a\\b\"#", "a\\b")] + fn test_fallback_reads_raw_and_plain_literal_values( + #[case] literal: &str, + #[case] value: &str, + ) { + let tokens: TokenStream = format!("@bogus, default = {literal}, rename = {literal}") + .parse() + .expect("token stream parses"); + let attr = create_attr_with_raw_tokens(tokens); + assert_eq!( + extract_default(std::slice::from_ref(&attr)), + Some(Some(value.to_string())) + ); + assert_eq!( + extract_field_rename(&[attr]).as_deref(), + Some(value), + "rename through the fallback" + ); + } + /// Test that a "default" occurring as part of a longer identifier is not /// a serde default: `scan_default_from_raw_tokens` skips it (the /// `key_positions` candidates must be whole keys), so the scan finds diff --git a/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs b/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs index 9e84dd7e..ba63001b 100644 --- a/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs +++ b/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs @@ -597,10 +597,21 @@ fn parse_type_impl( // `module::Schema` names its module-scoped key when one is registered: // a renamed `Schema` may share its name with an unrelated component. // Only without a registered key is the convention tried, a schema - // named `ModuleSchema` (or `moduleSchema`). + // named `ModuleSchema` (or `moduleSchema`). A path keyword parent + // (`crate::Schema`, `self::Schema`, `super::Schema`) is no module + // name, so it never selects a `CrateSchema`-style component. let resolved_name = match scoped_key() { Some(key) => key, - None if type_name == "Schema" && path.segments.len() > 1 => { + None if type_name == "Schema" + && path.segments.len() > 1 + && !matches!( + path.segments[path.segments.len() - 2] + .ident + .to_string() + .as_str(), + "crate" | "self" | "super" + ) => + { // The parent module name (e.g., "user" from "crate::models::user::Schema") let parent_name = path.segments[path.segments.len() - 2] .ident @@ -929,3 +940,5 @@ mod tests_arms; mod tests_const; #[cfg(test)] mod tests_instances; +#[cfg(test)] +mod tests_path_keyword; diff --git a/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_path_keyword.rs b/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_path_keyword.rs new file mode 100644 index 00000000..991014bd --- /dev/null +++ b/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_path_keyword.rs @@ -0,0 +1,26 @@ +//! `crate::Schema` / `self::Schema` / `super::Schema` (sidecar of +//! `conversion`; `use super::*` semantics preserved): a path keyword is no +//! module name, so the `Schema` convention never picks a component +//! named after the keyword. + +use rstest::rstest; + +use super::*; + +/// The bare `Schema` and a component named `CrateSchema` (`SelfSchema` / +/// `SuperSchema`) are both known; a keyword-qualified `Schema` names the bare +/// one, never the convention's `Schema` component. +#[rstest] +#[case::crate_root("crate::Schema", "CrateSchema")] +#[case::self_module("self::Schema", "SelfSchema")] +#[case::super_module("super::Schema", "SuperSchema")] +fn path_keyword_parent_names_the_bare_schema(#[case] ty_src: &str, #[case] other: &str) { + let known = HashSet::from(["Schema", other]); + let defs: HashMap = HashMap::new(); + let ty: Type = syn::parse_str(ty_src).unwrap(); + let schema_ref = parse_type_to_schema_ref(&ty, &known, &defs); + let SchemaRef::Ref(reference) = schema_ref else { + panic!("a $ref for {ty_src}: {schema_ref:?}"); + }; + assert_eq!(reference.ref_path, "#/components/schemas/Schema"); +}