Skip to main content

goish/
lib.rs

1// goish: write Rust using Go idioms.
2//
3//   use goish::prelude::*;
4//
5//   fn divide(a: int64, b: int64) -> (int64, error) {
6//       if b == 0 {
7//           return (0, errors::New("divide by zero"));
8//       }
9//       (a / b, nil)
10//   }
11//
12//   fn main() {
13//       Println!("hello", "world");
14//
15//       let (q, err) = divide(10, 0);
16//       if err != nil {
17//           Println!("error:", err);
18//       } else {
19//           Printf!("q = %d\n", q);
20//       }
21//   }
22//
23// Cheat-sheet:
24//
25//   Go                                goish
26//   ───────────────────────────────   ──────────────────────────────────
27//   int64 / float64 / byte / rune     int64 / float64 / byte / rune
28//   error                             error           (a newtype, not Option)
29//   nil                               nil             (zero-value error)
30//   if err != nil { ... }             if err != nil { ... }
31//   fmt.Println(a, b)                 fmt::Println!(a, b)
32//   fmt.Printf("%d", n)               fmt::Printf!("%d", n)
33//   fmt.Sprintf("%s", s)              fmt::Sprintf!("%s", s)
34//   errors.New("msg")                 errors::New("msg")
35
36#![allow(non_snake_case)]
37#![allow(non_camel_case_types)]
38#![allow(non_upper_case_globals)]
39// Our doc comments use Go-syntax cheat-sheets with `[brackets]` and `<T>`
40// that rustdoc would otherwise interpret as intra-doc-links / HTML tags.
41#![allow(rustdoc::broken_intra_doc_links)]
42#![allow(rustdoc::invalid_html_tags)]
43
44// Make `::goish::…` resolve to `crate::…` inside our own lib, so paths the
45// `select!` proc macro emits (e.g. `::goish::__flume::Selector`) work both
46// in downstream crates and in goish's own integration tests.
47extern crate self as goish;
48
49// ── Top-level Go packages ──────────────────────────────────────────────
50
51pub mod bufio;
52pub mod bytes;
53pub mod cmp;
54pub mod container;        // container/{list,heap,ring}
55pub mod context;
56pub mod crypto;           // crypto/{md5,sha1,sha256}
57pub mod encoding;         // encoding/{base64,binary,csv,hex,json}
58pub mod errors;
59pub mod flag;
60pub mod fmt;
61pub mod hash;             // hash/{crc32,fnv}
62pub mod html;
63pub mod io;
64pub mod iter;
65pub mod log;
66pub mod maps;
67pub mod math;             // math + math/rand
68pub mod mime;
69pub mod net;              // net/url (net/http in v0.5)
70pub mod os;               // os + os/exec
71pub mod path;             // path + path/filepath
72pub mod regexp;
73pub mod runtime;
74pub mod slices;
75pub mod sort;
76pub mod strconv;
77pub mod strings;
78pub mod sync;             // sync + sync/atomic
79pub mod testing;
80pub mod text;             // text/{tabwriter,scanner,template}
81pub mod time;
82pub mod unicode;          // unicode + unicode/utf8
83
84// ── Built-ins (Go keyword / language-level things) ─────────────────────
85
86#[doc(hidden)]
87pub mod __macros {
88    pub use goish_macros::rewrite_go_body;
89}
90
91/// Re-export of the `flume` crate so `select!` (proc-macro) can reference
92/// `$crate::__flume::Selector` without forcing users to add flume to
93/// their own `Cargo.toml`. Not a public API.
94#[doc(hidden)]
95pub use flume as __flume;
96
97/// `select!{ ... }` — Go's `select` statement (proc-macro form).
98///
99/// See `src/chan/mod.rs` for the semantic contract; the macro is
100/// defined in the `goish-macros` proc-macro crate.
101pub use goish_macros::select;
102
103/// Re-export of the `inventory` crate so the `test!` macro in user crates
104/// can submit registrations without the user depending on `inventory`
105/// directly. Not a public API.
106#[doc(hidden)]
107pub mod __goish_inventory {
108    pub use inventory::submit;
109}
110
111pub mod chan;
112#[doc(hidden)]
113pub mod clone_dyn;
114pub use clone_dyn::DynClone;
115pub mod consts;
116pub mod defer;
117pub mod gostring;
118pub mod goroutine;
119pub mod range;
120#[doc(hidden)]
121pub mod _map;
122#[doc(hidden)]
123pub mod _slice;
124pub mod types;
125#[doc(hidden)]
126pub mod struct_macro;
127pub use struct_macro::__goish_into_string;
128
129// Backward-compat flat re-exports — keeps v0.3 import paths working. New
130// code should prefer the Go-import-path form (`encoding::base64`, `math::rand`).
131pub use encoding::base64;
132pub use encoding::binary;
133pub use encoding::csv;
134pub use encoding::hex;
135pub use encoding::json;
136pub use math::rand;
137pub use net::http;
138pub use net::url;
139pub use os::exec;
140pub use path::filepath;
141pub use unicode::utf8;
142
143// Make Go primitive type names visible at the crate root so macros that
144// say `$crate::int` resolve correctly.
145pub use crate::types::*;
146
147pub mod prelude {
148    pub use crate::bufio;
149    pub use crate::bytes;
150    pub use crate::chan::Chan;
151    pub use crate::cmp;
152    pub use crate::container;
153    pub use crate::context;
154    pub use crate::crypto;
155    pub use crate::encoding;
156    pub use crate::errors::{self, error, nil, GoishError, IsNil};
157    pub use crate::clone_dyn::DynClone;
158    pub use crate::flag;
159    pub use crate::fmt;
160    pub use crate::hash;
161    pub use crate::html;
162    pub use crate::io;
163    pub use crate::io::{Reader as _GoishIoReader, Writer as _GoishIoWriter,
164                        Closer as _GoishIoCloser, Seeker as _GoishIoSeeker,
165                        ReaderAt as _GoishIoReaderAt, WriterAt as _GoishIoWriterAt};
166    pub use crate::iter;
167    pub use crate::log;
168    pub use crate::maps;
169    pub use crate::math;
170    pub use crate::mime;
171    pub use crate::net;
172    pub use crate::os;
173    pub use crate::path;
174    pub use crate::regexp;
175    pub use crate::runtime;
176    pub use crate::slices;
177    pub use crate::sort;
178    pub use crate::strconv;
179    pub use crate::strings;
180    pub use crate::sync;
181    pub use crate::testing;
182    pub use crate::text;
183    pub use crate::time;
184    pub use crate::unicode;
185
186    // v0.3-compat flat names — call site keeps the short form users expect.
187    pub use crate::base64;
188    pub use crate::binary;
189    pub use crate::csv;
190    pub use crate::exec;
191    pub use crate::filepath;
192    pub use crate::hex;
193    pub use crate::http;
194    pub use crate::json;
195    pub use crate::rand;
196    pub use crate::url;
197    pub use crate::utf8;
198
199    pub use crate::types::*;
200    pub use crate::goroutine::Goroutine;
201    pub use crate::{
202        Const, Cookie, Enum, Errorf, Fprintf, IntNewtype, MailAddress, Printf, Println,
203        SliceNewtype, clone_trait_object, copy, Sprintf, Type, append, benchmark, cap, cat, chan,
204        close, const_block, defer, delete, go, len, make, map, range, recover, select, slice,
205        static_err, string, stringer, Struct, test, test_h, test_main, var,
206    };
207
208    // Trait re-exports — required in scope for trait-method resolution like
209    // `x.Len()` when x impls sort::Interface.
210    pub use crate::sort::Interface as _SortInterface;
211}