(input: TokenStream, allow_dir: bool)
| 226 | |
| 227 | impl PyCompileArgs { |
| 228 | fn parse(input: TokenStream, allow_dir: bool) -> Result<Self, Diagnostic> { |
| 229 | let mut module_name = None; |
| 230 | let mut mode = None; |
| 231 | let mut source: Option<CompilationSource> = None; |
| 232 | let mut crate_name = None; |
| 233 | |
| 234 | fn assert_source_empty(source: &Option<CompilationSource>) -> Result<(), syn::Error> { |
| 235 | if let Some(source) = source { |
| 236 | Err(syn::Error::new( |
| 237 | source.span.0, |
| 238 | "Cannot have more than one source", |
| 239 | )) |
| 240 | } else { |
| 241 | Ok(()) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | syn::meta::parser(|meta| { |
| 246 | let ident = meta |
| 247 | .path |
| 248 | .get_ident() |
| 249 | .ok_or_else(|| meta.error("unknown arg"))?; |
| 250 | let check_str = || meta.value()?.call(parse_str); |
| 251 | let str_path = || { |
| 252 | let s = check_str()?; |
| 253 | let mut base_path = s |
| 254 | .span() |
| 255 | .unwrap() |
| 256 | .local_file() |
| 257 | .ok_or_else(|| err_span!(s, "filepath literal has no span information"))?; |
| 258 | base_path.pop(); |
| 259 | Ok::<_, syn::Error>((base_path, PathBuf::from(s.value()))) |
| 260 | }; |
| 261 | if ident == "mode" { |
| 262 | let s = check_str()?; |
| 263 | match s.value().parse() { |
| 264 | Ok(mode_val) => mode = Some(mode_val), |
| 265 | Err(e) => bail_span!(s, "{}", e), |
| 266 | } |
| 267 | } else if ident == "module_name" { |
| 268 | module_name = Some(check_str()?.value()) |
| 269 | } else if ident == "source" { |
| 270 | assert_source_empty(&source)?; |
| 271 | let code = check_str()?.value(); |
| 272 | source = Some(CompilationSource { |
| 273 | kind: CompilationSourceKind::SourceCode(code), |
| 274 | span: (ident.span(), meta.input.cursor().span()), |
| 275 | }); |
| 276 | } else if ident == "file" { |
| 277 | assert_source_empty(&source)?; |
| 278 | let (base, rel_path) = str_path()?; |
| 279 | source = Some(CompilationSource { |
| 280 | kind: CompilationSourceKind::File { base, rel_path }, |
| 281 | span: (ident.span(), meta.input.cursor().span()), |
| 282 | }); |
| 283 | } else if ident == "dir" { |
| 284 | if !allow_dir { |
| 285 | bail_span!(ident, "py_compile doesn't accept dir") |
no test coverage detected