| 45 | } |
| 46 | |
| 47 | pub fn parse_route(&mut self) -> Result<Route, String> { |
| 48 | let annotation = self.parse_route_annotation(); |
| 49 | let mut docs = String::new(); |
| 50 | let mut path = (String::from("/"), Vec::new()); |
| 51 | |
| 52 | // Check if this is a top-level route declaration |
| 53 | let is_top_route = self.check_identifier("route"); |
| 54 | if is_top_route { |
| 55 | self.advance(); // consume 'route' |
| 56 | path = self.parse_path()?; |
| 57 | self.consume(TokenType::OpenBrace, "Expect '{' after route path")?; |
| 58 | |
| 59 | docs = self.parse_docs(); |
| 60 | } |
| 61 | |
| 62 | let mut endpoints = Vec::new(); |
| 63 | while !self.is_at_end() && !self.check(TokenType::CloseBrace) { |
| 64 | endpoints.push(self.parse_endpoint()?); |
| 65 | } |
| 66 | |
| 67 | if is_top_route { |
| 68 | self.consume(TokenType::CloseBrace, "Expect '}' after route body")?; |
| 69 | } |
| 70 | |
| 71 | Ok(Route { |
| 72 | annotation, |
| 73 | prefix: path.0, |
| 74 | params: path.1, |
| 75 | endpoints, |
| 76 | docs, |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | fn parse_endpoint(&mut self) -> Result<Endpoint, String> { |
| 81 | let annotation = self.parse_route_annotation(); |