1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use anyhow::{bail, Context};
use cargo::{
	core::{Edition, Manifest, Registry, Summary, Target, TargetKind},
	util::OptVersionReq
};
use semver::{Comparator, Op, Version, VersionReq};
use std::{
	collections::{HashMap, HashSet, VecDeque},
	fmt::{self, Debug, Formatter},
	fs::File,
	io::{self, Read, Write},
	path::Path,
	process::{Command, Output},
	task::Poll
};
use syn::{Attribute, Ident, Item, ItemUse, Lit, LitStr, Meta, UsePath, UseTree, Visibility};
use unindent::Unindent;

type ScopeScope = HashMap<String, VecDeque<(LinkType, String)>>;

#[derive(Debug)]
pub struct Scope {
	// use statements and declared items. maps name to path.
	pub scope: ScopeScope,
	// private modules so that `pub use`'d items are considered inlined.
	pub privmods: HashSet<String>,
	// the scope included a wildcard use statement.
	pub has_glob_use: bool
}

#[derive(Debug, Eq, PartialEq)]
pub enum LinkType {
	/// Function statements.
	Function,

	/// `use` statement that links to a path.
	Use,

	/// `pub use` statement that links to the name pub used as.
	PubUse,

	/// Other statements we'll implement later.
	Other
}

fn make_prelude<const N: usize>(prelude: [(&'static str, &'static str); N]) -> ScopeScope {
	prelude
		.into_iter()
		.map(|(name, path)| {
			(
				name.into(),
				[(LinkType::Use, format!("::std::{path}::{name}"))]
					.into_iter()
					.collect()
			)
		})
		.collect()
}

impl Scope {
	fn insert<K, V>(&mut self, key: K, ty: LinkType, value: V)
	where
		K: Into<String>,
		V: Into<String>
	{
		self.scope
			.entry(key.into())
			.or_insert_with(VecDeque::new)
			.push_front((ty, value.into()));
	}

	/// Create a new scope from the Rust prelude.
	pub fn prelude(edition: Edition) -> Self {
		let mut scope = Scope {
			// https://doc.rust-lang.org/stable/std/prelude/index.html#prelude-contents
			scope: make_prelude([
				("Copy", "marker"),
				("Send", "marker"),
				("Sized", "marker"),
				("Sync", "marker"),
				("Unpin", "marker"),
				("Drop", "ops"),
				("Fn", "ops"),
				("FnMut", "ops"),
				("FnOnce", "ops"),
				("drop", "mem"),
				("Box", "boxed"),
				("ToOwned", "borrow"),
				("Clone", "clone"),
				("PartialEq", "cmp"),
				("PartialOrd", "cmp"),
				("Eq", "cmp"),
				("Ord", "cmp"),
				("AsRef", "convert"),
				("AsMut", "convert"),
				("Into", "convert"),
				("From", "convert"),
				("Default", "default"),
				("Iterator", "iter"),
				("Extend", "iter"),
				("IntoIterator", "iter"),
				("DoubleEndedIterator", "iter"),
				("ExactSizeIterator", "iter"),
				("Option", "option"),
				("Some", "option::Option"),
				("None", "option::Option"),
				("Result", "result"),
				("Ok", "result::Result"),
				("Err", "result::Result"),
				("String", "string"),
				("ToString", "string"),
				("Vec", "vec")
			]),
			privmods: HashSet::new(),
			has_glob_use: false
		};

		if edition >= Edition::Edition2021 {
			// https://blog.rust-lang.org/2021/05/11/edition-2021.html#additions-to-the-prelude
			for (key, value) in make_prelude([
				("TryInto", "convert"),
				("TryFrom", "convert"),
				("FromIterator", "iter")
			]) {
				scope.scope.insert(key, value);
			}
		}

		scope
	}
}

#[derive(Debug)]
pub struct CrateCode(String);

impl CrateCode {
	pub(crate) fn read_from_disk<P>(path: &P) -> anyhow::Result<CrateCode>
	where
		P: AsRef<Path> + ?Sized
	{
		let mut file = File::open(path)?;
		let mut buf = String::new();
		file.read_to_string(&mut buf)?;

		Ok(CrateCode(buf))
	}

	pub(crate) fn read_expansion<P>(manifest_path: &P, target: &Target) -> anyhow::Result<CrateCode>
	where
		P: AsRef<Path> + ?Sized
	{
		let mut cmd = Command::new("cargo");
		cmd.arg("+nightly").arg("rustc").arg(format!(
			"--manifest-path={}",
			manifest_path.as_ref().display()
		));
		match target.kind() {
			TargetKind::Lib(_) => {
				cmd.arg("--lib");
			},
			TargetKind::Bin => {
				cmd.arg("--bin").arg(target.name());
			},
			_ => {}
		};
		cmd.arg("--").arg("-Zunpretty=expanded");

		let Output {
			stdout,
			stderr,
			status
		} = cmd.output()
			.context("Failed to run cargo to expand crate content")?;

		if !status.success() {
			// Something bad happened during the compilation. Let's print
			// anything Cargo reported to us and return an error.
			io::stdout()
				.lock()
				.write_all(stderr.as_slice())
				.expect("Failed to write cargo errors to stdout");

			bail!("Cargo failed to expand the macros")
		}

		String::from_utf8(stdout)
			.context("Failed to convert cargo output to UTF-8")
			.map(CrateCode)
	}
}

#[derive(Debug)]
pub struct InputFile {
	/// The name of the crate.
	pub crate_name: String,
	/// The repository url (if specified).
	pub repository: Option<String>,
	/// The license field (if specified).
	pub license: Option<String>,
	/// The unmodified rustdoc string
	pub rustdoc: String,
	/// The crate-level dependencies, mapping the valid identifier in rust code to the (possibly
	/// renamed, containing invalid characters, etc.) crate name and version.
	pub dependencies: HashMap<String, Dependency>,
	/// The scope at the crate root.
	pub scope: Scope
}

pub struct Dependency {
	/// The crate name as it appears on crates.io.
	pub crate_name: String,

	/// The version requirement of the dependency.
	pub req: VersionReq,

	/// The exact version of the dependency.
	pub version: Version
}

impl Dependency {
	fn new(crate_name: String, req: VersionReq, version: Version) -> Self {
		Self {
			crate_name,
			req,
			version
		}
	}

	pub fn as_tuple(&self) -> (&str, Option<&Version>) {
		(self.crate_name.as_str(), Some(&self.version))
	}
}

impl Debug for Dependency {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		write!(
			f,
			"{} = \"{}\" ({})",
			self.crate_name, self.req, self.version
		)
	}
}

pub fn read_code(
	manifest: &Manifest,
	registry: &mut dyn Registry,
	code: CrateCode
) -> anyhow::Result<InputFile> {
	let crate_name = manifest.name().to_string();
	let repository = manifest.metadata().repository.clone();
	let license = manifest.metadata().license.clone();

	let file = syn::parse_file(code.0.as_str())?;

	let rustdoc = read_rustdoc_from_file(&file)?;
	let dependencies = resolve_dependencies(manifest, registry)?;
	let scope = read_scope_from_file(manifest, &file)?;

	Ok(InputFile {
		crate_name,
		repository,
		license,
		rustdoc,
		dependencies,
		scope
	})
}

fn read_rustdoc_from_file(file: &syn::File) -> anyhow::Result<String> {
	let mut doc = String::new();
	for attr in &file.attrs {
		if attr.path.is_ident("doc") {
			if let Some(str) = parse_doc_attr(attr)? {
				// always push a newline: unindent ignores the first line
				doc.push('\n');
				doc.push_str(&str.value());
			}
		}
	}
	Ok(doc.unindent())
}

fn parse_doc_attr(input: &Attribute) -> syn::Result<Option<LitStr>> {
	input.parse_meta().and_then(|meta| {
		Ok(match meta {
			Meta::NameValue(kv) => Some(match kv.lit {
				Lit::Str(str) => str,
				lit => return Err(syn::Error::new(lit.span(), "Expected string literal"))
			}),
			_ => None
		})
	})
}

fn resolve_dependencies(
	manifest: &Manifest,
	registry: &mut dyn Registry
) -> anyhow::Result<HashMap<String, Dependency>> {
	let mut deps = HashMap::new();

	// we currently insert our own crate as a dependency to allow doc links referencing ourself.
	// however, we might want to change this so that custom doc urls can be used, so that e.g.
	// the repository readme points to some rustdoc generated from the master branch, instead of
	// the last release. Also, the version in the current manifest might not be released, so
	// those links might be dead.
	let version = manifest.version().clone();
	deps.insert(
		manifest.name().to_string().replace('-', "_"),
		Dependency::new(
			manifest.name().to_string(),
			[Comparator {
				op: Op::Exact,
				major: version.major,
				minor: Some(version.minor),
				patch: Some(version.patch),
				pre: version.pre.clone()
			}]
			.into_iter()
			.collect(),
			version
		)
	);

	let pending_deps = manifest
		.dependencies()
		.iter()
		.filter_map(|dep| {
			let dep_name = dep.name_in_toml().to_string().replace('-', "_");
			let mut add_dep = |crate_name: String, req: &VersionReq, version: &Version| {
				if deps
					.get(&dep_name)
					.map(|dep| &dep.version < version)
					.unwrap_or(true)
				{
					deps.insert(
						dep_name.clone(),
						Dependency::new(crate_name, req.clone(), version.clone())
					);
				}
			};

			match dep.version_req() {
				OptVersionReq::Locked(version, req) => {
					add_dep(dep.package_name().to_string(), req, version);
					None
				},
				OptVersionReq::Req(req) => {
					let mut f = |sum: Summary| {
						add_dep(sum.name().to_string(), req, sum.version());
					};
					Some(registry.query(dep, &mut f, false))
				},
				_ => {
					let mut f = |sum: Summary| {
						add_dep(sum.name().to_string(), &VersionReq::STAR, sum.version());
					};
					Some(registry.query(dep, &mut f, false))
				}
			}
		})
		.collect::<Vec<_>>();

	registry
		.block_until_ready()
		.expect("Failed to wait for dependency resolver");

	for dep in pending_deps {
		match dep {
			Poll::Ready(dep) => dep.expect("Failed to resolve dependency"),
			_ => unreachable!("We've waited for the dependency to be ready")
		}
	}

	Ok(deps)
}

macro_rules! scope_insert {
	($scope:ident, $crate_name:expr, $ident:expr) => {{
		let ident: &::syn::Ident = $ident;
		$scope.insert(
			ident.to_string(),
			LinkType::Other,
			format!("::{}::{ident}", $crate_name)
		);
	}};
}

fn read_scope_from_file(manifest: &Manifest, file: &syn::File) -> anyhow::Result<Scope> {
	let crate_name = manifest.name().replace('-', "_");
	let mut scope = Scope::prelude(manifest.edition());

	for i in &file.items {
		match i {
			Item::Const(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Enum(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::ExternCrate(i) if i.ident != "self" && i.rename.is_some() => {
				let krate = &i.rename.as_ref().unwrap().1;
				scope.insert(krate.to_string(), LinkType::Other, format!("::{}", i.ident));
			},
			Item::Fn(i) => add_fun_to_scope(&mut scope, &crate_name, &i.sig.ident),
			Item::Macro(i) if i.ident.is_some() => {
				add_macro_to_scope(&mut scope, &crate_name, i.ident.as_ref().unwrap())
			},
			Item::Macro2(i) => add_macro_to_scope(&mut scope, &crate_name, &i.ident),
			Item::Mod(i) => match i.vis {
				Visibility::Public(_) => {
					scope_insert!(scope, crate_name, &i.ident)
				},
				_ => {
					scope.privmods.insert(i.ident.to_string());
				}
			},
			Item::Static(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Struct(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Trait(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::TraitAlias(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Type(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Union(i) => scope_insert!(scope, crate_name, &i.ident),
			Item::Use(i) if !is_prelude_import(i) => {
				add_use_tree_to_scope(&mut scope, &crate_name, &i.vis, String::new(), &i.tree)
			},
			_ => {}
		};
	}

	// remove privmod imports from scope
	for values in &mut scope.scope.values_mut() {
		let mut i = 0;
		while i < values.len() {
			if values[i].0 == LinkType::Use {
				let path = &values[i].1;
				if (!path.starts_with("::") || path.starts_with(&format!("::{crate_name}::")))
					&& Some(path.split("::").collect::<Vec<_>>())
						.map(|segments| segments.len() > 1 && scope.privmods.contains(segments[0]))
						.unwrap()
				{
					values.remove(i);
					continue;
				}
			}

			i += 1;
		}
	}

	Ok(scope)
}

fn add_fun_to_scope(scope: &mut Scope, crate_name: &str, ident: &Ident) {
	let path = format!("::{crate_name}::{ident}");
	scope.insert(ident.to_string(), LinkType::Function, &path);
	scope.insert(format!("{ident}()"), LinkType::Function, &path);
}

fn add_macro_to_scope(scope: &mut Scope, crate_name: &str, ident: &Ident) {
	let path = format!("::{crate_name}::{ident}");
	scope.insert(ident.to_string(), LinkType::Other, &path);
	scope.insert(format!("{ident}!"), LinkType::Other, &path)
}

fn add_use_tree_to_scope(
	scope: &mut Scope,
	crate_name: &str,
	vis: &Visibility,
	prefix: String,
	tree: &UseTree
) {
	match tree {
		UseTree::Path(path) => add_use_tree_to_scope(
			scope,
			crate_name,
			vis,
			format!("{prefix}{}::", path.ident),
			&path.tree
		),
		UseTree::Name(name) => {
			// skip `pub use dependency;` style uses; they don't add any unknown elements to the scope
			if !prefix.is_empty() {
				add_use_item_to_scope(scope, crate_name, vis, &prefix, &name.ident, &name.ident);
			}
		},
		UseTree::Rename(name) => {
			add_use_item_to_scope(scope, crate_name, vis, &prefix, &name.rename, &name.ident);
		},
		UseTree::Glob(_) => {
			scope.has_glob_use = true;
		},
		UseTree::Group(group) => {
			for tree in &group.items {
				add_use_tree_to_scope(scope, crate_name, vis, prefix.clone(), tree);
			}
		},
	};
}

fn add_use_item_to_scope(
	scope: &mut Scope,
	crate_name: &str,
	vis: &Visibility,
	prefix: &str,
	rename: &Ident,
	ident: &Ident
) {
	if matches!(vis, Visibility::Public(_)) {
		scope.insert(
			rename.to_string(),
			LinkType::PubUse,
			format!("::{crate_name}::{rename}")
		);
	}
	scope.insert(
		rename.to_string(),
		LinkType::Use,
		format!("{prefix}{ident}")
	);
}

fn is_prelude_import(item_use: &ItemUse) -> bool {
	match &item_use.tree {
		UseTree::Path(UsePath { ident, tree, .. }) if ident == "std" => match tree.as_ref() {
			UseTree::Path(UsePath { ident, .. }) => ident == "prelude",
			_ => false
		},
		_ => false
	}
}