GithubHelp home page GithubHelp logo

reading from stdin? about multer-rs HOT 4 CLOSED

rwf2 avatar rwf2 commented on June 9, 2024
reading from stdin?

from multer-rs.

Comments (4)

SergioBenitez avatar SergioBenitez commented on June 9, 2024 1

It's really much easier than that:

[dependencies]
multer = { version = "2.1.0", features = ["tokio-io"] }
tokio = { version = "1.32.0", features = ["full"] }
use tokio::io;
use multer::Multipart;

#[tokio::main]
async fn main() -> multer::Result<()>  {
    let mut multipart = Multipart::with_reader(io::stdin(), "X-BOUNDARY");
    while let Some(mut field) = multipart.next_field().await? {
        while let Some(chunk) = field.chunk().await? {
            println!("chunk: {:?}", chunk);
        }
    }

    Ok(())
}
> echo "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n" | cargo run
Chunk: b"abcd"

from multer-rs.

davehorner avatar davehorner commented on June 9, 2024

So close, but so far. ReceiverStream provides ReceiverStream as Stream ...I need a Result<O, E>.

use futures_util::stream::StreamExt;
use tokio::io;
use async_stdin::recv_from_stdin;
use tokio_stream::wrappers::ReceiverStream;
use multer::Multipart;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut rx = recv_from_stdin(10);

    let stream = ReceiverStream::new(rx);

    let boundary = "X-BOUNDARY";

    // Create a Multipart instance from the byte stream and boundary.
    let mut multipart = Multipart::new(stream, boundary);

    while let Some(field) = multipart.next_field().await? {
        let name = field.name();
        let file_name = field.file_name();

        println!("Name: {:?}, File Name: {:?}", name, file_name);

        // Read field content as bytes.
        let mut bytes = Vec::new();
        //field.copy_to(&mut bytes).await?;

        // Convert bytes to a UTF-8 string.
        let content = String::from_utf8_lossy(&bytes).to_string();
        println!("Content: {:?}", content);
    }

    Ok(())
}
error[E0271]: type mismatch resolving `<ReceiverStream<String> as Stream>::Item == Result<_, _>`
   --> examples\simple_stdin.rs:16:40
    |
16  |     let mut multipart = Multipart::new(stream, boundary);
    |                         -------------- ^^^^^^ expected `Result<_, _>`, found `String`
    |                         |
    |                         required by a bound introduced by this call
    |
    = note: expected enum `Result<_, _>`
             found struct `String`
note: required by a bound in `Multipart::<'r>::new`
   --> c:\working\rust\multer-rs\src\multipart.rs:107:19
    |
105 |     pub fn new<S, O, E, B>(stream: S, boundary: B) -> Self
    |            --- required by a bound in this associated function
106 |     where
107 |         S: Stream<Item = Result<O, E>> + Send + 'r,
    |                   ^^^^^^^^^^^^^^^^^^^ required by this bound in `Multipart::<'r>::new`

from multer-rs.

davehorner avatar davehorner commented on June 9, 2024

oh man, so very nice! that worked like a charm. thank you.

from multer-rs.

davehorner avatar davehorner commented on June 9, 2024

de-vri-es/webhook-httpd@7bddfcb
You're welcome to the example if it might help others. very cool.

from multer-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.