gftp/list

Parsers for FTP directory listing formats: LIST (POSIX and DOS), MLSD, and MLST.

LIST parsing

The LIST output format is not standardized and depends on the FTP server. This parser supports both POSIX and DOS formats, trying POSIX first. If you find a format that doesn’t parse correctly, please report an issue at https://github.com/veeso/gftp.

import gftp/list
import gftp/list/file

let line = "-rw-r--r-- 1 user group 1234 Nov 5 13:46 example.txt"
let assert Ok(f) = list.parse_list(line)
let name = file.name(f) // "example.txt"
let size = file.size(f) // 1234

MLSD/MLST parsing (RFC 3659)

Machine-readable listing formats provide structured, standardized output:

import gftp/list
import gftp/list/file

let line = "type=file;size=1234;modify=20200105134600; example.txt"
let assert Ok(f) = list.parse_mlsd(line)
let name = file.name(f) // "example.txt"

Usage with gftp

import gftp
import gftp/list
import gleam/list as gleam_list
import gleam/option.{None}

let assert Ok(lines) = gftp.list(client, None)
let assert Ok(files) = gleam_list.try_map(lines, list.parse_list)

Types

The possible errors that can occur when parsing a line from a LIST, MLSD, or MLST command output.

pub type ParseError {
  SyntaxError
  InvalidDate
  BadSize
}

Constructors

  • SyntaxError
  • InvalidDate
  • BadSize

The result of parsing a line from a LIST, MLSD, or MLST command output.

pub type ParseResult =
  Result(file.File, ParseError)

Values

pub fn describe_error(error: ParseError) -> String

Describe the given ParseError in human-readable form.

pub fn parse_list(line: String) -> Result(file.File, ParseError)

Parse a LIST output line (POSIX or DOS format) into a File.

Tries POSIX format first, then falls back to DOS format.

// POSIX format
let assert Ok(f) = list.parse_list("-rw-r--r-- 1 user group 1234 Nov 5 13:46 example.txt")

// DOS format
let assert Ok(f) = list.parse_list("10-19-20  03:19PM       403 readme.txt")
pub fn parse_mlsd(line: String) -> Result(file.File, ParseError)

Parse a line from MLSD command output into a File.

let line = "type=file;size=1024;modify=20200105134600; readme.txt"
let assert Ok(f) = list.parse_mlsd(line)
pub fn parse_mlst(line: String) -> Result(file.File, ParseError)

Parse a line from MLST command output into a File.

let line = "type=file;size=2048;modify=20210315120000; document.pdf"
let assert Ok(f) = list.parse_mlst(line)
Search Document