[][src]Function tree_magic::from_u8_node

pub fn from_u8_node(parentnode: NodeIndex, bytes: &[u8]) -> Option<String>

Gets the type of a file from a raw bytestream, starting at a certain node in the type graph.

Returns MIME as string wrapped in Some if a type matches, or None if no match is found under the given node. Retreive the node from the TYPE.hash HashMap, using the MIME as the key.

Panics

Will panic if the given node is not found in the graph. As the graph is immutable, this should not happen if the node index comes from TYPE.hash.

Examples

/// In this example, we know we have a ZIP, but we want to see if it's something
/// like an Office document that subclasses a ZIP. If it is not, like this example,
/// it will return None.

// Load a ZIP file
let input: &[u8] = include_bytes!("../tests/application/zip");
 
// Get the graph node for ZIP
let zipnode = tree_magic::TYPE.hash.get("application/zip").unwrap();

// Find the MIME type of the ZIP, starting from ZIP.
let result = tree_magic::from_u8_node(*zipnode, input);
assert_eq!(result, None);