Struct RiffFile

Source
pub struct RiffFile { /* private fields */ }
Expand description

RIFF file

Implementations§

Source§

impl RiffFile

Resource Interchange File Format

Source

pub fn open(filename: &str) -> Result<Self>

Open a RIFF file from a filename

Examples found in repository?
examples/view.rs (line 18)
10fn main() -> Result<()> {
11    if env::args().len() < 2 {
12        println!("Usage: view [filename]");
13        exit(-1);
14    }
15
16    let filename = env::args().nth(1).unwrap();
17
18    let file = RiffFile::open(&filename)?;
19
20    println!("File type: {}", format_fourcc(file.file_type()));
21    println!("File size: {}", file.file_size());
22
23    show_entry(&file.read_file()?, 0, file.bytes())?;
24    Ok(())
25}
More examples
Hide additional examples
examples/copy.rs (line 20)
11fn main() -> Result<()> {
12    if env::args().len() < 2 {
13        println!("Usage: copy [source] [desy]");
14        exit(-1);
15    }
16
17    let filename_src = env::args().nth(1).unwrap();
18    let filename_dst = env::args().nth(2).unwrap();
19
20    let file = RiffFile::open(&filename_src)?;
21    let mut outfile = File::create(&filename_dst)?;
22
23    let toplevel = file.read_file()?;
24    toplevel.to_owned(file.bytes()).write(&mut outfile)?;
25
26    Ok(())
27}
examples/copy_metadata.rs (line 53)
43fn main() -> Result<()> {
44    if env::args().len() < 2 {
45        println!("Usage: copy [source] [desy]");
46        exit(-1);
47    }
48
49    let filename_src = env::args().nth(1).unwrap();
50    let filename_base = env::args().nth(2).unwrap();
51    let filename_new = env::args().nth(3).unwrap();
52
53    let file = RiffFile::open(&filename_src)?;
54    let toplevel = file.read_file()?;
55    let avif_chunk = find_chunk(
56        &toplevel,
57        &|c: &Chunk<DataRef>| {
58            let b = c.bytes(file.bytes());
59            &c.id == b"strd" && &b[..4] == b"AVIF"
60        },
61    ).unwrap();
62    let avif_chunk = avif_chunk.to_owned(file.bytes());
63    let avif_chunk = Entry::Chunk(avif_chunk);
64    let name_chunk = find_chunk(
65        &toplevel,
66        &|c: &Chunk<DataRef>| {
67            let b = c.bytes(file.bytes());
68            &c.id == b"strn" && &b[..4] == b"FUJI"
69        },
70    ).unwrap();
71    let name_chunk = name_chunk.to_owned(file.bytes());
72    let name_chunk = Entry::Chunk(name_chunk);
73    
74    
75    let file = RiffFile::open(&filename_base)?;
76    let toplevel = file.read_file()?.to_owned(file.bytes());
77    
78    let new_toplevel = walk(toplevel, &|e| match e {
79        Entry::List(l) if &l.list_type == b"strl" => {
80            let mut l = l.clone();
81            let avif_chunk = avif_chunk.clone();
82            let position = l.children.iter().position(|e| match e {
83                Entry::Chunk(c) if &c.id == b"strd" => true,
84                _ => false,
85            });
86            match position {
87                Some(p) => {
88                    l.children[p] = avif_chunk;
89                },
90                None => {
91                    let pos = l.children.iter().position(|e| match e {
92                        Entry::Chunk(c) if &c.id == b"strf" => true,
93                        _ => false,
94                    });
95                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
96                    l.children.insert(pos, avif_chunk);
97                },
98            };
99            let name_chunk = name_chunk.clone();
100            let position = l.children.iter().position(|e| match e {
101                Entry::Chunk(c) if &c.id == b"strn" => true,
102                _ => false,
103            });
104            match position {
105                Some(p) => {
106                    l.children[p] = name_chunk;
107                },
108                None => {
109                    let pos = l.children.iter().position(|e| match e {
110                        Entry::Chunk(c) if &c.id == b"strd" => true,
111                        _ => false,
112                    });
113                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
114                    l.children.insert(pos, name_chunk);
115                },
116            };
117            Entry::List(l)
118        },
119         other => other,
120    });
121
122    let mut outfile = File::create(&filename_new)?;
123    
124    new_toplevel.write(&mut outfile)?;
125
126    Ok(())
127}
Source

pub fn open_with_file_handle(file: &File) -> Result<Self>

Open a RIFF file from a File handle

Source

pub fn file_type(&self) -> &FourCC

Examples found in repository?
examples/view.rs (line 20)
10fn main() -> Result<()> {
11    if env::args().len() < 2 {
12        println!("Usage: view [filename]");
13        exit(-1);
14    }
15
16    let filename = env::args().nth(1).unwrap();
17
18    let file = RiffFile::open(&filename)?;
19
20    println!("File type: {}", format_fourcc(file.file_type()));
21    println!("File size: {}", file.file_size());
22
23    show_entry(&file.read_file()?, 0, file.bytes())?;
24    Ok(())
25}
Source

pub fn file_size(&self) -> usize

Examples found in repository?
examples/view.rs (line 21)
10fn main() -> Result<()> {
11    if env::args().len() < 2 {
12        println!("Usage: view [filename]");
13        exit(-1);
14    }
15
16    let filename = env::args().nth(1).unwrap();
17
18    let file = RiffFile::open(&filename)?;
19
20    println!("File type: {}", format_fourcc(file.file_type()));
21    println!("File size: {}", file.file_size());
22
23    show_entry(&file.read_file()?, 0, file.bytes())?;
24    Ok(())
25}
Source

pub fn bytes(&self) -> &[u8]

Examples found in repository?
examples/view.rs (line 23)
10fn main() -> Result<()> {
11    if env::args().len() < 2 {
12        println!("Usage: view [filename]");
13        exit(-1);
14    }
15
16    let filename = env::args().nth(1).unwrap();
17
18    let file = RiffFile::open(&filename)?;
19
20    println!("File type: {}", format_fourcc(file.file_type()));
21    println!("File size: {}", file.file_size());
22
23    show_entry(&file.read_file()?, 0, file.bytes())?;
24    Ok(())
25}
More examples
Hide additional examples
examples/copy.rs (line 24)
11fn main() -> Result<()> {
12    if env::args().len() < 2 {
13        println!("Usage: copy [source] [desy]");
14        exit(-1);
15    }
16
17    let filename_src = env::args().nth(1).unwrap();
18    let filename_dst = env::args().nth(2).unwrap();
19
20    let file = RiffFile::open(&filename_src)?;
21    let mut outfile = File::create(&filename_dst)?;
22
23    let toplevel = file.read_file()?;
24    toplevel.to_owned(file.bytes()).write(&mut outfile)?;
25
26    Ok(())
27}
examples/copy_metadata.rs (line 58)
43fn main() -> Result<()> {
44    if env::args().len() < 2 {
45        println!("Usage: copy [source] [desy]");
46        exit(-1);
47    }
48
49    let filename_src = env::args().nth(1).unwrap();
50    let filename_base = env::args().nth(2).unwrap();
51    let filename_new = env::args().nth(3).unwrap();
52
53    let file = RiffFile::open(&filename_src)?;
54    let toplevel = file.read_file()?;
55    let avif_chunk = find_chunk(
56        &toplevel,
57        &|c: &Chunk<DataRef>| {
58            let b = c.bytes(file.bytes());
59            &c.id == b"strd" && &b[..4] == b"AVIF"
60        },
61    ).unwrap();
62    let avif_chunk = avif_chunk.to_owned(file.bytes());
63    let avif_chunk = Entry::Chunk(avif_chunk);
64    let name_chunk = find_chunk(
65        &toplevel,
66        &|c: &Chunk<DataRef>| {
67            let b = c.bytes(file.bytes());
68            &c.id == b"strn" && &b[..4] == b"FUJI"
69        },
70    ).unwrap();
71    let name_chunk = name_chunk.to_owned(file.bytes());
72    let name_chunk = Entry::Chunk(name_chunk);
73    
74    
75    let file = RiffFile::open(&filename_base)?;
76    let toplevel = file.read_file()?.to_owned(file.bytes());
77    
78    let new_toplevel = walk(toplevel, &|e| match e {
79        Entry::List(l) if &l.list_type == b"strl" => {
80            let mut l = l.clone();
81            let avif_chunk = avif_chunk.clone();
82            let position = l.children.iter().position(|e| match e {
83                Entry::Chunk(c) if &c.id == b"strd" => true,
84                _ => false,
85            });
86            match position {
87                Some(p) => {
88                    l.children[p] = avif_chunk;
89                },
90                None => {
91                    let pos = l.children.iter().position(|e| match e {
92                        Entry::Chunk(c) if &c.id == b"strf" => true,
93                        _ => false,
94                    });
95                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
96                    l.children.insert(pos, avif_chunk);
97                },
98            };
99            let name_chunk = name_chunk.clone();
100            let position = l.children.iter().position(|e| match e {
101                Entry::Chunk(c) if &c.id == b"strn" => true,
102                _ => false,
103            });
104            match position {
105                Some(p) => {
106                    l.children[p] = name_chunk;
107                },
108                None => {
109                    let pos = l.children.iter().position(|e| match e {
110                        Entry::Chunk(c) if &c.id == b"strd" => true,
111                        _ => false,
112                    });
113                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
114                    l.children.insert(pos, name_chunk);
115                },
116            };
117            Entry::List(l)
118        },
119         other => other,
120    });
121
122    let mut outfile = File::create(&filename_new)?;
123    
124    new_toplevel.write(&mut outfile)?;
125
126    Ok(())
127}
Source

pub fn read_file(&self) -> Result<Entry<DataRef>>

Examples found in repository?
examples/view.rs (line 23)
10fn main() -> Result<()> {
11    if env::args().len() < 2 {
12        println!("Usage: view [filename]");
13        exit(-1);
14    }
15
16    let filename = env::args().nth(1).unwrap();
17
18    let file = RiffFile::open(&filename)?;
19
20    println!("File type: {}", format_fourcc(file.file_type()));
21    println!("File size: {}", file.file_size());
22
23    show_entry(&file.read_file()?, 0, file.bytes())?;
24    Ok(())
25}
More examples
Hide additional examples
examples/copy.rs (line 23)
11fn main() -> Result<()> {
12    if env::args().len() < 2 {
13        println!("Usage: copy [source] [desy]");
14        exit(-1);
15    }
16
17    let filename_src = env::args().nth(1).unwrap();
18    let filename_dst = env::args().nth(2).unwrap();
19
20    let file = RiffFile::open(&filename_src)?;
21    let mut outfile = File::create(&filename_dst)?;
22
23    let toplevel = file.read_file()?;
24    toplevel.to_owned(file.bytes()).write(&mut outfile)?;
25
26    Ok(())
27}
examples/copy_metadata.rs (line 54)
43fn main() -> Result<()> {
44    if env::args().len() < 2 {
45        println!("Usage: copy [source] [desy]");
46        exit(-1);
47    }
48
49    let filename_src = env::args().nth(1).unwrap();
50    let filename_base = env::args().nth(2).unwrap();
51    let filename_new = env::args().nth(3).unwrap();
52
53    let file = RiffFile::open(&filename_src)?;
54    let toplevel = file.read_file()?;
55    let avif_chunk = find_chunk(
56        &toplevel,
57        &|c: &Chunk<DataRef>| {
58            let b = c.bytes(file.bytes());
59            &c.id == b"strd" && &b[..4] == b"AVIF"
60        },
61    ).unwrap();
62    let avif_chunk = avif_chunk.to_owned(file.bytes());
63    let avif_chunk = Entry::Chunk(avif_chunk);
64    let name_chunk = find_chunk(
65        &toplevel,
66        &|c: &Chunk<DataRef>| {
67            let b = c.bytes(file.bytes());
68            &c.id == b"strn" && &b[..4] == b"FUJI"
69        },
70    ).unwrap();
71    let name_chunk = name_chunk.to_owned(file.bytes());
72    let name_chunk = Entry::Chunk(name_chunk);
73    
74    
75    let file = RiffFile::open(&filename_base)?;
76    let toplevel = file.read_file()?.to_owned(file.bytes());
77    
78    let new_toplevel = walk(toplevel, &|e| match e {
79        Entry::List(l) if &l.list_type == b"strl" => {
80            let mut l = l.clone();
81            let avif_chunk = avif_chunk.clone();
82            let position = l.children.iter().position(|e| match e {
83                Entry::Chunk(c) if &c.id == b"strd" => true,
84                _ => false,
85            });
86            match position {
87                Some(p) => {
88                    l.children[p] = avif_chunk;
89                },
90                None => {
91                    let pos = l.children.iter().position(|e| match e {
92                        Entry::Chunk(c) if &c.id == b"strf" => true,
93                        _ => false,
94                    });
95                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
96                    l.children.insert(pos, avif_chunk);
97                },
98            };
99            let name_chunk = name_chunk.clone();
100            let position = l.children.iter().position(|e| match e {
101                Entry::Chunk(c) if &c.id == b"strn" => true,
102                _ => false,
103            });
104            match position {
105                Some(p) => {
106                    l.children[p] = name_chunk;
107                },
108                None => {
109                    let pos = l.children.iter().position(|e| match e {
110                        Entry::Chunk(c) if &c.id == b"strd" => true,
111                        _ => false,
112                    });
113                    let pos = pos.map(|i| i + 1).unwrap_or(l.children.len());
114                    l.children.insert(pos, name_chunk);
115                },
116            };
117            Entry::List(l)
118        },
119         other => other,
120    });
121
122    let mut outfile = File::create(&filename_new)?;
123    
124    new_toplevel.write(&mut outfile)?;
125
126    Ok(())
127}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.