thag_rs 0.2.0

A versatile cross-platform playground and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
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
/*[toml]
[dependencies]
thag_common = { version = "0.2, thag-auto" }
*/
/// `thag` front-end command to run Rust scripts from URLs provided that `thag` can figure out the dependencies.
///
/// It supports raw files as well as GitHub, GitLab, Bitbucket and the Rust Playground.
///
/// This relies on `thag`'s dependency inference to resolve dependencies and even features (since default
/// features for a given crate can be configured via `thag -C`). Failing that, you can of course paste the raw
/// source into the thag playground (`thag -d`) and edit / run / save it there, or directly into a `.rs` file
/// and run it with `thag /path/to/file`.
///
/// Usage:
///
/// ```bash
/// thag_url <url> [additional_thag_args] [-- <script_args>]
/// ```
///
/// Example:
///
/// ```bash
/// thag_url https://github.com/clap-rs/clap/blob/master/examples/demo.rs -- --name "is this the Krusty Krab?"
/// ```
///
//# Purpose: A front-end to allow `thag` to run scripts from URLs while keeping `thag` itself free of network dependencies.
//# Categories: technique, thag_front_ends, tools
use std::{error::Error, process::Command, string::ToString};
use syn::{parse_file, Expr};
use thag_common::{auto_help, help_system::check_help_and_exit, set_verbosity_from_env};
use url::Url;

enum SourceType {
    GitHub,
    GitLab,
    Bitbucket,
    RustPlayground,
    Raw,
}

#[derive(Debug)]
enum UrlError {
    Http(String),
    ParseError(String),
    SyntaxError(String),
}

impl std::fmt::Display for UrlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Http(msg) => write!(f, "HTTP Error: {msg}"),
            Self::ParseError(msg) => write!(f, "Parse Error: {msg}"),
            Self::SyntaxError(msg) => write!(f, "Syntax Error: {msg}"),
        }
    }
}

impl Error for UrlError {}

fn validate_rust_content(content: &str) -> Result<(), UrlError> {
    // Try parsing as a complete file first
    if parse_file(content).is_ok() {
        return Ok(());
    }

    // Try parsing as an expression
    // if syn::parse_str::<syn::Expr>(content).is_ok() {
    //     return Ok(());
    // }
    if extract_ast_expr(content).is_ok() {
        return Ok(());
    }

    // eprintln!("content={content}");
    // If parsing failed, format the content for better error display
    let temp_file = tempfile::NamedTempFile::new()
        .map_err(|e| UrlError::ParseError(format!("Failed to create temp file: {e}")))?;

    std::fs::write(temp_file.path(), content)
        .map_err(|e| UrlError::ParseError(format!("Failed to write content: {e}")))?;

    // Run rustfmt on the content
    let output = Command::new("rustfmt")
        .arg("--edition")
        .arg("2021")
        .arg(temp_file.path())
        .output()
        .map_err(|e| UrlError::ParseError(format!("Failed to run rustfmt: {e}")))?;

    // Display formatted content
    println!("Invalid Rust syntax. Formatted content:");
    println!("{}", "─".repeat(40));
    println!("{}", String::from_utf8_lossy(&output.stdout));
    println!("{}", "─".repeat(40));

    Err(UrlError::SyntaxError(
        "Content is not valid Rust code".to_string(),
    ))
}

/// Extracts the syn AST expression for a Rust code section.
///
/// # Errors
///
/// This function will bubble up any `syn` parse errors encountered.
pub fn extract_ast_expr(rs_source: &str) -> Result<Expr, syn::Error> {
    let mut expr: Result<Expr, syn::Error> = syn::parse_str::<Expr>(rs_source);
    if expr.is_err() && !(rs_source.starts_with('{') && rs_source.ends_with('}')) {
        // Try putting the expression in braces.
        let string = format!(r"{{{rs_source}}}");
        let str = string.as_str();
        // vprtln!(V::N, "str={str}");

        expr = syn::parse_str::<Expr>(str);
    }
    expr
}

fn fetch_and_validate(url: &str) -> Result<String, UrlError> {
    let response = tinyget::get(url)
        .send()
        .map_err(|e| UrlError::Http(format!("Failed to fetch URL: {e}")))?;

    // eprintln!(
    //     "response={response:#?}, response.status_code={}",
    //     response.status_code
    // );

    if response.status_code != 200 {
        return Err(UrlError::Http(format!(
            "HTTP {} - {}",
            response.status_code, response.reason_phrase
        )));
    }

    let content = response
        .as_str()
        .map_err(|e| UrlError::Http(format!("Failed to read response: {e}")))?;

    // Validate content before returning
    validate_rust_content(content)?;

    Ok(content.to_string())
}

fn detect_source_type(url: &Url) -> SourceType {
    url.host_str().map_or(SourceType::Raw, |host| match host {
        "github.com" => SourceType::GitHub,
        "gitlab.com" => SourceType::GitLab,
        "bitbucket.org" => SourceType::Bitbucket,
        "play.rust-lang.org" => SourceType::RustPlayground,
        _ => SourceType::Raw,
    })
}

fn convert_to_raw_url(url_str: &str) -> Result<String, UrlError> {
    let url = Url::parse(url_str).map_err(|e| UrlError::ParseError(format!("Invalid URL: {e}")))?;

    match detect_source_type(&url) {
        SourceType::GitHub => {
            let path = url.path();

            if path.contains("/raw/") {
                return Ok(url_str.to_string());
            }

            if !path.contains("/blob/") {
                return Err(UrlError::SyntaxError(
                    "GitHub URL must contain '/blob/' in path".to_string(),
                ));
            }
            if path.split('/').count() < 4 {
                return Err(UrlError::SyntaxError(
                    "Invalid GitHub URL format: expected user/repo/blob/path".to_string(),
                ));
            }
            let raw_url = url_str
                .replace("github.com", "raw.githubusercontent.com")
                .replace("/blob/", "/");
            eprintln!("raw_url={raw_url}");
            Ok(raw_url)
        }
        SourceType::GitLab => {
            let path = url.path();
            if !path.contains("/-/blob/") {
                return Err(UrlError::SyntaxError(
                    "GitLab URL must contain '/-/blob/' in path".to_string(),
                ));
            }
            if path.split('/').count() < 5 {
                return Err(UrlError::SyntaxError(
                    "Invalid GitLab URL format: expected user/repo/-/blob/path".to_string(),
                ));
            }
            Ok(url_str.replace("/-/blob/", "/-/raw/"))
        }
        SourceType::Bitbucket => {
            let path = url.path();
            if !path.contains("/src/") {
                return Err(UrlError::SyntaxError(
                    "Bitbucket URL must contain '/src/' in path".to_string(),
                ));
            }
            if path.split('/').count() < 4 {
                return Err(UrlError::SyntaxError(
                    "Invalid Bitbucket URL format: expected user/repo/src/path".to_string(),
                ));
            }
            Ok(url_str.replace("/src/", "/raw/"))
        }
        SourceType::RustPlayground => {
            let gist_id = url
                .query_pairs()
                .find(|(key, _)| key == "gist")
                .map(|(_, value)| value.to_string())
                .ok_or_else(|| {
                    UrlError::SyntaxError("No gist ID found in Playground URL".to_string())
                })?;

            if gist_id.len() != 32 {
                // Standard GitHub gist ID length
                return Err(UrlError::SyntaxError("Invalid gist ID format".to_string()));
            }

            Ok(format!(
                "https://gist.githubusercontent.com/rust-play/{gist_id}/raw"
            ))
        }
        SourceType::Raw => Ok(url_str.to_string()),
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    // Check for help first - automatically extracts from source comments
    let help = auto_help!();
    check_help_and_exit(&help);

    set_verbosity_from_env();

    let args: Vec<String> = std::env::args().collect();

    // Need at least URL and optionally additional flags
    if args.len() < 2 {
        print_usage(&args[0]);
        std::process::exit(1);
    }

    eprintln!("args={args:#?}");

    // Parse arguments
    let mut iter = args.iter().skip(1); // skip program name
    let mut url = String::new();
    let mut additional_args = Vec::new();
    let mut found_separator = false;

    for arg in iter.by_ref() {
        match arg.as_str() {
            "--" => {
                found_separator = true;
                additional_args.push("--".to_string());
                break;
            }
            arg => {
                if url.is_empty() {
                    url = arg.to_string();
                } else {
                    additional_args.push(arg.to_string());
                }
            }
        }
    }

    // Collect remaining args after --
    if found_separator {
        additional_args.extend(iter.map(ToString::to_string));
    }

    if url.is_empty() {
        print_usage(&args[0]);
        std::process::exit(1);
    }

    let raw_url = convert_to_raw_url(&url)?;

    match fetch_and_validate(&raw_url) {
        Ok(content) => {
            // Create a temporary file to save the script
            let temp_dir = std::env::temp_dir();
            let pid = std::process::id();
            let temp_file_path = temp_dir.join(format!("web_script_{pid}.rs"));
            let temp_ws_dir = temp_dir.join(format!("thag_rs/web_script_{pid}"));

            // Write content to temporary file
            std::fs::write(&temp_file_path, &content)?;

            eprintln!("Created temporary script at: {}", temp_file_path.display());
            eprintln!("additional_args={additional_args:#?}");

            // Run thag with the temporary file instead of using stdin
            let mut child = Command::new("thag")
                .arg(&temp_file_path)
                .args(&additional_args)
                .spawn()?;

            // Wait for thag to complete
            let status = child.wait()?;

            // Clean up the temporary file
            eprintln!("Removing temporary script at: {}", temp_file_path.display());
            if let Err(e) = std::fs::remove_file(&temp_file_path) {
                eprintln!("Warning: Could not remove temporary directory: {e}");
            }

            // Clean up the temporary target directory
            eprintln!(
                "Removing temporary target directory at: {}",
                temp_ws_dir.display()
            );
            if let Err(e) = std::fs::remove_dir_all(&temp_ws_dir) {
                panic!("Warning: Could not remove temporary target directory: {e}");
            }

            if !status.success() {
                std::process::exit(status.code().unwrap_or(1));
            }
            Ok(())
        }
        Err(e) => {
            match e {
                UrlError::Http(ref msg) => {
                    eprintln!("Failed to fetch content: {msg}");
                }
                UrlError::ParseError(ref msg) => {
                    eprintln!("Failed to process content: {msg}");
                }
                UrlError::SyntaxError(ref msg) => {
                    eprintln!("Invalid Rust code: {msg}");
                }
            }
            std::process::exit(1);
        }
    }
}

fn print_usage(program: &str) {
    eprintln!("Usage: {program} <url> [additional_thag_args] [-- <script_args>]");
    eprintln!("Supported sources:");
    eprintln!("  - GitHub (github.com)");
    eprintln!("  - GitLab (gitlab.com)");
    eprintln!("  - Bitbucket (bitbucket.org)");
    eprintln!("  - Rust Playground (play.rust-lang.org)");
    eprintln!("  - Raw URLs (direct links to raw content)");
    eprintln!("\nExamples:");
    eprintln!("  {program} https://github.com/user/repo/blob/master/script.rs -- -m");
    eprintln!("  {program} https://github.com/user/repo/blob/master/script.rs -v");
    eprintln!(
        "  {program} https://github.com/user/repo/blob/master/script.rs -- script_arg1 script_arg2"
    );
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn test_github_blob_url() {
        let url = "https://github.com/durbanlegend/thag_rs/blob/master/demo/hello.rs";
        let expected =
            "https://raw.githubusercontent.com/durbanlegend/thag_rs/master/demo/hello.rs";
        assert_eq!(convert_to_raw_url(url).unwrap(), expected);
    }

    #[test]
    fn test_github_raw_url() {
        let raw_url = "https://github.com/mikaelmello/inquire/raw/refs/heads/main/inquire/examples/complex_autocompletion.rs";
        assert_eq!(convert_to_raw_url(raw_url).unwrap().as_str(), raw_url);
    }

    #[test]
    fn test_gitlab_url() {
        // Example from gitlab.com/rust-embedded/cortex-m
        let url = "https://gitlab.com/rust-embedded/cortex-m/-/blob/master/src/lib.rs";
        let expected = "https://gitlab.com/rust-embedded/cortex-m/-/raw/master/src/lib.rs";
        assert_eq!(convert_to_raw_url(url).unwrap(), expected);
    }

    #[test]
    fn test_bitbucket_url() {
        // Example from bitbucket.org/atlassian/atlaskit-mk-2
        let url =
            "https://bitbucket.org/atlassian/atlaskit-mk-2/src/master/build/docs/src/md/index.ts";
        let expected =
            "https://bitbucket.org/atlassian/atlaskit-mk-2/raw/master/build/docs/raw/md/index.ts";
        assert_eq!(convert_to_raw_url(url).unwrap(), expected);
    }

    #[test]
    fn test_playground_url() {
        let url = "https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=362dc87d7c1c8f2d569cc205165424d3";
        let expected =
            "https://gist.githubusercontent.com/rust-play/362dc87d7c1c8f2d569cc205165424d3/raw";
        assert_eq!(convert_to_raw_url(url).unwrap(), expected);
    }

    #[test]
    fn test_invalid_urls() {
        // Test invalid URL format
        assert!(convert_to_raw_url("not_a_url").is_err());

        // Test invalid GitHub URL
        assert!(convert_to_raw_url("https://github.com/user/repo").is_err());

        // Test invalid GitLab URL
        assert!(convert_to_raw_url("https://gitlab.com/user/repo/blob/master/file.rs").is_err());

        // Test invalid Playground URL (no gist parameter)
        assert!(convert_to_raw_url("https://play.rust-lang.org/?version=stable").is_err());
    }

    #[test]
    fn test_raw_url() {
        let url = "https://example.com/raw/file.rs";
        assert_eq!(convert_to_raw_url(url).unwrap(), url);
    }
}