Given the contents of a Python file, extract the `script` metadata block with leading comment hashes removed and map its offsets to the original Python script. Given the following input string representing the contents of a Python script: ```python #!/usr/bin/env python3 # /// script # requires-python = '>=3.11' # dependencies = [ # 'requests<3', # 'rich', # ] # /// import requests print("
(contents: &str)
| 63 | /// |
| 64 | /// See: <https://peps.python.org/pep-0723/> |
| 65 | pub fn parse(contents: &str) -> Option<Self> { |
| 66 | let mut offset = 0; |
| 67 | while let Some(index) = FINDER.find(&contents.as_bytes()[offset..]) { |
| 68 | match Self::parse_at(contents, offset + index) { |
| 69 | Ok(tag) => return Some(tag), |
| 70 | Err(end) => offset = end, |
| 71 | } |
| 72 | } |
| 73 | None |
| 74 | } |
| 75 | |
| 76 | /// Returns the offset at which to resume searching if the block is invalid. An opening |
| 77 | /// marker inside an unclosed comment block would scan the same suffix and fail too. |
no test coverage detected