Repository navigation
[codex] add openapi backend emission demo #2410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e31d0ca
add openapi backend emission demo
briansrls 1315834
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 152ffef
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 2514277
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 5b34b81
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 8142cd2
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 4c13742
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls aaf0f1c
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 38f8355
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls e100795
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 10b139b
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 5e231c6
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls 044d8b2
Merge remote-tracking branch 'origin/main' into session/sleek-eagle-557
briansrls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,6 +250,101 @@ pub fn project_markdown_documentation(dag: &Dag) -> Result<String, ProjectOpenAp | |
| Ok(out) | ||
| } | ||
|
|
||
| pub fn project_rust_backend_service(dag: &Dag) -> Result<String, ProjectOpenApiError> { | ||
| let routes = extract_rest_routes(dag)?; | ||
| let mut out = String::from( | ||
| r#"const ROUTES: &[(&str, &str)] = &[ | ||
| "#, | ||
| ); | ||
| for route in &routes { | ||
| out.push_str(" ("); | ||
| out.push_str(&rust_string_literal(&route.method)); | ||
| out.push_str(", "); | ||
| out.push_str(&rust_string_literal(&route.path)); | ||
| out.push_str("),\n"); | ||
| } | ||
| out.push_str( | ||
| r#"]; | ||
|
|
||
| fn main() { | ||
| let mut args = std::env::args().skip(1); | ||
| let Some(method) = args.next() else { | ||
| print_routes(); | ||
| return; | ||
| }; | ||
| let Some(path) = args.next() else { | ||
| print_routes(); | ||
| return; | ||
| }; | ||
|
|
||
| for (route_method, route_template) in ROUTES { | ||
| if method.eq_ignore_ascii_case(route_method) && path_matches(route_template, &path) { | ||
| println!("200 {} {}", route_method, route_template); | ||
| return; | ||
| } | ||
| } | ||
| println!("404 {} {}", method.to_ascii_uppercase(), path); | ||
| } | ||
|
|
||
| fn print_routes() { | ||
| for (method, route_template) in ROUTES { | ||
| println!("{} {}", method, route_template); | ||
| } | ||
| } | ||
|
|
||
| fn path_matches(template: &str, candidate: &str) -> bool { | ||
| let template = template.as_bytes(); | ||
| let candidate = candidate.as_bytes(); | ||
| let mut t = 0; | ||
| let mut c = 0; | ||
| while t < template.len() { | ||
| if template[t] == b'{' { | ||
| let Some(close_offset) = template[t + 1..].iter().position(|byte| *byte == b'}') else { | ||
| return false; | ||
| }; | ||
| let close = t + 1 + close_offset; | ||
| let after_param = close + 1; | ||
| if after_param == template.len() { | ||
| return c < candidate.len(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BLOCKING: The terminal ParamToken branch accepts any non-empty suffix, so /users/{id} also matches /users/42/extra, breaking THESIS omni-emission coherence between the OpenAPI path and runnable backend. |
||
| } | ||
| let next_param = template[after_param..] | ||
| .iter() | ||
| .position(|byte| *byte == b'{') | ||
| .map(|offset| after_param + offset) | ||
| .unwrap_or(template.len()); | ||
| let literal = &template[after_param..next_param]; | ||
| let Some(next_match) = find_subslice(&candidate[c..], literal) else { | ||
| return false; | ||
| }; | ||
| if next_match == 0 { | ||
| return false; | ||
| } | ||
| c += next_match; | ||
| t = after_param; | ||
| } else { | ||
| if candidate.get(c) != Some(&template[t]) { | ||
| return false; | ||
| } | ||
| t += 1; | ||
| c += 1; | ||
| } | ||
| } | ||
| c == candidate.len() | ||
| } | ||
|
|
||
| fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> { | ||
| if needle.is_empty() { | ||
| return Some(0); | ||
| } | ||
| haystack | ||
| .windows(needle.len()) | ||
| .position(|window| window == needle) | ||
| } | ||
| "#, | ||
| ); | ||
| Ok(out) | ||
| } | ||
|
|
||
| fn append_path_parameter_yaml(out: &mut String, parameter: &str) { | ||
| out.push_str(" - name: "); | ||
| out.push_str(&yaml_double_quoted(parameter)); | ||
|
|
@@ -468,6 +563,25 @@ fn yaml_plain_operation_id(method: &str, path: &str) -> String { | |
| format!("{}_{}", method.to_ascii_lowercase(), suffix) | ||
| } | ||
|
|
||
| fn rust_string_literal(value: &str) -> String { | ||
| let mut literal = String::from("\""); | ||
| for ch in value.chars() { | ||
| match ch { | ||
| '\\' => literal.push_str("\\\\"), | ||
| '"' => literal.push_str("\\\""), | ||
| '\n' => literal.push_str("\\n"), | ||
| '\r' => literal.push_str("\\r"), | ||
| '\t' => literal.push_str("\\t"), | ||
| other if other.is_control() => { | ||
| write!(&mut literal, "\\u{{{:X}}}", other as u32).expect("write to String"); | ||
| } | ||
| other => literal.push(other), | ||
| } | ||
| } | ||
| literal.push('"'); | ||
| literal | ||
| } | ||
|
|
||
| fn markdown_table_cell(value: &str) -> String { | ||
| value.replace('\\', "\\\\").replace('|', "\\|") | ||
| } | ||
|
|
@@ -613,4 +727,9 @@ data service_operations: List<DemoOperation> = [ | |
| assert_eq!(markdown_code_span_table_cell("`x"), "`` `x ``"); | ||
| assert_eq!(markdown_code_span_table_cell("x`"), "`` x` ``"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rust_backend_projection_escapes_route_literals() { | ||
| assert_eq!(rust_string_literal("/a\"b\\c\n"), "\"/a\\\"b\\\\c\\n\""); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BLOCKING: This expands hand-written Rust under src/v3, but the changed text only names Brief #2219 and does not provide the exact P5 receipt required for new hand Rust.