hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

format.ha (1789B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use fmt;
      5 use hare::unparse;
      6 use io;
      7 use time::chrono;
      8 use time::date;
      9 
     10 // Formats a set of tags to an [[io::handle]] in "+tag1-tag2" format.
     11 export fn format_tags(
     12 	out: io::handle,
     13 	tags: ([]str | []tag),
     14 ) (size | io::error) = {
     15 	let n = 0z;
     16 	match (tags) {
     17 	case let tags: []str =>
     18 		for (let tag .. tags) {
     19 			n += fmt::fprintf(out, "+{}", tag)?;
     20 		};
     21 	case let tags: []tag =>
     22 		for (let tag .. tags) {
     23 			n += fmt::fprintf(
     24 				out,
     25 				if (tag.include) "+{}" else "-{}",
     26 				tag.name)?;
     27 		};
     28 	};
     29 	return n;
     30 };
     31 
     32 // Formats a [[srcset]] to an [[io::handle]].
     33 export fn format_srcset(out: io::handle, srcs: *srcset) (size | io::error) = {
     34 	let n = 0z;
     35 	n += fmt::fprint(out, "relevant tags: ")?;
     36 	n += format_tags(out, srcs.seentags)?;
     37 	n += fmt::fprintln(out)?;
     38 	const dt = date::from_instant(chrono::LOCAL, srcs.mtime);
     39 	n += date::format(out, "last change to source list: %F %T\n", &dt)?;
     40 
     41 	n += fmt::fprintln(out, "hare sources:")?;
     42 	for (let ha .. srcs.ha) {
     43 		n += fmt::fprintln(out, "  ", ha)?;
     44 	};
     45 
     46 	n += fmt::fprintln(out, "assembly sources:")?;
     47 	for (let s .. srcs.s) {
     48 		n += fmt::fprintln(out, "  ", s)?;
     49 	};
     50 
     51 	n += fmt::fprintln(out, "object sources:")?;
     52 	for (let o .. srcs.o) {
     53 		n += fmt::fprintln(out, "  ", o)?;
     54 	};
     55 
     56 	return n;
     57 };
     58 
     59 // Formats a [[module]] to an [[io::handle]].
     60 export fn format(out: io::handle, mod: *module) (size | io::error) = {
     61 	let n = 0z;
     62 	n += fmt::fprintln(out, "module:", mod.name)?;
     63 	n += fmt::fprintln(out, "path:", mod.path)?;
     64 	n += format_srcset(out, &mod.srcs)?;
     65 	n += fmt::fprintln(out, "dependencies:")?;
     66 	for (let (_, ident) .. mod.deps) {
     67 		n += fmt::fprint(out, "  ")?;
     68 		n += unparse::ident(out, ident)?;
     69 		n += fmt::fprintln(out)?;
     70 	};
     71 	return n;
     72 };