Parcourir la source

Create gists

master
Dylan Baker il y a 5 ans
Parent
révision
6b2414e2db
5 fichiers modifiés avec 50 ajouts et 2 suppressions
  1. 5
    0
      Cargo.lock
  2. 2
    0
      Cargo.toml
  3. 9
    2
      src/cli.rs
  4. 30
    0
      src/client.rs
  5. 4
    0
      src/main.rs

+ 5
- 0
Cargo.lock Voir le fichier

335
 dependencies = [
335
 dependencies = [
336
  "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
336
  "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
337
  "reqwest 0.9.16 (registry+https://github.com/rust-lang/crates.io-index)",
337
  "reqwest 0.9.16 (registry+https://github.com/rust-lang/crates.io-index)",
338
+ "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
339
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
338
 ]
340
 ]
339
 
341
 
340
 [[package]]
342
 [[package]]
982
 name = "serde"
984
 name = "serde"
983
 version = "1.0.90"
985
 version = "1.0.90"
984
 source = "registry+https://github.com/rust-lang/crates.io-index"
986
 source = "registry+https://github.com/rust-lang/crates.io-index"
987
+dependencies = [
988
+ "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
989
+]
985
 
990
 
986
 [[package]]
991
 [[package]]
987
 name = "serde_derive"
992
 name = "serde_derive"

+ 2
- 0
Cargo.toml Voir le fichier

7
 [dependencies]
7
 [dependencies]
8
 clap = "2.33.0"
8
 clap = "2.33.0"
9
 reqwest = "0.9.16"
9
 reqwest = "0.9.16"
10
+serde = { version = "1.0.90", features = ["derive"] }
11
+serde_json = "1.0"

+ 9
- 2
src/cli.rs Voir le fichier

2
 use std::fs;
2
 use std::fs;
3
 use std::io;
3
 use std::io;
4
 
4
 
5
+use crate::client::create_gist;
6
+
5
 pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
7
 pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
6
     let title = matches.value_of("title").unwrap();
8
     let title = matches.value_of("title").unwrap();
7
     let input = matches.value_of("input");
9
     let input = matches.value_of("input");
14
     let mut body = String::new();
16
     let mut body = String::new();
15
     rdr.read_to_string(&mut body)?;
17
     rdr.read_to_string(&mut body)?;
16
 
18
 
17
-    println!("title: {}", title);
18
-    println!("body: {}", body);
19
+    match create_gist(title, &body) {
20
+        Ok(resp) => println!(
21
+            "Success! Your new gist is available at http://localhost:8000/gists/{}",
22
+            resp.id
23
+        ),
24
+        Err(err) => println!("{}", err),
25
+    }
19
 
26
 
20
     Ok(())
27
     Ok(())
21
 }
28
 }

+ 30
- 0
src/client.rs Voir le fichier

1
+use serde::{Deserialize, Serialize};
2
+
3
+#[derive(Serialize, Deserialize, Debug)]
4
+pub struct OutgoingGist {
5
+    pub title: String,
6
+    pub body: String,
7
+}
8
+
9
+#[derive(Serialize, Deserialize, Debug)]
10
+pub struct IncomingGist {
11
+    pub id: i32,
12
+    pub title: String,
13
+    pub body: String,
14
+}
15
+
16
+pub fn create_gist(title: &str, body: &str) -> Result<IncomingGist, Box<std::error::Error>> {
17
+    let client = reqwest::Client::new();
18
+    let gist = OutgoingGist {
19
+        title: title.to_string(),
20
+        body: body.to_string(),
21
+    };
22
+
23
+    let resp: IncomingGist = client
24
+        .post("http://localhost:8000/api/gists")
25
+        .json(&gist)
26
+        .send()?
27
+        .json()?;
28
+
29
+    Ok(resp)
30
+}

+ 4
- 0
src/main.rs Voir le fichier

1
 extern crate clap;
1
 extern crate clap;
2
+extern crate reqwest;
3
+extern crate serde;
4
+extern crate serde_json;
2
 
5
 
3
 use clap::{App, Arg, SubCommand};
6
 use clap::{App, Arg, SubCommand};
4
 use std::io;
7
 use std::io;
5
 
8
 
6
 mod cli;
9
 mod cli;
10
+mod client;
7
 
11
 
8
 fn main() -> io::Result<()> {
12
 fn main() -> io::Result<()> {
9
     let matches = App::new("gist")
13
     let matches = App::new("gist")

Chargement…
Annuler
Enregistrer